← Blog
Claude CodeTutorialSetup Guide

How to Set Up Claude Code with an Affordable API Provider

· Updated (prices in the text follow the live price list)

Short answer

To run Claude Code through APIVAI, set two environment variables: ANTHROPIC_BASE_URL="https://api.apivai.com" and ANTHROPIC_AUTH_TOKEN="<your key>", then start claude as usual. Claude Code accepts any Anthropic-compatible API key through ANTHROPIC_AUTH_TOKEN. Claude Code itself does not change; requests go to the same Claude models and are billed from a prepaid balance at APIVAI prices, for example $1.15 / $5.74 per million input / output tokens for Claude Sonnet 4.6 (as of 2026-10-10), which is 62% below the official list price.

Setup takes about five minutes.

What is Claude Code?

Claude Code is Anthropic's command-line coding agent. You run it inside a project folder, describe a task in plain language, and it reads your files, proposes and makes edits, runs commands such as tests or builds, and explains what it did. Anthropic recommends its native installer (curl -fsSL https://claude.ai/install.sh | bash on macOS and Linux, irm https://claude.ai/install.ps1 | iex in Windows PowerShell); the npm package @anthropic-ai/claude-code still works and needs Node.js 22 or later. It is documented in Anthropic's Claude Code docs.

Every step Claude Code takes is an API request to a Claude model, and an agent working on a real codebase sends a lot of context with each request. That is why the per-token price matters more for Claude Code than for occasional chat use. By default it bills through Anthropic's API at list prices ($3.00 / $15.00 per million tokens for Claude Sonnet 4.6). For a broader look at the numbers, see how to cut Claude Code and GPT API costs.

How does a custom API provider work with Claude Code?

Claude Code reads its connection settings from environment variables:

  • ANTHROPIC_BASE_URL: where requests are sent. For APIVAI this is https://api.apivai.com, with no /v1 and no trailing slash, because Claude Code adds /v1/messages itself.
  • ANTHROPIC_AUTH_TOKEN: the key sent with each request.

What changes: the address and the key. What stays the same: Claude Code, its commands, tools, permissions and the Claude models it talks to.

Prerequisites

  • Claude Code installed (native installer, or npm with Node.js 22+; claude --version should print a version)
  • An APIVAI account with a topped-up balance
  • An API key created on the APIVAI Dashboard
  • A terminal: bash or zsh on macOS/Linux, PowerShell or cmd on Windows

Step 1: Get a Claude Code API key

  1. Create an account with your email and the verification code you receive.
  2. Top up your balance. The minimum is $10 by card or cryptocurrency, or ¥20 via Alipay / WeChat Pay. There is no subscription and no monthly minimum.
  3. On the Dashboard, create an API key. Give it a name such as "claude-code-laptop" and set a budget. When the key reaches its budget it stops working, so a runaway session cannot spend your whole balance.
  4. Copy the key and keep it private. Use a separate key per machine or per project so you can see costs separately and revoke one without affecting the others.

Step 2: Configure your terminal

macOS and Linux

Add these lines to your shell profile, ~/.zshrc (default on macOS) or ~/.bashrc:

export ANTHROPIC_BASE_URL="https://api.apivai.com"
export ANTHROPIC_AUTH_TOKEN="your-apivai-key"

Reload the profile and start Claude Code:

source ~/.zshrc   # or: source ~/.bashrc
claude

Windows PowerShell

Open your profile with notepad $PROFILE (create the file if PowerShell says it does not exist) and add:

$env:ANTHROPIC_BASE_URL = "https://api.apivai.com"
$env:ANTHROPIC_AUTH_TOKEN = "your-apivai-key"

Open a new PowerShell window and run claude.

Windows cmd

setx stores user environment variables permanently:

setx ANTHROPIC_BASE_URL "https://api.apivai.com"
setx ANTHROPIC_AUTH_TOKEN "your-apivai-key"

setx does not change the window you ran it in; it applies to terminals opened afterwards. Close the window, open a new one, then run claude. Variables set this way are also visible to PowerShell.

Alternative: Claude Code's settings file

Instead of shell variables, you can put them in Claude Code's user settings file, ~/.claude/settings.json (on Windows, %USERPROFILE%\.claude\settings.json), under an env object:

{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.apivai.com",
    "ANTHROPIC_AUTH_TOKEN": "your-apivai-key"
  }
}

If the file already exists, add the env block to it rather than replacing the file. This keeps the setting scoped to Claude Code. File locations and precedence can change between versions, so see Anthropic's Claude Code settings docs for the current details.

Step 3: Verify the key before starting Claude Code

A direct request tells you whether the key, the URL and your network are fine, before Claude Code is involved. On macOS/Linux, with the variables from step 2 loaded:

curl https://api.apivai.com/v1/messages \
  -H "x-api-key: $ANTHROPIC_AUTH_TOKEN" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{"model":"claude-haiku-4-5","max_tokens":50,"messages":[{"role":"user","content":"Say hello"}]}'

In PowerShell:

$body = '{"model":"claude-haiku-4-5","max_tokens":50,"messages":[{"role":"user","content":"Say hello"}]}'
Invoke-RestMethod -Uri "https://api.apivai.com/v1/messages" -Method Post -ContentType "application/json" -Headers @{ "x-api-key" = $env:ANTHROPIC_AUTH_TOKEN; "anthropic-version" = "2023-06-01" } -Body $body

