Setup guide - Documented by OpenRouter
Use OpenRouter in Claude Code
OpenRouter publishes an official Claude Code guide: point ANTHROPIC_BASE_URL at OpenRouter's Anthropic-compatible endpoint (https://openrouter.ai/api), pass the OpenRouter key as ANTHROPIC_AUTH_TOKEN and blank out ANTHROPIC_API_KEY. You can override each model class with OpenRouter slugs.
Last verified 10 steps2 official sources
Steps
- Step 1.
Install Claude Code (macOS, Linux, WSL).
Terminalcurl -fsSL https://claude.ai/install.sh | bash - Step 2.
Or install with npm (requires Node.js 18 or newer).
Terminalnpm install -g @anthropic-ai/claude-code - Step 3.
Add these environment variables to your shell profile (~/.zshrc or ~/.bashrc). ANTHROPIC_API_KEY must be explicitly empty.
Terminalexport 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="" # Important: Must be explicitly empty export CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1 # Optional: enables the gateway model picker - Step 4.
Reload your shell so the new values are live.
Snippetsource ~/.zshrc # or ~/.bashrc, or open a new terminal - Step 5.
Alternative: put the same values in a project-level .claude/settings.local.json in your project root (do not commit your key).
JSON{ "env": { "ANTHROPIC_BASE_URL": "https://openrouter.ai/api", "ANTHROPIC_AUTH_TOKEN": "<your-openrouter-api-key>", "ANTHROPIC_API_KEY": "", "CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY": "1" } } - Step 6.
If you previously logged in to Claude Code with an Anthropic account, run /logout once, then quit and relaunch claude.
Snippet> /logout - Step 7.
Start a session in your project.
Terminalcd /path/to/your/project claude - Step 8.
Verify with /status. The output should name ANTHROPIC_AUTH_TOKEN and the OpenRouter base URL.
Snippet> /status Auth token: ANTHROPIC_AUTH_TOKEN Anthropic base URL: https://openrouter.ai/api - Step 9.
Optional: route each Claude Code model class to a specific OpenRouter model.
Terminalexport ANTHROPIC_DEFAULT_FABLE_MODEL="~anthropic/claude-fable-latest[1m]" export ANTHROPIC_DEFAULT_OPUS_MODEL="~anthropic/claude-opus-latest[1m]" export ANTHROPIC_DEFAULT_SONNET_MODEL="~anthropic/claude-sonnet-latest[1m]" export ANTHROPIC_DEFAULT_HAIKU_MODEL="~anthropic/claude-haiku-latest" export CLAUDE_CODE_SUBAGENT_MODEL="~anthropic/claude-opus-latest[1m]" - Step 10.
Optional: enable the /fast command (requires Claude Code v2.1.96 or newer). Pin ANTHROPIC_DEFAULT_OPUS_MODEL to a specific Opus id such as anthropic/claude-opus-5 for fast mode to actually apply.
Terminalexport CLAUDE_CODE_SKIP_FAST_MODE_ORG_CHECK=1
Caveats
- OpenRouter's own warning: Claude Code with OpenRouter is only guaranteed to work with the Anthropic first-party provider. It recommends setting Anthropic 1P as the top-priority provider, and notes Claude Code may not work correctly with non-Anthropic models.
- The base URL is https://openrouter.ai/api (no /v1). This is the Anthropic Messages-compatible endpoint, not the OpenAI-compatible /api/v1.
- Do not put these variables in a project .env file. The native Claude Code installer does not read standard .env files.
- Ordering matters: OPENROUTER_API_KEY must be defined before ANTHROPIC_AUTH_TOKEN expands it, or every request fails with an auth error.
- Fable is not offered in /model by default when pointed at OpenRouter. Setting ANTHROPIC_DEFAULT_FABLE_MODEL is the only way to make it selectable.
- /fast with the ~anthropic/claude-opus-latest alias reports "Fast mode ON" but sends no speed parameter. Pin a concrete Opus id to get fast mode.
- Claude Code assumes a 200k context window unless the model name ends in [1m].
More setup guides
Same tool, other providers
Related reading
- Model Routing Recipes: Practical Config Patterns to Cut AI SpendModel routing recipes with paste-ready OpenRouter and LiteLLM configs for task tiers, fallback chains, cost ceilings and cache-aware routing that cut AI spend.
- OpenRouter Fusion Makes Model Panels Real. Use Them Like Escalation, Not AutopilotOpenRouter Fusion turns multi-model panels into an API feature. Use it as an escalation lane for prompts where a wrong answer is expensive, not as the default.
- OpenRouter in 2026: Review, Setup, and When Model Routing PaysOpenRouter review and setup - one API key for 300+ models with fallbacks and provider routing, what it costs, and when to go direct to a provider instead.
- LLM Routers Compared: LiteLLM vs Portkey vs OpenRouter in 2026LiteLLM vs Portkey vs OpenRouter compared for production AI apps: cost tracking, fallbacks, caching, and load balancing, plus when to use each router.
- How to Build Agents That Run for Hours: Anthropic's Planner, Generator and Evaluator HarnessAnthropic Applied AI's Ash Prabaker and Andrew Wilson trace how Claude went from 20-minute runs to multi-hour builds, and the GAN-style planner, generator and evaluator harness behind their one-shot apps.
- Why One Agent Isn't Enough: Anthropic on Flows, UltraCode and Running 1,000 AgentsCJ Avilla and Harrison Stall of Anthropic on multi-agent topologies: lessons from Unix, OS/360 and Linux, Dynamic Workflows in Managed Agents, and when to use Flows vs UltraCode.
FAQ
How do I use OpenRouter in Claude Code?
OpenRouter publishes an official Claude Code guide: point ANTHROPIC_BASE_URL at OpenRouter's Anthropic-compatible endpoint (https://openrouter.ai/api), pass the OpenRouter key as ANTHROPIC_AUTH_TOKEN and blank out ANTHROPIC_API_KEY. You can override each model class with OpenRouter slugs.
Which OpenRouter model ids work in Claude Code?
The official docs read for this guide list ~anthropic/claude-fable-latest[1m], ~anthropic/claude-opus-latest[1m], ~anthropic/claude-sonnet-latest[1m], ~anthropic/claude-haiku-latest, anthropic/claude-opus-5, anthropic/claude-opus-4.8, anthropic/claude-opus-4.7, anthropic/claude-opus-4.6, openrouter/auto and openrouter/pareto-code. Requests go to https://openrouter.ai/api.
Is this an official OpenRouter and Claude Code setup?
OpenRouter documents this setup in its own docs. It was last checked against 2 official pages on November 8, 2026.
Sources
Last verified against 2 official pages.
Newsletter
New models, set up the day they ship
One email per week. New model releases, the coding tools that support them and how to wire them up.