In this article
Why Route Claude Code Through OpenRouter?
Claude Code is one of the most capable AI coding agents available today, but running it exclusively on Anthropic’s own API adds up quickly. A heavy session burning through Opus 4.7 can cost several dollars before lunch.
OpenRouter is an API aggregator that gives you a single endpoint to reach 400+ models from Anthropic, Google, Meta, DeepSeek, Qwen, and dozens of other providers. Crucially, it maintains an Anthropic-compatible API surface, which means Claude Code connects to it natively, without any local proxy server or wrapper script.
Three concrete reasons to make the switch:
- Free models with real coding ability. As of May 2026, OpenRouter lists 28 free models, several of which rank competitively on coding benchmarks. You can do a meaningful chunk of day-to-day coding work at zero cost.
- Automatic failover. If one Anthropic provider hits a rate limit or goes down, OpenRouter silently reroutes to another. You stop seeing 529 errors mid-session.
- Flexible model mixing. You can point Claude Code’s background tasks at a cheap model and its foreground reasoning at a stronger one, keeping costs low without sacrificing quality where it matters.
This guide covers the full setup, plus an honest ranking of the free models worth using for coding right now.
Prerequisites
- Node.js 18+ installed
- Claude Code installed (
npm install -g @anthropic-ai/claude-codeorbrew install claude-code) - A free OpenRouter account and an API key (Settings > API Keys)
You do not need an Anthropic account or API key for this setup.
Step-by-Step Setup
1. Get Your OpenRouter API Key
Log in to openrouter.ai, go to Settings > API Keys, and create a new key. Copy it somewhere safe.
2. Set the Environment Variables
Open your shell profile (~/.zshrc, ~/.bashrc, or ~/.config/fish/config.fish) and add these three lines:
export OPENROUTER_API_KEY="sk-or-v1-your-key-here"
export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
export ANTHROPIC_API_KEY=""
The blank ANTHROPIC_API_KEY is intentional. Claude Code checks for that variable first; leaving it empty forces it to fall through to ANTHROPIC_AUTH_TOKEN and the custom base URL.
Important: Add these to your shell profile, not a project .env file. Claude Code’s native installer does not read .env files from the working directory.
Reload your shell:
source ~/.zshrc # or source ~/.bashrc
3. Verify the Connection
Launch Claude Code and run:
/status
You should see the active model listed and no authentication errors. You can also open the OpenRouter Activity Dashboard and watch your requests appear in real time.
Common mistake: Using https://openrouter.ai/api/v1 as the base URL. Claude Code’s integration specifically requires https://openrouter.ai/api without the /v1 suffix. Using the wrong URL causes silent failures or authentication errors.
Switching Models Inside Claude Code
Once the integration is live, you have two ways to point Claude Code at a specific OpenRouter model.
The /model Command (Interactive)
Inside a Claude Code session, type:
/model deepseek/deepseek-v4-flash:free
This immediately switches the active model for the session. Use the full OpenRouter model ID in provider/model-name format.
Environment Variable (Persistent)
To set a default model that survives restarts, add this to your shell profile alongside the other variables:
export ANTHROPIC_DEFAULT_SONNET_MODEL="qwen/qwen3-coder:free"
Claude Code maps its internal “Sonnet-class” requests to whatever you put here. This is the variable that controls the model used for most coding tasks.
The openrouter/auto Router
OpenRouter offers a meta-model ID openrouter/auto that routes each request to the best available model given current load and capability requirements. For free-tier use, you can also target only free models:
export ANTHROPIC_DEFAULT_SONNET_MODEL="openrouter/auto"
This is the lowest-friction option if you do not want to think about model selection. The tradeoff is less predictability; the model behind each request may change between sessions.
Best Free Models for Coding (May 2026)
Not all free models on OpenRouter are created equal. Several are small research models unsuitable for agentic coding tasks. Below are the ones that hold up under real workload.
Tier 1: Use These First
Qwen3 Coder (qwen/qwen3-coder:free)
Alibaba’s Qwen3 Coder is the strongest free coding model available on OpenRouter right now. It carries a 1M-token context window and native tool-use support, both essential for Claude Code’s agentic features. It ranks near the top of the Aider leaderboard and handles multi-file refactors, test generation, and code explanation with near-Claude-4 quality on a free budget. Start here for most coding sessions.
DeepSeek V4 Flash (deepseek/deepseek-v4-flash:free)
DeepSeek V4 Flash hits 91.6% on LiveCodeBench and 79.0% on SWE-bench Verified, which puts it ahead of many paid models from six months ago. Its 1M context window and native reasoning make it especially good for debugging sessions where you need the model to trace through logic, not just pattern-match. It is DeepSeek’s “flash” tier, so it is faster and cheaper to serve than V4 Pro, which is why it remains free on OpenRouter.
Tier 2: Solid Backups
Google Gemma 4 27B (google/gemma-4-27b-it:free)
Gemma 4 27B supports both vision and tool use, making it useful when you need to describe a UI from a screenshot or analyze a diagram alongside code. Context window is 128K, which covers most single-repo tasks. Quality is a step below Qwen3 Coder and DeepSeek V4 Flash for pure coding, but it is the most reliable backup when the Tier 1 models hit rate limits.
Meta Llama 3.3 70B (meta-llama/llama-3.3-70b-instruct:free)
Llama 3.3 70B is the most widely supported open-weight model in the ecosystem. It handles boilerplate generation, documentation writing, and code explanation well. It is less capable on complex multi-step reasoning, so avoid routing hard architectural decisions through it. Treat it as a fast, free utility model for the low-stakes parts of a session.
Quick Reference Table
| Model ID | Context | Tool Use | Best For |
|---|---|---|---|
qwen/qwen3-coder:free | 1M | Yes | General coding, multi-file tasks |
deepseek/deepseek-v4-flash:free | 1M | Yes | Debugging, reasoning-heavy tasks |
google/gemma-4-27b-it:free | 128K | Yes | Vision + code, reliable fallback |
meta-llama/llama-3.3-70b-instruct:free | 128K | Partial | Boilerplate, docs, explanations |
openrouter/auto | varies | varies | Auto-routing, no preference |
Rate Limits and the Rotation Strategy
Free models on OpenRouter are rate-limited. The current limits as of May 2026:
- No credits on file: 50 requests/day across all free models
- $10+ credits deposited: 1,000 requests/day across all free models
The 50-request daily cap is genuinely tight for a coding session. Claude Code is chatty; a single /edit command on a large file can consume several requests. If you are hitting the cap before your work is done, here are the practical options.
Option 1: Deposit $10 and stay mostly free. Adding $10 in credits to your OpenRouter account bumps your free-model limit to 1,000 requests/day. You are not spending those credits on free models; you are just unlocking a higher free tier. This is the recommended path for anyone using Claude Code as a primary tool. Your $10 balance covers any paid-model overages and persists until you spend it.
Option 2: Rotate between two or three model IDs. Rate limits are applied per model, not across your entire account. You can switch mid-session from qwen/qwen3-coder:free to deepseek/deepseek-v4-flash:free when one hits its limit. Both support tool use and have 1M context, so the handoff is seamless for most tasks.
# In session, when one model runs dry:
/model deepseek/deepseek-v4-flash:free
Option 3: Use openrouter/auto with free preference. The auto-router is aware of rate limit state and will shift traffic to less-loaded models automatically. It will not always pick free models, but you can constrain it by setting a spending limit in your OpenRouter account settings.
What Does Not Work (Honest Caveats)
This setup is genuinely useful, but it is not a perfect drop-in replacement for the Anthropic-native experience.
Agentic tool use is model-dependent. Claude Code’s most powerful behaviors, including multi-step file edits, bash execution, and the memory system, rely on structured tool calls. Claude models are trained specifically for this interaction pattern. Free models like Llama 3.3 70B have partial tool-use support; they may handle simple edits but drop the ball on deeply nested agentic sequences. Stick to Qwen3 Coder or DeepSeek V4 Flash for anything beyond basic tasks.
OpenRouter’s Anthropic compatibility layer is not identical to the real API. Advanced features like extended thinking blocks, prompt caching, and some system-prompt behaviors may not work correctly. If you encounter strange output formatting or tool-call failures, switching to an Anthropic-native model (anthropic/claude-sonnet-4-5) through OpenRouter will confirm whether the issue is the free model or the integration layer.
Free model availability fluctuates. A model that is free today may move to a paid tier next month as providers adjust their economics. The list at openrouter.ai/collections/free-models is the authoritative source; check it when a model suddenly starts returning billing errors.
OpenRouter officially recommends Anthropic 1P for production. Their own documentation states that Claude Code is only guaranteed to work with the Anthropic first-party provider. Free models are a great fit for personal projects, exploration, and high-volume lower-stakes tasks, but for mission-critical automated pipelines, routing through Anthropic directly is safer.
Frequently Asked Questions
Is this allowed by Anthropic?
Yes. OpenRouter is an officially recognized integration for Claude Code. Anthropic publishes documentation for the setup and OpenRouter has a dedicated Claude Code integration page.
Can I still use paid Anthropic models through this setup?
Yes. Any model on OpenRouter is accessible, including anthropic/claude-opus-4-7 and anthropic/claude-sonnet-4-6. You pay OpenRouter’s per-token rate for those, which is comparable to or slightly above Anthropic’s direct pricing. The main reason to use paid models through OpenRouter is the failover benefit; otherwise, Anthropic direct is usually marginally cheaper.
What if a free model goes offline mid-session?
Claude Code will return an error on the next request. Run /model with an alternative free model ID to continue. If you have openrouter/auto set, the router typically handles this automatically.
Can I use this setup in a CI/CD pipeline?
Technically yes, but the free-tier rate limits are too low for automated pipelines. At 50 requests/day without credits, a single CI run could exhaust the limit. Add $10 in credits to unlock the 1,000/day tier, or route pipeline traffic through a paid model with a cost cap set in OpenRouter’s billing settings.
How do I go back to Anthropic’s API?
Remove or comment out the three environment variables (ANTHROPIC_BASE_URL, ANTHROPIC_AUTH_TOKEN, the blank ANTHROPIC_API_KEY) from your shell profile, then reload the shell and log in to Claude Code with your Anthropic account normally.
The Bottom Line
OpenRouter’s free tier has reached a point where you can run genuine coding sessions at zero cost. Qwen3 Coder and DeepSeek V4 Flash, both free on OpenRouter as of May 2026, are capable enough to handle the majority of tasks you would throw at a mid-tier paid model six months ago.
The setup takes under five minutes. The main decision after that is whether to deposit $10 to unlock the 1,000 request/day limit. For anyone using Claude Code more than casually, that is an easy call.
For occasional use or experimentation, the free tier as-is is more than enough to get real work done.