A JSON reply with a content field means everything works, and the request appears on your Dashboard within moments. An error code points to the matching row in the troubleshooting table below. Note that the test URL contains /v1/messages, while ANTHROPIC_BASE_URL must not.

How do I choose and switch models?

Type /model inside Claude Code to pick a model for the current session. The main options, as of 2026-10-10 (full list on the pricing page):

ModelAPIVAI price per 1M (input / output)Good for
Claude Fable 5.1$4.42 / $22.08The hardest problems, long autonomous tasks
Claude Opus 5.5$1.54 / $7.65Complex coding, refactors, analysis
Claude Sonnet 5.5$0.77 / $3.82Everyday coding: the recommended default
Claude Sonnet 4.6$1.15 / $5.74Widely supported; Sonnet 5.5 is newer and cheaper
Claude Haiku 4.5$0.38 / $1.92Quick edits and simple questions

A practical pattern: start on Sonnet, switch to Opus with /model when a task gets stuck or needs deeper reasoning, and switch back once it is solved.

How do I keep Claude Code costs down?

  • Use Sonnet for routine work. Most edits, tests and explanations do not need Opus or Fable, and Sonnet costs a fraction of them per token.
  • Compact long sessions. Claude Code has a /compact command that summarizes the conversation so far, so later requests carry less context. Use it when a session has grown long, or start a fresh session for a new task (see Anthropic's docs for current command behavior).
  • Let prompt caching work. Claude Code caches repeated context such as system prompts and files automatically. Cached input is billed at the cache-read rate ($0.12 per 1M for Claude Sonnet 4.6), far below normal input.
  • Set a budget per key. A key stops at its budget, which caps the cost of any one machine, project or teammate.
  • Watch the Dashboard. Every request is listed with its model, tokens and cost, so you can see which sessions are expensive.

Using Codex CLI with the same key

The same APIVAI key works with OpenAI's Codex CLI, which uses the OpenAI format with /v1 in the base URL. For Codex 0.121 and later, put this in ~/.codex/config.toml (on Windows, %USERPROFILE%\.codex\config.toml):

model = "gpt-5.5"
model_provider = "apivai"

[model_providers.apivai]
name = "APIVAI"
base_url = "https://api.apivai.com/v1"
env_key = "OPENAI_API_KEY"
wire_api = "responses"

Then set the key and start Codex:

export OPENAI_API_KEY="your-apivai-key"
codex

The same idea works in Cursor; see the Cursor setup guide.

Troubleshooting

SymptomLikely causeFix
401 UnauthorizedWrong or deleted key, or the key has used its budgetCopy the key again from the Dashboard; raise the key's budget or top up
401 even with a correct keyClaude Code is sending a different credential: an old ANTHROPIC_API_KEY, an apiKeyHelper or a /login session (/status shows which one is active)Remove the leftover, for example: unset ANTHROPIC_API_KEY (bash/zsh), Remove-Item Env:ANTHROPIC_API_KEY (PowerShell), and remove it from profiles or settings
404 Not FoundBase URL contains /v1 or a typoUse exactly https://api.apivai.com for Claude Code; /v1 only for OpenAI-format tools such as Codex
429 Too Many RequestsMore than 60 requests per minute on one keyWait a moment, or ask support for a higher limit
Claude Code still uses the old accountVariables not loaded in this terminalOpen a new terminal; check with echo $ANTHROPIC_BASE_URL or echo $env:ANTHROPIC_BASE_URL
Timeouts or connection errorsA proxy, firewall or TLS inspection on your networkAllow api.apivai.com, or set your network's proxy in HTTPS_PROXY (see Anthropic's docs for Claude Code's proxy support)

If a problem persists, run the curl test from step 3; it separates key and network problems from Claude Code configuration. The docs list every endpoint and error.

All API error codes and their fixes are collected in Claude API error codes.

What does Claude Code cost per month?

Assuming about 4,000 input and 1,500 output tokens per message with Claude Sonnet 4.6, over 30 days, as of 2026-10-10:

Daily usageOfficial APIAPIVAI
Light (10 messages/day)$10.35$3.96
Medium (30 messages/day)$31.05$11.89
Heavy (100 messages/day)$104$39.63

These figures ignore prompt caching, which usually lowers the real bill further. Agent sessions on large repositories can send much more context per message, so check your Dashboard after the first few days. Current prices are on the pricing page; for a comparison with subscription tools, see Claude Code vs Cursor cost.

FAQ

Do I need an Anthropic account to use Claude Code this way?

No Anthropic API key is needed. Claude Code sends requests to the base URL you set and authenticates with your APIVAI key.

Is the model the same as on the official API?

Yes. Requests pass through to the Claude models you select with /model; only the address and the billing change.

Can I switch back to my Anthropic account?

Yes. Remove or comment out the two variables (or the env block in settings.json) and open a new terminal.

Does APIVAI see my code?

APIVAI does not store or inspect the content of requests or responses. It keeps only usage metadata (model, token counts, cost, time) for billing.

What happens when my balance or key budget runs out?

Requests return 401 until you top up or raise the key's budget. Nothing is charged beyond your prepaid balance.

Can one key be used for both Claude Code and Codex?

Yes. One key works with the Anthropic format (https://api.apivai.com) and the OpenAI formats (https://api.apivai.com/v1).

Ready to start?

Get your API key in 30 seconds. Pay as you go for Claude and GPT.

Get Started