How to use OpenRouter, or any API provider, with Claude Code
Updated 2026-11-19
Model page: Claude Opus 5.5 API — pricing, specs and the playground
People route Claude Code through another provider for three reasons: to pay per token instead of hitting subscription limits, to use one bill and key across many models, or because Anthropic is not directly available to them. Anthropic documents this as connecting Claude Code to an LLM gateway, and the rules are the same whether the gateway is OpenRouter, APIMODELS, a company proxy or the local claude-code-router.
Everything below is taken from the Claude Code environment-variable and gateway docs, OpenRouter's own Claude Code guide and the claude-code-router README, checked on 19 November 2026.
The variables that matter
Put these in the env block of ~/.claude/settings.json so they apply no matter how claude is started; if a shell export and the settings file disagree, the settings file wins. Do not put a credential in a project's .claude/settings.json. If you were logged in with an Anthropic account before, run /logout once so the cached session does not take over.
| Variable | What it does |
|---|---|
| ANTHROPIC_BASE_URL | Where requests go. Use the provider's base URL without /v1; Claude Code adds /v1/messages itself. |
| ANTHROPIC_AUTH_TOKEN | Sent as Authorization: Bearer <token>. Anthropic's docs say to use this one if your provider did not tell you which. |
| ANTHROPIC_API_KEY | Sent as x-api-key. If set, it overrides your Claude subscription after you approve it once. |
| ANTHROPIC_MODEL | The model Claude Code uses, for example claude-opus-5-5. |
| ANTHROPIC_DEFAULT_SONNET_MODEL / _OPUS_MODEL / _HAIKU_MODEL | What the sonnet, opus and haiku aliases resolve to; haiku is also used for background work. |
| API_TIMEOUT_MS | How long each attempt waits for response headers (default 600000). Raise it when routing through a proxy. |
| CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS | Set it when a gateway rejects the anthropic-beta header. |
Claude Code with OpenRouter
OpenRouter's own guide sets ANTHROPIC_BASE_URL to https://openrouter.ai/api, ANTHROPIC_AUTH_TOKEN to your OpenRouter key, and ANTHROPIC_API_KEY explicitly to an empty string. It also notes that Claude Code with OpenRouter is only guaranteed to work with the Anthropic first-party provider, and that Claude Code is optimised for Anthropic models and may not work correctly with others. After setting it up, /status should show the auth token and the OpenRouter base URL.
Claude Code with claude-code-router
claude-code-router (CCR) is an open-source local gateway that sits between Claude Code and your providers and can send different kinds of work to different models. In the current version 3, you install it with npm install -g @musistudio/claude-code-router (Node.js 22 or newer), open the management UI with ccr ui, add a provider under Providers, and pick Claude Code under Agent Config. The local gateway listens on http://127.0.0.1:3456 and settings are stored in ~/.claude-code-router/config.sqlite.
Older guides show a ~/.claude-code-router/config.json with a Providers list (name, api_base_url, api_key, models, transformer) and a Router block (default, background, think, longContext) started with ccr code. That is version 2. In version 3 a config.json is only read once to migrate, and editing it afterwards has no effect. You do not need CCR to use Claude models through APIMODELS, since Claude Code connects to it directly; CCR is useful when you want to mix providers or send different kinds of work to different models.
Claude Code with APIMODELS
APIMODELS serves Claude on the native Anthropic Messages API, so Claude Code needs only the two variables: ANTHROPIC_BASE_URL set to https://api.apimodels.app and ANTHROPIC_AUTH_TOKEN set to your APIMODELS key, plus ANTHROPIC_MODEL if you want a specific model. Tool use, thinking blocks and streaming events keep their native shape. Long Claude streams get an Anthropic ping every 15 seconds while the model is thinking, which Claude Code's idle watchdog counts as activity, and every current Claude model has at least two upstream routes. CC Switch can import the settings in one click.
| Model | Anthropic list (input / output per 1M tokens) | APIMODELS (input / output per 1M tokens) | Call name |
|---|---|---|---|
| Claude Fable 5.2 | $8 / $40 | $4.80 / $24 | claude-fable-5-2 |
| Claude Opus 5.5 | $4 / $20 | $2.40 / $12 | claude-opus-5-5 |
| Claude Sonnet 5.5 | $2 / $10 | $1.20 / $6 | claude-sonnet-5-5 |
| Claude Haiku 5.5 (prompts up to 100K tokens) | $0.10 / $0.50 | $0.09 / $0.45 | claude-haiku-5-5 |
What you give up with any third-party endpoint
Subscription-only features: cloud sessions, Claude Code in Slack, Desktop, /schedule, Code Review, Ultrareview, the Chrome extension, computer use, artifacts and voice dictation need a claude.ai login.
Remote Control and server-managed settings: Claude Code turns these off whenever ANTHROPIC_BASE_URL points somewhere other than api.anthropic.com.
Fast mode: its availability check goes straight to Anthropic, so with only ANTHROPIC_AUTH_TOKEN set Claude Code treats fast mode as off.
Fine-grained tool streaming is off by default through a custom base URL.
Non-Claude models: Anthropic does not support routing Claude Code to non-Claude models through any gateway. It may work, but you are on your own.
Subscription limits stop applying: requests carry your provider's credential instead of your claude.ai login, so they are billed by that provider and your plan's 5-hour and weekly limits do not cover them.
If it does not work
Run /status to see the base URL and credential source Claude Code is really using.
ANTHROPIC_API_KEY is set but ignored, with no prompt: enable it under /config with the Use custom API key option.
401 behind a gateway: the text after 401 is the gateway's message, not Anthropic's. Check the key with that provider.
Errors about the anthropic-beta header or "Extra inputs are not permitted": set CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1.
Claude Code stalls on every reply: the gateway is probably buffering whole responses instead of streaming them; Anthropic's docs say Claude Code stalls in that case.
Quick start
~/.claude/settings.json (APIMODELS)
{
"env": {
"ANTHROPIC_BASE_URL": "https://api.apimodels.app",
"ANTHROPIC_AUTH_TOKEN": "sk_...your_apimodels_key...",
"ANTHROPIC_MODEL": "claude-opus-5-5"
}
}Shell (OpenRouter, from OpenRouter's guide)
export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
export ANTHROPIC_API_KEY="" # must be explicitly emptycURL
# Test the endpoint Claude Code will use, before you start claude
curl "$ANTHROPIC_BASE_URL/v1/messages" \
-H "Authorization: Bearer $ANTHROPIC_AUTH_TOKEN" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{"model": "claude-opus-5-5", "max_tokens": 64, "messages": [{"role": "user", "content": "ping"}]}'FAQ
How do I use OpenRouter in Claude Code?
Set ANTHROPIC_BASE_URL to https://openrouter.ai/api, ANTHROPIC_AUTH_TOKEN to your OpenRouter key and ANTHROPIC_API_KEY to an empty string, then run /logout once if you were logged in with Anthropic and check /status. OpenRouter says this is only guaranteed with the Anthropic first-party provider.
Can Claude Code use other models?
Technically you can point it at a gateway that serves other models, but Anthropic does not support routing Claude Code to non-Claude models and OpenRouter warns it may not work correctly. The supported path is a different provider for the same Claude models, chosen with ANTHROPIC_MODEL or the ANTHROPIC_DEFAULT_*_MODEL variables.
What is claude-code-router?
An open-source local gateway for coding agents that sits between Claude Code and your providers and can route different tasks to different models. Version 3 is installed with npm install -g @musistudio/claude-code-router, configured in a web UI (ccr ui) and listens on 127.0.0.1:3456; the config.json and ccr code workflow belong to version 2.
How do I use Claude Code with an API key instead of a subscription?
Set ANTHROPIC_API_KEY (Anthropic keys, sent as x-api-key) or ANTHROPIC_BASE_URL plus ANTHROPIC_AUTH_TOKEN (a provider such as APIMODELS). Approve the key when Claude Code asks, then run /status. From then on requests are billed per token and your subscription's usage limits do not apply to them.
Do I lose features when Claude Code uses a third-party API?
Yes, some. Remote Control and server-managed settings turn off whenever the base URL is not api.anthropic.com, subscription-only features such as cloud sessions, Slack, Desktop and Code Review need a claude.ai login, fast mode is treated as off with only an auth token set, and fine-grained tool streaming is off by default.
