Back to Blog

How to Use a Model Router with Claude Code: OpenRouter (Including Auto Code) and CodeRouter, Step by Step

2026-11-09·9 min read·CodeRouter Team

Disclosure: CodeRouter is our product. The OpenRouter steps below are taken from OpenRouter's own Claude Code guide, Auto Code docs and prompt caching docs, checked on November 9, 2026. If OpenRouter's pages say something different when you read this, follow them.

Claude Code talks to the Anthropic Messages API. Any router that speaks that API can sit in the middle: you change where Claude Code sends requests and which key it sends, and the router picks (or passes through) the model. Two hosted options are below. Pick one; don't set both.

Before you start: AUTH_TOKEN, not API_KEY

Both setups put the router's key in ANTHROPIC_AUTH_TOKEN, which Claude Code sends as a bearer token. ANTHROPIC_API_KEY is a different variable, and putting the router's key there is a common setup mistake. For OpenRouter, the guide also says to "explicitly blank out ANTHROPIC_API_KEY to prevent conflicts": if it holds a real Anthropic key, Claude Code "sends it as x-api-key and may authenticate against Anthropic instead".

If you were ever logged in to Claude Code with an Anthropic account, run /logout once inside Claude Code, quit, and start claude again.

Option 1: Claude Code with OpenRouter

Step 1: set the environment variables

From OpenRouter's guide, in your shell profile (~/.zshrc or ~/.bashrc):

export OPENROUTER_API_KEY="<your-openrouter-api-key>"
export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
export ANTHROPIC_API_KEY="" # Important: Must be explicitly empty

Then source ~/.zshrc or open a new terminal. Two details from the guide:

The guide also offers a per-project alternative: the same values in the env block of .claude/settings.local.json. Keep that file out of version control if it holds your key.

Step 2: start Claude Code and check

cd /path/to/your/project
claude

Inside Claude Code, run /status. OpenRouter's guide says you should see:

Auth token: ANTHROPIC_AUTH_TOKEN
Anthropic base URL: https://openrouter.ai/api

If the auth line names ANTHROPIC_API_KEY, or a login method names a Claude account, the environment didn't reach the session: reload your shell and relaunch. Requests also show up in OpenRouter's Activity dashboard.

Step 3 (optional): let Auto Code pick the model

By default this setup sends Claude Code's usual Claude models through OpenRouter. To have OpenRouter choose the model per turn instead, use the Auto Code model ID. OpenRouter's Auto Code docs say to "set model to openrouter/auto-code wherever your harness takes a model string", and its Claude Code guide lists the variables where Claude Code takes model strings:

export ANTHROPIC_DEFAULT_OPUS_MODEL="openrouter/auto-code"
export ANTHROPIC_DEFAULT_SONNET_MODEL="openrouter/auto-code"
export ANTHROPIC_DEFAULT_HAIKU_MODEL="openrouter/auto-code"
export CLAUDE_CODE_SUBAGENT_MODEL="openrouter/auto-code"

Set only the ones you want routed; for example, leave Haiku-class quick tasks on a fixed model. After restarting, open /model to confirm what's listed, and check the model column in the Activity dashboard to see which models actually answered.

Two things to know before you do this:

Cost: Auto Code adds no fee; you pay the standard price of the model that serves each turn, billed from your OpenRouter credits, plus OpenRouter's platform fee on credit purchases. Details: OpenRouter pricing.

Option 2: Claude Code with CodeRouter

CodeRouter is our router for coding agents. The model name auto means "route this request"; CodeRouter picks one of four tiers (Claude Haiku 5.5, Kimi K2.6, Claude Sonnet 5.5, Claude Opus 5.5), and a Claude Code session stays on one model unless a code change fails its tests or the agent stops making progress.

Step 1: get a key and run the setup script

Create a key in the dashboard (it starts with cr_), then:

curl -fsSL https://www.coderouter.io/setup.sh | bash -s -- cr_YOUR_KEY

The script checks the key, backs up ~/.claude/settings.json, and merges these into its env block, leaving everything else alone:

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

The base URL has no /v1: Claude Code adds /v1/messages itself. To undo it, run the same command with --uninstall instead of the key; it removes only these keys and restores what they replaced. If you'd rather not run a script, paste the block above yourself, or try one session without changing any file:

