Back to Blog

Claude Code Cheap API Router Setup (2026 Guide)

2026-04-21·10 min read·CodeRouter Team

Update, Nov 12, 2026: CodeRouter's pricing has changed since this post. It is now pay as you go — model list price with no markup from a prepaid balance, plus a 5.5% payment fee when you buy credit, no monthly fee, with smart routing or Direct mode (you name the exact model) on the same account — or Team at $19 per developer a month plus usage at list price. The monthly plans and the separate Direct plan described below are no longer sold; accounts already on them keep their terms. The rest of the post is as published. Current terms: pricing.

TL;DR — Claude Code always calls Anthropic's API with whatever model you've configured. Configure it with model: auto pointed at CodeRouter, and the router decides per call which model runs — starting each task on the cheapest tier that can handle it (Claude Haiku 5.5 or DeepSeek Chat, then Kimi K2.6, Claude Sonnet 5.5, Claude Opus 5.5) and moving up only when tests or errors show a failure. Same Claude Code UX, lower bill: in TokenWatt's 176-task coding benchmark (TokenWatt is a measurement project from the same team as CodeRouter), routing solved 96–100% of tasks for 56–70% less than sending every request to Opus, across runs.

Why Claude Code is expensive

Claude Code is purpose-built for long agentic coding sessions — Plan mode, long tool chains, file-read/write, Bash execution. It's probably the most efficient coding agent at output quality per engineered result.

The problem is cost per token:

A typical full Claude Code day with 3–5 planning sessions and 100+ implementation steps easily hits $30–80/day direct to Anthropic. That's $700–2400/month for one heavy user.

How routing reduces this

Claude Code's workflow maps well onto per-request routing. CodeRouter's routing (v3, since November 2026) works like this: a small judgment model (Jev) reads a compact excerpt of each request — the latest message and the latest tool output — and judges the coding phase, how hard the task is and whether the last turn failed; cost rules then pick a tier. Each task starts on the cheapest tier that can handle it — Claude Haiku 5.5 (or DeepSeek Chat), Kimi K2.6, Claude Sonnet 5.5 and Claude Opus 5.5 — and moves up one tier only when tests or errors show a failure.

Claude Code turn What the router sees Where it starts
Plan mode on a hard, open-ended task Hard planning Claude Opus 5.5
Plan mode on a moderate task Moderately hard planning Claude Sonnet 5.5
Edit / Write with implementation intent, writing tests Typical implement / test Kimi K2.6
Renames, formatting, writing a .md file Small edit / docs Claude Haiku 5.5 (or DeepSeek Chat)
Bash output shows a failing test or traceback The last turn failed One tier up (two failures needed at 30K+ tokens of context, because switching models loses the prompt cache)

A session stays on one model so the prompt cache stays warm (cache hits from turn 2 in Claude Code). No config needed beyond model: auto.

Setup — Claude Code proxy configuration

Two ways to configure Claude Code: env vars (quickest, single-session) or ~/.claude/settings.json (persistent across launches). Both shown below.

Critical: the env-var name trap

Before you copy any setup snippet — the env var that authenticates Claude Code against a third-party gateway is ANTHROPIC_AUTH_TOKEN, not ANTHROPIC_API_KEY.

If you set ANTHROPIC_API_KEY=cr_xxx and run Claude Code:

Why: ANTHROPIC_API_KEY is hard-wired to authenticate against api.anthropic.com. When ANTHROPIC_BASE_URL points elsewhere, only ANTHROPIC_AUTH_TOKEN (Bearer token mode) is honored.

Sanity check: after launching, look at the badge in Claude Code's header. If it doesn't say API Usage Billing, your env var didn't take effect. Run claude logout and try again.

Env vars (one-shot)

# Install Claude Code if you haven't already
$ npm install -g @anthropic-ai/claude-code@latest

# Point Claude Code at CodeRouter — note AUTH_TOKEN, not API_KEY
$ export ANTHROPIC_BASE_URL=https://www.coderouter.io
$ export ANTHROPIC_AUTH_TOKEN=cr_your_coderouter_key

# Force every model tier through smart routing. Without these, Claude Code's
# /model picker may try a hardcoded default that doesn't exist on our side.
$ export ANTHROPIC_MODEL=auto
$ export ANTHROPIC_DEFAULT_OPUS_MODEL=auto
$ export ANTHROPIC_DEFAULT_SONNET_MODEL=auto
$ export ANTHROPIC_DEFAULT_HAIKU_MODEL=auto

# v2.1.x had experimental beta features that some third-party gateways
# don't yet implement. Disabling them is harmless and avoids 400 errors.
$ export CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1

$ claude

Persistent — ~/.claude/settings.json

The persistent config lives at ~/.claude/settings.json (note: .claude/ in your home dir, not ~/.config/claude-code/). Format:

