How to Use Claude Code with OpenRouter
TL;DR Learn how to configure Claude Code to route LLM calls through OpenRouter, enabling access to models from multiple providers through a single API.
-
OpenRouter is an API gateway that gives you access to LLMs from multiple providers (Anthropic, OpenAI, DeepSeek, Meta, Google, etc.) through a single endpoint
- Use Claude Code with non-Anthropic models like DeepSeek or OpenAI GPT
- Or mix models from different providers depending on the task
-
For a general introduction to Claude Code, see the Claude Code setup guide
-
This guide covers:
- Setting up OpenRouter, with and without BYOK (Bring Your Own Key)
- Testing the API connection
- Configuring Claude Code to route through OpenRouter
What is OpenRouter and BYOK#
-
OpenRouter acts as a proxy between your client and LLM providers
- You send requests to
https://openrouter.ai/api/v1 - OpenRouter routes them to the appropriate provider
- You send requests to
-
BYOK (Bring Your Own Key) lets you use your existing API keys from providers (Anthropic, OpenAI, etc.) instead of buying credits from OpenRouter
- Useful if you already have subscriptions or credits with a provider
- Register your keys with OpenRouter and traffic routes through your own accounts
Step 1: Configure OpenRouter with BYOK#
-
Generate an OpenRouter API key (
sk-or-v1-...) for your client applications -
To bring your own keys, create API keys for your providers at:
Provider Environment Variable Key Format Anthropic ANTHROPIC_API_KEYsk-ant-api03-...OpenAI OPENAI_API_KEYsk-proj-... -
Note that Anthropic encodes in the key
...-api-03-...since you need to use metered use -
Register your BYOK keys with OpenRouter at: BYOK Settings
Step 2: Test the OpenRouter API#
-
List available models through OpenRouter:
-
Test a simple completion:
Step 3: Configure Claude Code for OpenRouter#
- Claude Code uses Anthropic's SDK, which connects to
api.anthropic.comby default -
To route through OpenRouter, set these environment variables:
-
Send the API requests from Claude Code to OpenRouter using their API and key
- Select the models to use for the different Claude Code tier (e.g., using OpenAI GPT-5 using OpenRouter)
-
Unset the direct Anthropic key to avoid conflicts
-
Verify the environment is configured correctly:
Testing Connection to OpenRouter with the Anthropic Python SDK#
-
Run the script
.claude/test_openrouter_api.pyto verify that OpenRouter responds when using the Anthropic API
Step 4: Using Different Models#
- Once OpenRouter is configured, point Claude Code's model tiers at whatever models you want
-
For help choosing which model fits your workflow, see How to Compare and Choose LLM Models:
-
A set up I personally like based on the analysis above
-
Verify the current configuration:
Test Claude Code#
Verifying the Model in Claude Code#
-
Once Claude Code launches note that the model is the one mapped on Haiku (in the case above, it's
deepseek-v4-flash) and the access isAPI Usage Billing(since we are not going through a Claude plan) -
Check which model is active with the
/modelcommand:
``` ❯ /model
Select model
Switch between Claude models. Your pick becomes the default for new
sessions.
1. Default (recommended) Use the default model (currently
anthropic/haiku-4.5) · $5/$25 per Mtok
2. anthropic/haiku-4.5 Custom Opus model
3. anthropic/haiku-4.5 Custom Sonnet model
❯ 4. deepseek/deepseek-v4-flash ✔ Custom Haiku model
```
Monitoring and Troubleshooting#
-
Check OpenRouter Logs for request history, latency, and error details
-
For an alternative workflow using OpenRouter with Simon Willison's LLM CLI, see How to Use OpenRouter with LLM CLI
-
Refer to the OpenRouter API documentation for the full API specification
-
Common issues:
- Authentication errors: Make sure
ANTHROPIC_AUTH_TOKENis your OpenRouter key, not your Anthropic key - Model not found: Check the exact model ID on OpenRouter's models page
(e.g.,
deepseek/deepseek-v4-flash, notdeepseek-v4-flash) - Rate limiting: OpenRouter applies rate limits per provider
- Key conflicts: Unset
ANTHROPIC_API_KEYwhen using OpenRouter since it can conflict withANTHROPIC_AUTH_TOKEN
- Authentication errors: Make sure
Using cc#
- This repository also ships a
ccwrapper script that automates the environment variable setup for OpenRouter - See The
ccConvenience Wrapper)