Based on Anthropic’s official docs, checked
Claude Code with OpenRouter: setup, models and troubleshooting
OpenRouter exposes an Anthropic Messages API compatible endpoint, so Claude Code can talk to it directly by changing a few environment variables, with no local proxy. OpenRouter pitches it for provider failover, team budget controls and usage analytics. Its docs say Claude Code through OpenRouter is only guaranteed to work with the Anthropic first-party provider, and recommend making Anthropic 1P the top-priority provider. This guide follows OpenRouter’s Claude Code guide and Anthropic’s LLM gateway docs, checked October 31, 2026.
1. Install Claude Code
Use the native installer (or npm with Node.js 18 or newer). See our install guide for details.
curl -fsSL https://claude.ai/install.sh | bash2. Set the gateway variables
Add these to your shell profile (~/.zshrc or ~/.bashrc), then reload it. The OpenRouter key goes in ANTHROPIC_AUTH_TOKEN, which Claude Code sends as a bearer token, and ANTHROPIC_API_KEY must be explicitly empty. Define OPENROUTER_API_KEY before the line that uses it. Don’t use a project .env file; OpenRouter notes the native installer doesn’t read it.
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="" source ~/.zshrc3. Clear a cached Anthropic login
If you were signed in to Claude Code with an Anthropic account, run /logout once, then quit and relaunch claude. A cached login next to a gateway credential causes auth-conflict warnings and model-not-found errors.
/logout4. Start and verify
Run claude in your project, then /status. It should show ANTHROPIC_AUTH_TOKEN as the auth token and https://openrouter.ai/api as the base URL. Requests also appear in your OpenRouter Activity Dashboard.
> /status Auth token: ANTHROPIC_AUTH_TOKEN Anthropic base URL: https://openrouter.ai/api
Project-scoped setup instead of your shell profile
You can put the same variables in the env block of .claude/settings.local.json in your project root. Keep your real key out of version control.
Billing and your Claude subscription
Anthropic’s LLM gateway docs say that while a gateway credential is active, requests carry it instead of your claude.ai subscription login, and the subscription’s usage limits don’t apply to them. With OpenRouter you pay from OpenRouter credits, and usage, including reasoning tokens, shows in your OpenRouter dashboard.
Setting only ANTHROPIC_BASE_URL without a gateway credential doesn’t replace the subscription: a saved claude.ai login stays the active credential.
Choosing models for each role
Claude Code picks models by role. Override them with ANTHROPIC_DEFAULT_OPUS_MODEL, ANTHROPIC_DEFAULT_SONNET_MODEL, ANTHROPIC_DEFAULT_HAIKU_MODEL, ANTHROPIC_DEFAULT_FABLE_MODEL and CLAUDE_CODE_SUBAGENT_MODEL, then open /model to confirm each class is listed. OpenRouter says Fable isn’t offered in /model unless you set ANTHROPIC_DEFAULT_FABLE_MODEL.
Set CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1 to show OpenRouter’s gateway model picker, a curated list rather than the full catalog. Some entries route to non-Anthropic providers, and OpenRouter warns Claude Code may not work correctly with them.
Fast mode and the GitHub Action
To use /fast through OpenRouter, set CLAUDE_CODE_SKIP_FAST_MODE_ORG_CHECK=1 (Claude Code v2.1.96 or newer) and pin ANTHROPIC_DEFAULT_OPUS_MODEL to a specific supported Opus ID; with a “latest” alias, OpenRouter says requests go out at standard speed.
In the official claude-code-action, pass the OpenRouter key as anthropic_api_key and set ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN in the step’s env block.
Troubleshooting
Model-not-found errors for OpenRouter models usually mean a credential conflict: run /logout for a cached login, or empty a real ANTHROPIC_API_KEY left in your profile. Auth errors: check the key is in ANTHROPIC_AUTH_TOKEN and reload your shell.
If the context window always shows 200k, append [1m] to a model slug that supports a 1M window; OpenRouter strips the marker before routing.
Tips
- Load the key from your OS keychain rather than leaving it in plain text in a dotfile, and rotate it at openrouter.ai/settings/keys if it leaks.
- Make Anthropic first-party the top-priority provider in OpenRouter for the most reliable results.
- OpenRouter says it doesn’t log your prompts unless you opt in to prompt logging in account settings.
Next steps
Verified MCP servers for Claude
Generate an MCP config for Claude
Claude Code hub
Claude Code vs OpenAI Codex: documented features compared
Cursor vs Claude Code: documented features compared
Claude Code vs GitHub Copilot: documented features compared
Google Antigravity vs Claude Code: documented features compared
How to add an MCP server to Codex
How to add an MCP server to Cursor
How to add an MCP server to GitHub Copilot in VS Code
How to add an MCP server to Google Antigravity
How to migrate from Gemini CLI to Antigravity CLI
Portable agent skills: one SKILL.md for every coding agent
Safe “always allow” rules for Claude Code and Codex CLI
GitHub Copilot CLI yolo mode: --allow-all and safer tool rules
Codex sandbox modes and approval policies explained
How to install Claude Code on macOS, Linux and Windows
How to install the OpenAI Codex CLI
Claude Code auto mode: what runs, what gets blocked, how to turn it off
Agent Skills moves to the Agentic AI Foundation: what changes
MCP security: the risks in MCP servers and how to lock them down
Claude Code in VS Code: install, shortcuts and CLI differences
What is AGENTS.md? The open instruction file for coding agents
Claude Code on Windows: native, WSL and common errors
Codex skills: create, install and use SKILL.md skills in Codex
GitHub Copilot CLI: install, sign in and start using it
Cursor Agent SDK: build, test and deploy agents as code
GPT-6.1 Terra in Codex and the API: setup, pricing and limits
MCP OAuth: connect a remote MCP server in Claude Code, Codex and Cursor
Codex config.toml: location, precedence and the settings to change
Codex on Windows: native sandbox setup, WSL and fixes
Antigravity CLI (agy): install, sign in, modes and commands
Google Antigravity on Windows: install, WSL, sandbox and fixes
GitHub Copilot coding agent (now cloud agent): how to use it
GitHub Copilot agent mode: how to use it in your IDE
Claude Code tips from Anthropic’s best practices
How to add an MCP server: Claude Code, Codex, Cursor, Copilot and Antigravity
Supabase RLS for AI-built apps: grants, policies and tests
Antigravity skills: create, store and run SKILL.md skills
Claude Code in JetBrains IDEs: install, connect and fix issues
GitHub Copilot skills: create, install and use agent skills
Claude Code subagents: create, configure and run them
Check which plugins, hooks and MCP servers your agent loads
Cursor rules examples: .mdc files for each rule type
Fix spawn npx ENOENT when starting an MCP server
How to install Claude Code
Claude Code pricing
Claude Code tips from the official docs
Claude Code MCP setup
Claude Code
Frequently asked questions
Yes. Set ANTHROPIC_BASE_URL to https://openrouter.ai/api, put your OpenRouter key in ANTHROPIC_AUTH_TOKEN and set ANTHROPIC_API_KEY to an empty string.
Sources
- https://openrouter.ai/docs/guides/guides/claude-code-integration · checked 2026-10-31
- https://code.claude.com/docs/en/llm-gateway · checked 2026-10-31
- https://code.claude.com/docs/en/llm-gateway-connect · checked 2026-10-31