{
  "env": {
    "ANTHROPIC_BASE_URL": "https://www.coderouter.io",
    "ANTHROPIC_AUTH_TOKEN": "cr_your_coderouter_key",
    "ANTHROPIC_MODEL": "auto",
    "ANTHROPIC_DEFAULT_OPUS_MODEL": "auto",
    "ANTHROPIC_DEFAULT_SONNET_MODEL": "auto",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL": "auto",
    "CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS": "1"
  }
}

When you next run claude, it picks up this env block automatically — no shell-level export needed.

When Claude Code connects, it speaks Anthropic's native /v1/messages protocol. CodeRouter translates that into whichever provider the router chose (Anthropic for the Claude tiers, Moonshot for Kimi K2.6, DeepSeek for DeepSeek Chat) and translates the response back so Claude Code sees a consistent Anthropic-shaped reply regardless of which upstream actually served it.

Troubleshooting — the four errors you'll likely hit

These are the actual production failures we've seen from real testers in the past 48 hours. If you hit one, the fix is below.

"There's an issue with the selected model (auto). It may not exist..."

This is Claude Code's catch-all error for "model validation failed". Three independent causes can trigger it:

  1. Wrong env var name — see the AUTH_TOKEN section above. Switch from ANTHROPIC_API_KEY to ANTHROPIC_AUTH_TOKEN and confirm the header shows "API Usage Billing".

  2. Experimental betas (v2.1.x only) — Claude Code v2.1.x sends an anthropic-beta header for features that some gateways don't implement. Add:

    export CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1
    

    Or in settings.json, include "CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS": "1" in the env block.

  3. Older Claude Code without the auto model in /model picker — Claude Code v2.1.80 and earlier don't show a "Custom model" option in the /model slash command. Either pass it on the CLI:

    claude --model auto
    

    Or upgrade: npm install -g @anthropic-ai/claude-code@latest.

"Your 7-day free trial has ended" / "Your free trial's $5 usage credit is used up" (HTTP 429)

CodeRouter's free trial runs on CodeRouter's own provider keys: $1 of usage over 7 days with no card, $5 once you add a card (it isn't charged). When either runs out, requests return 429 until you pick an option:

  1. Upgrade to a paid plan — Starter $19/month includes $15 of model usage, Solo $29 includes $25, Pro $99 includes $85 (all at list price, Opus included); Team is $19 per developer plus usage at list +5%
  2. Add your own provider key at /dashboard/models — requests then run on your keys and the provider bills you directly
  3. (Internal teams only) Get whitelisted by your org's CodeRouter admin

Claude Code header still shows "Claude Max"

Existing OAuth session is winning over your env vars. Logout first:

claude logout
# then re-run with your env vars set

"Anthropic API error 400: Unable to download the file"

Caused by a base64-encoded image being forwarded to Anthropic as {type: "url"} instead of {type: "base64"}. Already fixed in CodeRouter — if you still hit this, the image MIME type may be unsupported (Anthropic only accepts image/png, image/jpeg, image/gif, image/webp).

Verifying routing is active

After your first Claude Code session:

  1. Open the CodeRouter dashboard → Analytics.
  2. Look at the Phase Distribution chart. You should see a mix — if 100% of requests show as one phase (e.g. all "implement"), detection isn't picking up your agent's patterns and you should check your Claude Code version.
  3. Check the Models Used chart. A healthy session mixes Claude Haiku 5.5 / DeepSeek Chat, Kimi K2.6 and Claude Sonnet 5.5, with Claude Opus 5.5 for hard planning and repeated failures. All-Opus means something is wrong — report it to us. You can also check per request: the X-CodeRouter-Model and X-CodeRouter-Tier response headers show what was picked.
  4. Weekly Savings card shows an estimate of what Opus-for-everything would have cost.

Real-world cost example

Two measured reference points instead of one anecdote:

Your number depends on how much of your work is planning, implementation, debugging and small edits, and on your cache hit rate. The dashboard shows your cost per request and per model.

FAQ

Will Plan mode still work? Yes. Hard planning starts on Claude Opus 5.5 and moderately hard planning on Claude Sonnet 5.5. What changes is the post-plan implementation calls that Claude Code makes by the dozen — those start on cheaper tiers and move up only if they fail.

What about Claude Code's native MCP tool ecosystem? Unaffected. MCP servers are local; only the LLM calls route through CodeRouter.

Does routing to Kimi or DeepSeek break Claude Code's tool-call format? No — CodeRouter translates Anthropic's tool_use blocks into OpenAI's tool_calls format for non-Anthropic providers and back. Claude Code sees a consistent Anthropic-shaped response regardless of which upstream served it.

What about Claude Code on Bedrock / Vertex? If you're on Bedrock, keep your existing setup — no CodeRouter needed since Bedrock pricing differs. We integrate with native Anthropic API, not with cloud-provider resellers.

Related

Try phase-aware routing on your own workload

One base URL for Claude Code, Cursor or Aider. Free trial, no credit card.

Start free trial

Get weekly AI cost optimization tips

Join developers cutting their agent bills