Claude Code with OpenRouter
VerifiedConnect Claude Code to OpenRouter's official Anthropic-compatible endpoint. One key gives you access to hundreds of models with provider failover.
Last verified 2026-07-30 against official provider documentation.
About Claude Code
Claude Code is an agentic coding tool that runs in your terminal. It can be pointed at any Anthropic-compatible API endpoint through environment variables.
About OpenRouter
OpenRouter is a unified API gateway to hundreds of models from many providers. One key unlocks every model slug, with provider failover and a single billing dashboard.
Key points
- OpenRouter officially documents the Anthropic-compatible endpoint at https://openrouter.ai/api.
- ANTHROPIC_API_KEY must be set to an explicitly empty string — otherwise Claude Code authenticates against Anthropic directly.
- If you previously logged in with an Anthropic account, run /logout inside Claude Code once, or the cached login overrides your variables.
- Model aliases starting with ~ always resolve to the newest version of that model on OpenRouter.
Set up Claude Code with OpenRouter
Choose your operating system, shell and model. The steps below update instantly and are generated locally in your browser.
Anthropic models are recommended: the integration is guaranteed for Anthropic's first-party provider. Set it as your top-priority provider on OpenRouter.
Browse the full OpenRouter model catalogGet an API key
Create a OpenRouter API key. Claude Code authenticates with it — it never leaves your machine.
Get a OpenRouter API key- Install Claude Code: npm install -g @anthropic-ai/claude-code
- Sign in, open Keys, and create a key that starts with sk-or-. Free-tier requests are available; adding $10 in credits raises your daily free limit.
Configure the connection
Set these environment variables in your shell profile.
- 1Add these lines to ~/.zshrc
- 2Run: source ~/.zshrc
export OPENROUTER_API_KEY="sk-or-"
export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
export ANTHROPIC_API_KEY=""
# ANTHROPIC_API_KEY must be explicitly empty, not unset
export ANTHROPIC_DEFAULT_OPUS_MODEL="~anthropic/claude-sonnet-latest"
export ANTHROPIC_DEFAULT_SONNET_MODEL="~anthropic/claude-sonnet-latest"
export ANTHROPIC_DEFAULT_HAIKU_MODEL="~anthropic/claude-sonnet-latest"
export CLAUDE_CODE_SUBAGENT_MODEL="~anthropic/claude-sonnet-latest"Paste your API key where the placeholder is. Then open a new terminal window.
Verify the configuration
Run this command in a new terminal. A successful response confirms that Claude Code is talking to the configured endpoint.
claudeAfter Claude Code opens, type `/status` inside the session. The exact labels vary by version; confirm the auth token and base URL shown there.
Start using it
Launch Claude Code and send your first message.
- Type: Hello. What can you help me with today?
claudeTroubleshooting
Most issues fall into one of these categories. Each entry lists the cause, the fix, and how to verify it.
401 Unauthorized
Troubleshooting guideThe API key is wrong, expired, or the provider account has no balance.
Create a fresh key on the provider dashboard, then regenerate the config with the new key. If the key is stored in a shell profile, open a new terminal so the export takes effect.
Verify: Run `echo $ANTHROPIC_AUTH_TOKEN` (or `echo $DEEPSEEK_API_KEY` / `$OPENROUTER_API_KEY` for Codex) and confirm the value looks like a valid key.
404 Not Found
Troubleshooting guideThe base URL is wrong (typo, extra path segment) or, on Codex, the wire protocol does not match what the provider serves.
Copy the exact base URL from this page. For Codex, keep `wire_api = "responses"` with OpenRouter; if DeepSeek returns 404 on /responses, try removing the wire_api line to match the endpoint your provider serves.
Verify: Re-run the verify command from step 3 and check the error path: it should end in /anthropic (Claude Code) or /responses (Codex).
Model Not Found
Troubleshooting guideThe model name does not exist on the provider, or the agent cached an old login that overrides the environment.
Pick a model from the provider list on this page. For Claude Code with OpenRouter, run `/logout` inside Claude Code, then relaunch — a cached Anthropic login overrides your environment variables.
Verify: Confirm ANTHROPIC_API_KEY is set to an empty string (OpenRouter) or unset (DeepSeek), then run `claude /status` again.
Rate Limit / Insufficient Quota
Troubleshooting guideThe provider limits requests per minute, or the account balance ran out.
Wait for the limit window, top up the account balance, or switch to a cheaper model. OpenRouter users can raise free-tier limits by adding credits.
Verify: Check the provider dashboard (balance and usage page) for the rejected request.
Network Error / Connection Failed
Troubleshooting guideThe machine cannot reach the provider endpoint, or an intercepting proxy rewrites the request.
Check DNS and connectivity to the base URL, review proxy and firewall settings, and retry. Use the exact endpoint from this page; third-party mirrors are not supported.
Verify: Run `curl -I <base url>` (e.g. `curl -I https://api.deepseek.com/anthropic`) and confirm a reachable response.
Timeout / Request Timed Out
Troubleshooting guideA slow model, a long prompt, or a restricted network to the provider region.
Retry the request, shorten the prompt, or switch to a faster model. Region-based connectivity issues may require a different network or a provider with closer servers.
Verify: Run the verify command again when the network is idle and confirm it completes.
Model Not Found with an old account
A cached Anthropic OAuth login overrides your environment variables.
Run /logout inside Claude Code, set the four environment variables, and relaunch.
Verify: /status should show "Anthropic base URL: https://openrouter.ai/api".
Frequently asked questions
Why must ANTHROPIC_API_KEY be empty?
Claude Code falls back to Anthropic authentication when the variable is unset or contains a real key. Setting it to an empty string forces all traffic through the OpenRouter endpoint.
What do the ~ model aliases mean?
Aliases such as ~anthropic/claude-sonnet-latest resolve to the newest model version on OpenRouter, so you do not need to update your configuration when a new version ships.
Does OpenRouter charge extra?
No markup on token pricing. Credit purchases carry a small fee, and you can set per-key budget caps in the dashboard.