Claude Code
Claude Code is a powerful agentic tool. While you can use any model via OpenRouter, we recommend sticking to highly capable models (like Claude Opus 4.5, GPT 5.2, etc.) for the best experience, as complex coding tasks require strong reasoning.
Quick Start
This guide will get you running Claude Code powered by OpenRouter in just a few minutes.
Step 1: Install Claude Code
Native Install (Recommended)
npm
macOS, Linux, WSL:
Windows PowerShell:
Step 2: Connect Claude to OpenRouter
Instead of logging in with Anthropic directly, connect Claude Code to OpenRouter. This requires setting a few environment variables.
Requirements:
- Use
https://openrouter.ai/apifor the base url - Provide your OpenRouter API key as the auth token
- Important: Explicitly blank out the Anthropic API key to prevent conflicts
Shell Profile
Project Settings File
Add these environment variables to your shell profile:
Persistence: We recommend adding these lines to your shell profile (~/.bashrc, ~/.zshrc, or ~/.config/fish/config.fish).
Do not put these in a project-level .env file. The native Claude Code installer does not read standard .env files.
Step 3: Start your session
Navigate to your project directory and start Claude Code:
You are now connected! Any prompt you send will be routed through OpenRouter.
Step 4: Verify
You can confirm your connection by running the /status command inside Claude Code.
You can also check the OpenRouter Activity Dashboard to see your requests appearing in real-time.
Changing Models
By default, Claude Code uses specific Anthropic models aliases (like “Sonnet”, “Opus”, “Haiku”). OpenRouter automatically maps these to the correct Anthropic models.
Overriding Default Models
You can configure Claude Code to use any model on OpenRouter (including OpenAI, Google, or Llama models) by overriding the default model aliases using environment variables.
Tool Use Required: Claude Code relies on tool use capabilities to perform actions like reading files, running terminal commands, and editing code. When selecting alternative models, make sure they support tool use. See the full list of models that support tool use.
For example, to swap the default “Sonnet” alias for GPT-5.1 Codex Max:
You can override other tiers as well:
Tip: While you can use any model, Claude Code relies deeply on agentic behaviors (tool use, precise formatting). For best results, we recommend using Exacto—our specialized coding and tool-use model—designed for strong code generation and reliable agentic performance. Learn more about Exacto on our Exacto variant page.
Advanced Configuration with Presets
For more complex configurations—such as defining fallback models, custom system prompts, or specific provider routing—you can use OpenRouter Presets.
- Create a preset at openrouter.ai/settings/presets (e.g., with slug
my-coding-setup). - Use the preset as your model override:
This allows you to manage model settings and fallbacks on OpenRouter without changing your local environment variables.
How It Works
OpenRouter exposes an input that is compatible with the Anthropic Messages API.
- Direct Connection: When you set
ANTHROPIC_BASE_URLtohttps://openrouter.ai/api, Claude Code speaks its native protocol directly to OpenRouter. No local proxy server is required. - Anthropic Skin: OpenRouter’s “Anthropic Skin” behaves exactly like the Anthropic API. It handles model mapping and passes through advanced features like “Thinking” blocks and native tool use.
- Billing: You are billed using your OpenRouter credits. Usage (including reasoning tokens) appears in your OpenRouter dashboard.
Using Your Claude Subscription
If you have a Claude subscription, you can use it for Anthropic models while routing non-Anthropic models through OpenRouter. This gives you the best of both worlds: your Claude subscription handles Anthropic models, while OpenRouter provides access to its full model catalog.
How It Works
When you use Claude Code with OpenRouter and have a Claude subscription, Claude Code automatically sends your subscription token via the x-api-key header. OpenRouter detects this and routes accordingly:
- Anthropic models (Claude Sonnet, Opus, Haiku) → Routed using your Claude subscription
- Non-Anthropic models (GPT, Gemini, Llama, etc.) → Routed through OpenRouter, billed to your OpenRouter credits
- Fallback → If your Claude subscription limits are reached, requests automatically fall back to OpenRouter
No additional configuration is needed beyond the standard setup above. Claude Code handles sending your subscription credentials automatically.
Agent SDK
The Anthropic Agent SDK lets you build AI agents programmatically using Python or TypeScript. Since the Agent SDK uses Claude Code as its runtime, you can connect it to OpenRouter using the same environment variables described above.
For complete setup instructions and code examples, see our Anthropic Agent SDK integration guide.
GitHub Action
You can use OpenRouter with the official Claude Code GitHub Action.To adapt the example workflow for OpenRouter, make two changes to the action step:
- Pass your OpenRouter API key via
anthropic_api_key(store it as a GitHub secret namedOPENROUTER_API_KEY) - Set the
ANTHROPIC_BASE_URLenvironment variable tohttps://openrouter.ai/api
Cost Tracking Statusline
You can add a custom statusline to Claude Code that tracks your OpenRouter API costs in real-time. The statusline displays the provider, model, cumulative cost, and cache discounts for your session.

Download the statusline scripts from the openrouter-examples repository, make them executable, and add the following to your ~/.claude/settings.json:
The script uses your ANTHROPIC_AUTH_TOKEN environment variable, which should already be set to your OpenRouter API key if you followed the setup above.
Troubleshooting
- Tool Use Errors: If you see errors about tool use not being supported, the model you selected does not support tool use capabilities. Claude Code requires tool use to read files, run commands, and edit code. Switch to a model that supports tool use.
- Auth Errors: Ensure
ANTHROPIC_API_KEYis set to an empty string (""). If it is unset (null), Claude Code might fall back to its default behavior and try to authenticate with Anthropic servers. - Context Length Errors: If you swap deep-reasoning tasks to smaller models, you may hit context limits. Use models with at least 128k context windows for best results.
- Privacy: OpenRouter does not log your source code prompts unless you explicitly opt-in to prompt logging in your account settings. See our Privacy Policy for details.