ANTHROPIC_BASE_URL=https://www.coderouter.io ANTHROPIC_AUTH_TOKEN=cr_YOUR_KEY ANTHROPIC_MODEL=auto claude

Step 2: check it

Run /status in Claude Code and look for the CodeRouter base URL. Each response carries headers naming the model, tier and cost (X-CodeRouter-Model, X-CodeRouter-Tier, X-CodeRouter-Cost), and usage appears in your dashboard. Our full Claude Code guide covers the error messages you might meet.

Want to name the model yourself?

On a pay-as-you-go account, switch on Direct mode in the dashboard: the model named in each request answers, with no routing and no fallback, at the same list price +8%. See Direct mode.

Troubleshooting

Symptom Likely cause Fix
Model-not-found errors for openrouter/auto or other OpenRouter-only models A credential conflict, per OpenRouter's guide Run /logout, relaunch; make sure ANTHROPIC_API_KEY="" is set in your shell profile, then reload the shell
Auth errors right after setup ANTHROPIC_AUTH_TOKEN expanded to an empty string, or the shell wasn't reloaded Define the key before using it; source the profile or open a new terminal
/status names ANTHROPIC_API_KEY or a Claude account The environment didn't reach the session Relaunch claude from a shell with the new variables
Context window shows 200K for a 1M model (OpenRouter) Claude Code assumes 200K unless the model name ends in [1m] Per OpenRouter's guide, append [1m] to the model slug where the model supports it
404 on every request with Auto Code Your allowed or excluded lists leave no eligible model Loosen the lists on OpenRouter's Routing page
HTTP 402 from CodeRouter Prepaid balance is $0 Top up in the dashboard; the response links there

Keeping the prompt cache warm

Claude Code resends the whole conversation on every turn, so most of a session's input can come from the prompt cache. On Anthropic models, OpenRouter's caching docs list cache reads at 0.1× the input price and cache writes at 1.25× (5-minute TTL) or 2× (1-hour TTL). In our own team's coding-agent traffic, over 90% of spend was input tokens (published here), so the cache matters more than the per-token price.

What helps, by option:

To check whether any router is costing you cache, compare cached input per session before and after you switch. That and five other checks are in What to measure before you pick a coding router.

What about LiteLLM?

LiteLLM is an open-source (MIT) gateway you host yourself, with virtual keys, budgets and teams; you pay model providers directly. It's the right shape if prompts must stay in your infrastructure or you already have provider contracts. Its setup for Claude Code is in LiteLLM's own documentation; we haven't reproduced it here. LiteLLM can also call CodeRouter as an OpenAI-compatible model named auto. Comparison: CodeRouter vs LiteLLM.

FAQ

Can I use OpenRouter in Claude Code? Yes. Set ANTHROPIC_BASE_URL=https://openrouter.ai/api, put your OpenRouter key in ANTHROPIC_AUTH_TOKEN, and set ANTHROPIC_API_KEY="". OpenRouter guarantees compatibility only with the Anthropic first-party provider.

Can I use OpenRouter Auto Code in Claude Code? Auto Code's docs say to use openrouter/auto-code wherever your harness takes a model string. In Claude Code, those are the model variables such as ANTHROPIC_DEFAULT_SONNET_MODEL. Auto Code can pick non-Anthropic models unless you restrict it on OpenRouter's Routing page.

Why do I get "model not found" for openrouter/auto? OpenRouter's guide says it's usually a credential conflict: a cached Anthropic login (fix with /logout) or a real ANTHROPIC_API_KEY still in your shell profile.

Do I need a local proxy? No, for either option: Claude Code sends requests straight to the router's URL.

How do I go back to Anthropic directly? Remove the variables (for CodeRouter's script, run it with --uninstall), restart Claude Code, and log in again.

How is CodeRouter different from OpenRouter for this? See CodeRouter vs OpenRouter and our explainer of how Auto Code works.

Sources, checked November 9, 2026: OpenRouter Claude Code guide, Auto Code docs, Auto Router docs, OpenRouter prompt caching, OpenRouter pricing.

Related reading

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