Set ANTHROPIC_BASE_URL to https://openrouter.ai/api, ANTHROPIC_AUTH_TOKEN to your OpenRouter key, and ANTHROPIC_API_KEY to an empty string, in your shell profile or .claude/settings.local.json. Verify with /status, which should read "Anthropic base URL: https://openrouter.ai/api". Pin models with ANTHROPIC_DEFAULT_SONNET_MODEL and friends. The known breakages: an unset (rather than empty) ANTHROPIC_API_KEY falls back to Anthropic, variable ordering can blank the token, Fable is not offered in /model unless you pin it, and fast mode reports on while sending nothing if you use a ~latest alias.
ANTHROPIC_BASE_URL="https://openrouter.ai/api", with no/v1.ANTHROPIC_API_KEY=""must be explicitly empty, not unset, or Claude Code falls back to Anthropic.- Define
OPENROUTER_API_KEYbefore the line that expands it, or the token is empty and every request fails. - Check with
/status: it must showAuth token: ANTHROPIC_AUTH_TOKEN. - Fable is not in
/modelunless you setANTHROPIC_DEFAULT_FABLE_MODEL. - Fast mode with a
~latestalias says "Fast mode ON" and silently does nothing. Pin a version.
The setup
Put this in your shell profile, not a .env file. OpenRouter's own integration docs are explicit about that, and about running /logout first if you previously signed in with an Anthropic account.
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=""
export CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1
Get a key and put credits on it
From openrouter.ai. Remember the top-up fee is 5.5% with a $0.80 minimum, so a $5 test top-up is a 16% rail. See OpenRouter pricing.
Sign Claude Code out of Anthropic
claude
> /logout
A live Anthropic session takes precedence over the environment in some configurations. Clear it before you debug anything else.
Export the variables and start a new shell
Environment changes do not reach an already-running Claude Code session. Open a new terminal.
Verify with /status
> /status
Auth token: ANTHROPIC_AUTH_TOKEN
Anthropic base URL: https://openrouter.ai/api
If Auth token reads ANTHROPIC_API_KEY instead, the variables did not reach the session and you are still billing Anthropic.
Pin your models
export ANTHROPIC_DEFAULT_FABLE_MODEL="~anthropic/claude-fable-latest"
export ANTHROPIC_DEFAULT_OPUS_MODEL="~anthropic/claude-opus-latest"
export ANTHROPIC_DEFAULT_SONNET_MODEL="~anthropic/claude-sonnet-latest"
export ANTHROPIC_DEFAULT_HAIKU_MODEL="~anthropic/claude-haiku-latest"
export CLAUDE_CODE_SUBAGENT_MODEL="~anthropic/claude-opus-latest"
The Haiku variable is the one people forget. It is the small, fast model Claude Code uses for background work such as summarizing and file triage, and if it is unset or pointed at something expensive you will notice it on the bill before you notice it in the transcript. Which Claude id belongs in each slot changes with every release, and the Anthropic model hub lists the current ones with a card each.
What breaks
| Symptom | Cause | Fix |
|---|---|---|
| Requests succeed but bill Anthropic | ANTHROPIC_API_KEY unset rather than empty | Set it to "" explicitly |
| Every request 401s | OPENROUTER_API_KEY exported after the line using it | Move the export above |
/status shows the wrong auth token | Env did not reach the session | New shell, then /logout |
Fable missing from /model | Not offered by default through a gateway | Set ANTHROPIC_DEFAULT_FABLE_MODEL |
/fast says ON but costs the same | ~latest alias drops the speed parameter | Pin a version, e.g. anthropic/claude-opus-5 |
/model list looks short | Gateway picker shows a curated set | Set the model env vars directly |
| Tool calls behave oddly on a non-Claude model | Claude Code is tuned for Anthropic models | Use Claude models, or accept the variance |
The fast-mode trap
The compatibility caveat, in their words
OpenRouter's own documentation says "Claude Code is optimized for Anthropic models and may not work correctly with other providers." That is worth taking literally. Claude Code's system prompt, tool schemas, and agentic loop are built against Claude, and pointing it at a cheaper open-weight model through the same wire produces something that runs and frequently does not behave. Cost per token is not cost per completed task.
The alternatives to doing it this way
claude-code-router
A local control plane (npm install -g @musistudio/claude-code-router, then ccr ui at http://127.0.0.1:3458, 36.7k stars, Node 22+) that sits between Claude Code and many providers: OpenAI, Anthropic, Gemini, OpenRouter, DeepSeek, SiliconFlow, Moonshot, Kimi Code, Mistral, Z.AI, Bailian, and custom endpoints. Worth it if you want per-request routing rules rather than one endpoint for everything. It is a second process to run, and its configuration lives in a local SQLite database managed through the UI. See the router guide.
A LiteLLM proxy in front
If you already run LiteLLM, Claude Code points at it directly. Two documented shapes:
# unified endpoint
export ANTHROPIC_BASE_URL="http://0.0.0.0:4000"
export ANTHROPIC_AUTH_TOKEN="<litellm-virtual-key>"
# or the Anthropic pass-through
export ANTHROPIC_BASE_URL="http://0.0.0.0:4000/anthropic"
This is the right answer for an organization: every developer gets a virtual key with a budget, spend is tracked centrally, and prompts never leave the network. It is the wrong answer for one person, because you now operate a proxy.
Requesty
The same three-variable shape against https://router.requesty.ai, with a flat 5% on inference and an EU endpoint at router.eu.requesty.ai. Detail in the Requesty review.
When a gateway is the wrong tool here
Step back from the mechanics for a second, because most people arriving at this page are solving a cost problem, not an integration problem. Routing Claude Code through OpenRouter converts a subscription into metered API billing plus 5.5%. That is a good trade if you were on API billing already and want model variety. It is usually a bad trade if you were on a Claude subscription, because you have just left a flat-rate plan for a meter.
Questions people ask
How do I use OpenRouter in Claude Code?
Export ANTHROPIC_BASE_URL="https://openrouter.ai/api", ANTHROPIC_AUTH_TOKEN set to your OpenRouter key, and ANTHROPIC_API_KEY="" as an explicitly empty string, in your shell profile or .claude/settings.local.json. Start a new shell, run /logout if you were signed in to Anthropic, and confirm with /status that it reports the OpenRouter base URL.
Why does Claude Code still use my Anthropic account after setting the base URL?
Almost always because ANTHROPIC_API_KEY is unset rather than set to an empty string. OpenRouter's docs are explicit that it must be explicitly empty, or Claude Code can fall back to authenticating against Anthropic directly. The other cause is a live login session: run /logout first. Check with /status, which should show "Auth token: ANTHROPIC_AUTH_TOKEN".
What base URL does Claude Code need for OpenRouter?
https://openrouter.ai/api, without the /v1 suffix. The /v1 path is for the OpenAI-compatible API; Claude Code speaks the Anthropic wire, which OpenRouter serves from the bare /api path.
Why is Fable missing from /model in Claude Code with OpenRouter?
It is not offered in the picker by default when Claude Code points at OpenRouter. Setting ANTHROPIC_DEFAULT_FABLE_MODEL is the only way to make it selectable. The gateway model picker also shows a curated list rather than the full catalogue, so setting the model environment variables directly is more reliable than browsing /model.
Does /fast work through OpenRouter?
Only if you pin an explicit model version. Fast mode requires CLAUDE_CODE_SKIP_FAST_MODE_ORG_CHECK=1 and is limited to Claude Opus 4.6, 4.7, 4.8, and Opus 5. With a ~latest alias, /fast reports "Fast mode ON" but the request goes without the speed parameter, so you get standard speed at standard pricing. Pin something like anthropic/claude-opus-5.
Is running Claude Code through OpenRouter cheaper?
Not on the same tokens. OpenRouter passes provider list prices through and adds 5.5% on credit purchases, so it is about 5.5% more than paying Anthropic directly, and considerably more than a flat Claude subscription if you were already on one. It gets cheaper only if you switch to a cheaper model, which is also where tool-calling reliability starts to vary.
Can I use a non-Anthropic model in Claude Code through OpenRouter?
Technically yes: any model OpenRouter serves can be named in the model environment variables. OpenRouter itself warns that "Claude Code is optimized for Anthropic models and may not work correctly with other providers." Expect variance in tool calling and agentic loops, and judge on completed tasks rather than on price per million tokens.
Sources
Every figure above was read from these pages on August 2026. Vendors reprice without notice; if you find a stale number, tell us.
- OpenRouter Claude Code integration every env var, /status output, fast-mode and Fable caveats
- OpenRouter FAQ 5.5% credit fee, no inference markup
- OpenRouter quickstart base URL and OpenAI SDK compatibility
- LiteLLM Claude Code tutorial unified endpoint and /anthropic pass-through env vars
- claude-code-router on GitHub install command, provider list, UI port, star count
- Requesty Claude Code integration alternative Anthropic-wire endpoint