How to Use Claude Code with OpenRouter
OpenRouter ·
On this page
You’re deep into a refactor. Claude Code has pulled context across a dozen files, planned the diff, and started executing. Then the session stops on a rate limit, 70% of the way through.
Routing Claude Code through OpenRouter is how you keep that session alive. OpenRouter sits between Claude Code and Anthropic’s API as a reliability and management layer. It adds provider failover, budget controls, and usage visibility, and it does that with no local proxy to run. The setup is three environment variables. This covers that setup, model routing, Fast Mode, and the cost math.
The team case is just as practical. Several developers running Claude Code at once means several Anthropic accounts, no shared spend view, and no per-developer cap until the invoice lands. One OpenRouter key fronts all of it: shared billing, per-key limits, and an Activity dashboard that shows cost per session.
Connect Claude Code in three steps
The whole setup is three environment variables, documented in the Claude Code integration guide, with no proxy, no Docker, and no local port to run.
# Add to ~/.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="" # must be explicitly empty
Three things to get right. The base URL is https://openrouter.ai/api. The auth token is your OpenRouter key, which starts with sk-or-. And ANTHROPIC_API_KEY has to be an empty string, not unset, or Claude Code can fall back to authenticating against Anthropic directly.
Prefer to scope it to one project? Put the same three values under an env block in .claude/settings.local.json at the project root. Don’t use a plain .env file, since the native installer doesn’t read one.
If you logged in to Claude Code with an Anthropic account before, run /logout once and relaunch, otherwise the cached login overrides your variables and you get confusing model-not-found errors. Confirm the switch with /status:
> /status
Auth token: ANTHROPIC_AUTH_TOKEN
Anthropic base URL: https://openrouter.ai/api
Your request should also show up in the Activity dashboard within seconds.
How the Anthropic Skin works without a proxy
OpenRouter exposes an endpoint compatible with the Anthropic Messages API, which it calls the Anthropic Skin. Claude Code speaks its native protocol straight to OpenRouter, and the Skin handles model mapping and passes advanced features through untouched.
That means Thinking blocks, native tool use, streaming, and multi-turn context all work as they do against Anthropic directly. Older setups leaned on a local proxy like claude-code-router to translate requests, which meant a Node.js runtime, a local port, and a translation layer to keep healthy. Native routing drops that whole tier for Anthropic workflows.
Underneath, OpenRouter load-balances across the Anthropic providers serving a model. If the one it tries first is rate-limiting you, it routes the same model to another provider that serves it, and you pay only for the call that lands. For a multi-step agentic task that holds state across many calls, that failover happening below Claude Code is the difference between a finished task and a half-applied edit.
Route each task class to the right model
Claude Code splits work across model slots. Override each one to point at a specific model on OpenRouter. The ~author/model-latest aliases always resolve to the newest version in a family, so they don’t go stale:
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"
A reasonable split: Opus for architecture and deep reasoning, Sonnet for everyday coding, Haiku for quick transformations and classification. Put these in the same shell profile or project settings file as the base URL and token.
Keep the models Anthropic, though. Claude Code is built around Anthropic request semantics, and the integration is only guaranteed to work with the Anthropic first-party provider. For maximum compatibility, set Anthropic 1P as your top-priority provider.
Fast Mode for Opus sessions
Anthropic’s Fast Mode delivers up to 2.5x faster output at premium pricing, served only by the Anthropic first-party provider. It applies to Claude Opus 4.6, 4.7, and 4.8, and no other models.
Claude Code has a built-in /fast toggle. When it’s on, Claude Code sends speed: "fast" alongside the configured Opus model, and OpenRouter reroutes to the matching -fast variant. To use it, set one variable:
export CLAUDE_CODE_SKIP_FAST_MODE_ORG_CHECK=1
This needs Claude Code v2.1.96 or newer. Send speed: "fast" to a model that doesn’t support it and OpenRouter just drops the parameter, so the request runs at standard speed and price. Reach for it on interactive sessions and live debugging, where latency is the thing you feel. Check the pricing page for current Opus rates before turning it on for production.
What it costs, and the free tier
OpenRouter doesn’t mark up token pricing. You pay the same per-token rate the provider charges, shown in the model catalog, and credit purchases carry a 5.5% fee with a $0.80 minimum.
The fee is small against real usage. Take 10M tokens a month on Claude Sonnet 4.5 at an 80/20 input/output split. At $3 per million input and $15 per million output, that’s about $54 in direct token cost, and the 5.5% credit fee adds roughly $3. For that you get failover, per-key budget caps, and one billing view across the team.
There’s also a free tier for trying things. Free models run at up to 50 requests a day, rising to 1,000 a day once you add $10 in credits. They carry smaller context windows than paid Anthropic models, so they’re good for learning Claude Code’s workflow, not for production sessions. Watch spend live in the Activity dashboard, the fastest way to catch a session routed to a heavier model than the task needs.
Wire it into CI and your terminal
The same routing works beyond your local shell.
For CI, the official Claude Code GitHub Action needs two changes: pass your OpenRouter key through anthropic_api_key, and set the base URL in the step’s env.
- name: Run Claude Code
uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: ${{ secrets.OPENROUTER_API_KEY }}
env:
ANTHROPIC_BASE_URL: https://openrouter.ai/api
For live cost in your terminal, the openrouter-examples repository ships a statusline script that shows provider, model, running cost, and cache discount. Point your ~/.claude/settings.json at it:
{
"statusLine": {
"type": "command",
"command": "/path/to/statusline.sh"
}
}
Building agents on top of Claude Code? The Anthropic Agent SDK uses the Claude Code runtime, so the same three environment variables route its requests through OpenRouter with no extra config.
Frequently asked questions
Do I need an Anthropic subscription to use Claude Code with OpenRouter?
No. Requests route through your OpenRouter credits, so you need an OpenRouter account and an API key, not an Anthropic plan. If you previously logged in to Claude Code with an Anthropic account, run /logout once to clear the cached session before switching.
Can I use non-Anthropic models with Claude Code through OpenRouter?
The native integration is built for Anthropic models and is only guaranteed to work with the Anthropic first-party provider. Claude Code expects Anthropic request semantics, so non-Anthropic models aren’t supported through the native endpoint.
How much does Claude Code cost through OpenRouter?
You pay the provider’s per-token rate plus a 5.5% fee on credit purchases, with a $0.80 minimum. Inference itself stays at provider rates. The Activity dashboard shows exact per-session cost in real time.
Does OpenRouter log my source code?
No. By default OpenRouter retains only metadata like token counts, not your prompts or completions. Logging your own inputs and outputs is opt-in, and a separate opt-in lets OpenRouter use your data to improve the product for a 1% usage discount. See the Privacy Policy for the terms.
What is Fast Mode and which models support it?
Fast Mode runs up to 2.5x faster output at premium pricing, served by the Anthropic first-party provider. It’s available only on Claude Opus 4.6, 4.7, and 4.8. Toggle it with /fast inside Claude Code.
What happens if Anthropic’s API is rate-limiting me?
OpenRouter fails over to another Anthropic provider serving the same model, so the session keeps running without reconnecting. You pay the rate for whichever provider actually served the request.