
Claude Code ships with Anthropic models, but a single environment variable, ANTHROPIC_BASE_URL, lets you route requests to practically any LLM provider. That means you can swap in DeepSeek at roughly $0.27 per million output tokens instead of Claude Opus at $25, or run models locally through Ollama for free. This guide covers every method to use different models in Claude Code, building on our detailed Claude Code comparison with hands-on setup for each approach.
Every Method at a Glance
Pick the method that matches your situation, then jump to its section for setup code.
| Method | Best For | Cost | Setup | Models |
|---|---|---|---|---|
| OpenRouter | Model variety, one API key | Pay-per-token (pass-through) | Easy | 300+ |
| Ollama | Privacy, free, offline | Free (your hardware) | Easy | 50+ open-source |
| LM Studio | Local models with a GUI | Free (your hardware) | Easy | 50+ open-source |
| claude-code-router | Smart per-task routing | Varies by provider | Medium | Depends on config |
| claude-code-proxy | Gemini/OpenAI backend | Provider pricing | Medium | OpenAI + Gemini |
| LiteLLM | Enterprise, load balancing | Provider pricing | Advanced | 100+ across providers |
Read on for step-by-step setup of each method, or jump to the decision framework if you already know what you need.
How Does Claude Code Model Switching Actually Work?
Before you configure any proxy, you need to understand why most tutorials leave you with a broken setup. Claude Code internally uses three model slots, not one:
- Haiku slot, background tasks like file summarization and tab completion
- Sonnet slot, default coding, the model you interact with most
- Opus slot, complex reasoning, planning, and multi-step agentic tasks
When you change the model with /model or --model, you're only changing the primary slot. Claude Code still tries to call the other two slots using Anthropic's API. If you've pointed ANTHROPIC_BASE_URL at a proxy but only set one model, those other slots fail with a "model not found" error.
The fix: set all three environment variables so every slot routes through your proxy.
# Cut Claude Code's Bill 90%: Run OpenRouter, Ollama or LiteLLM
export ANTHROPIC_DEFAULT_HAIKU_MODEL="your-haiku-equivalent"
export ANTHROPIC_DEFAULT_SONNET_MODEL="your-sonnet-equivalent"
export ANTHROPIC_DEFAULT_OPUS_MODEL="your-opus-equivalent"Native Model Switching (Anthropic Models)
If you're staying within Anthropic's model family, switching is straightforward. Use the /model command inside Claude Code, pass --model when launching from the terminal, or add models to the availableModels array in your settings file. Anthropic keeps adding options here, and our roundup of the newest Claude Code features for 2026 tracks what's shipped recently.
The opusplan Strategy
opusplan is Claude Code's built-in multi-model routing. It sends background tasks to Haiku and funnels complex reasoning to Opus, automatically. You don't configure it manually; just select it as your model. It's Anthropic's own answer to the "why am I paying Opus prices for a file listing?" problem. Worth trying before you reach for a third-party router, alongside Anthropic's fast mode if raw speed is your priority.
OpenRouter, One API Key, 300+ Models
OpenRouter acts as an API aggregator. You get a single API key and access to 300+ models from Anthropic, Google, Meta, Mistral, DeepSeek, and more. Pricing is pass-through for most models, no markup.
Setup Steps
- Create an account at openrouter.ai and generate an API key
- Run
claude /logoutto clear any existing Anthropic session - Set your environment variables:
export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
export ANTHROPIC_API_KEY="sk-or-v1-your-key-here"
# Map all three model slots
export ANTHROPIC_DEFAULT_HAIKU_MODEL="deepseek/deepseek-chat-v3"
export ANTHROPIC_DEFAULT_SONNET_MODEL="anthropic/claude-sonnet-4"
export ANTHROPIC_DEFAULT_OPUS_MODEL="anthropic/claude-opus-4"- Launch
claudeand verify the connection
Notice the three slot variables, skip them and you'll hit the "model not found" error we covered above. The official OpenRouter integration guide confirms this setup.
Recommended Models via OpenRouter
Not every model on OpenRouter handles Claude Code's tool calling reliably. Here's what actually works well:
| Task | Model | Why |
|---|---|---|
| Cheap background tasks | DeepSeek V3.2 | $0.27/$1.10 per MTok, decent tool calling |
| Default coding | Claude Sonnet 4 | Same quality as direct, consolidated billing |
| Huge codebases (1M context) | Gemini 2.5 Flash | $0.15/$0.60 per MTok, massive context window |
| Complex reasoning | Claude Opus 4 | Best agentic coding quality |
Cost and Billing
OpenRouter charges pass-through pricing on most models. You pay exactly what the underlying provider charges, plus a small margin on some third-party models. Set spending limits in your OpenRouter dashboard to avoid surprises.
Verdict: OpenRouter is the best option for developers who want maximum model variety with minimal setup. One API key, one billing dashboard, 300+ models. Hard to beat for flexibility.
Ollama, Free, Local, and Private
Ollama runs models entirely on your machine. Since v0.14.0 (January 2026), it supports the Anthropic Messages API natively, which means Claude Code can talk to it without any translation layer. Your code never leaves your machine.
Prerequisites and Hardware
You'll need decent hardware for a usable experience:
- 7B models (quick edits, simple tasks): 16GB RAM minimum
- 14B models (solid coding quality): 32GB RAM, GPU recommended
- 32B+ models (near-cloud quality): 64GB RAM or dedicated GPU with 24GB+ VRAM
For a deeper explore hardware requirements and model selection, check our guide to running LLMs locally.
Setup Steps
- Install Ollama from ollama.com
- Pull a coding-focused model
- Launch with Claude Code compatibility
- Set your environment variables
# Install and pull a coding model
ollama pull qwen2.5-coder:14b
# Launch with Claude Code compatibility
ollama launch claude
# Set env vars
export ANTHROPIC_BASE_URL="http://localhost:11434"
export ANTHROPIC_API_KEY="ollama"
export ANTHROPIC_DEFAULT_HAIKU_MODEL="qwen2.5-coder:14b"
export ANTHROPIC_DEFAULT_SONNET_MODEL="qwen2.5-coder:14b"
export ANTHROPIC_DEFAULT_OPUS_MODEL="qwen2.5-coder:14b"The Ollama integration docs recommend models with 64k+ token context windows for the best Claude Code experience.
Best Local Models for Coding
| Model | Size | Strength | Notes |
|---|---|---|---|
| Qwen 2.5 Coder 14B | 14B | Best overall local coding | Strong tool calling, good context handling |
| devstral | 24B | Mistral's coding specialist | Great for refactoring and code review |
| GLM 4.7 | 9B | General reasoning | Good balance of speed and quality |
Be honest with yourself about one thing: local models produce noticeably lower quality code than Claude Sonnet for complex, multi-step agentic tasks. Tool calling is less reliable, and you'll see more hallucinated file paths. They're fantastic for quick edits, simple generation, and privacy-sensitive work, just don't expect native Claude quality on a 50-file refactor.
Verdict: Ollama is the best choice for privacy-sensitive work and developers who want free, unlimited usage. If you have the hardware, it's hard to argue with "zero dollars forever."
Ollama Cloud vs OpenRouter: Pricing and When Each Wins
Ollama and OpenRouter solve the cost problem from opposite ends. Ollama runs models on hardware you control at zero per-token cost, while OpenRouter charges pass-through pricing across 300+ hosted models with no hardware to manage. This guide sets Ollama up locally, and the same fixed-cost-versus-per-token math applies if you run its hosted tier instead.
| Factor | Ollama | OpenRouter |
|---|---|---|
| Cost model | Free locally, you pay for hardware | Pass-through per token, no markup on most models |
| Example price | $0 per token | DeepSeek V3.2 at $0.27/$1.10, Gemini 2.5 Flash at $0.15/$0.60 per MTok |
| Hardware | 16GB RAM (7B) up to 24GB+ VRAM (32B+) | None, fully hosted |
| Model access | 50+ open-source models | 300+ across providers |
| Privacy | Code never leaves your machine | Requests pass through OpenRouter |
Choose Ollama when privacy, offline use, or high-volume routine edits matter and you already own the hardware. Choose OpenRouter when you want model variety, no upfront hardware, and pay-only-for-what-you-use billing.
LM Studio, Local Models With a GUI
LM Studio offers the same local model execution as Ollama, but with a visual interface. You browse models in a GUI, pick quantization levels with a click, and test chat before pointing Claude Code at it. Same hardware requirements as Ollama. If you're comparing runners, our guide to the best tools to run LLMs locally covers the trade-offs.
Setup
- Download LM Studio and install it
- Search for and download a coding model (Qwen 2.5 Coder, devstral, etc.)
- Start the local server from the LM Studio interface
- Point Claude Code at it:
# Point Claude Code to LM Studio's local server
export ANTHROPIC_BASE_URL="http://localhost:1234/v1"
export ANTHROPIC_API_KEY="lm-studio"
export ANTHROPIC_DEFAULT_SONNET_MODEL="your-loaded-model-name"The LM Studio blog has a walkthrough with screenshots if you prefer visual setup guides.
Verdict: LM Studio is best for developers who prefer a GUI over CLI for managing local models. If you're already comfortable with ollama pull and config files, stick with Ollama. If you want to browse models visually and tweak settings with sliders, LM Studio is your pick.
claude-code-router, Smart Model Routing
claude-code-router is an npm package that runs a local proxy between Claude Code and your configured providers. What makes it interesting is context-aware routing, you can send cheap background tasks to DeepSeek, default coding to Sonnet, and complex reasoning to Opus, all automatically.
The project has 30k+ GitHub stars and supports OpenRouter, DeepSeek, Gemini, Ollama, Groq, Volcengine, and SiliconFlow as providers.
Installation and Setup
npm install -g claude-code-router
claude-code-router init
claude-code-router startThe init command creates a config.json where you define your providers and model mappings:
{
"providers": {
"openrouter": {
"apiKey": "sk-or-v1-...",
"models": {
"haiku": "deepseek/deepseek-chat-v3",
"sonnet": "anthropic/claude-sonnet-4",
"opus": "anthropic/claude-opus-4"
}
}
}
}Context-Aware Routing Configuration
This is where claude-code-router shines. You can route different task types to different models based on complexity. Background summarization goes to a cheap model, while planning and architecture work goes to your best model. The router handles all the slot mapping transparently.
You can also mix providers, use Ollama for privacy-sensitive repos and OpenRouter for everything else, switching with the /model command inside Claude Code.
Verdict: claude-code-router is best for power users who want fine-grained control over which model handles each task type. It's the most configurable option, but that flexibility comes with more setup than a simple ANTHROPIC_BASE_URL swap.
claude-code-proxy and LiteLLM, Advanced Setups
These two methods serve different needs but share one trait: they're both proxy servers that translate between API formats.
claude-code-proxy (Gemini and OpenAI Backend)
claude-code-proxy is a Python tool (3.3k GitHub stars) that accepts Anthropic-format requests from Claude Code and translates them to OpenAI or Gemini format via LiteLLM under the hood. It's the simplest way to use Gemini or GPT models specifically with Claude Code.
pip install claude-code-proxy
claude-code-proxy --port 8080
# Then point Claude Code at it
export ANTHROPIC_BASE_URL="http://localhost:8080"
export ANTHROPIC_API_KEY="your-gemini-or-openai-key"If you specifically want Gemini's 1M token context for large monorepos, this is the most direct path.
LiteLLM (Enterprise Universal Proxy)
LiteLLM is an enterprise-grade proxy that supports 100+ providers with load balancing, fallback chains, rate limiting, and audit logging. It's overkill for a solo developer but essential for teams that need governance around AI model usage.
The setup uses a YAML config:
model_list:
- model_name: claude-sonnet-4-20250514
litellm_params:
model: gemini/gemini-2.5-flash
api_key: os.environ/GEMINI_API_KEY
- model_name: claude-haiku-3-5-20241022
litellm_params:
model: deepseek/deepseek-chat
api_key: os.environ/DEEPSEEK_API_KEY# Start LiteLLM proxy
litellm --config config.yaml --port 4000
# Point Claude Code to LiteLLM
export ANTHROPIC_BASE_URL="http://localhost:4000"
export ANTHROPIC_API_KEY="sk-litellm"One important note from the official Claude Code docs: "Anthropic doesn't endorse, maintain, or audit LiteLLM." Use it, but know it's a community tool, not an Anthropic product. The LiteLLM tutorial has more advanced configuration examples.
Verdict: Use claude-code-proxy if you want Gemini or OpenAI models specifically. Use LiteLLM if your team needs load balancing, fallback, and audit logging across providers.
Claude Code Router vs LiteLLM: Which One Should You Use?
Claude Code Router is the simpler pick for solo developers who want per-task model routing, while LiteLLM is built for teams that need load balancing, fallback chains, and audit logging. Router installs via npm and reads a config.json; LiteLLM runs an enterprise YAML-configured proxy across 100+ providers.
| Factor | Claude Code Router | LiteLLM |
|---|---|---|
| Setup difficulty | Medium: npm install, edit config.json | Advanced: YAML config file |
| Best for | Solo devs wanting per-task routing | Teams needing governance |
| Config format | config.json | YAML model_list |
| Standout feature | Context-aware per-task routing | Load balancing, fallback, audit logging |
| Providers | OpenRouter, DeepSeek, Gemini, Ollama, Groq, and more | 100+ across providers |
| Maturity | 30k+ GitHub stars, built for Claude Code | Enterprise proxy, community tool (not Anthropic-endorsed) |
Pick claude-code-router if you're a solo developer who wants cheap background tasks on DeepSeek and complex reasoning on Opus without much setup. Pick LiteLLM if your team needs load balancing, rate limiting, and audit logs across providers.
Weighing other proxies too? Our roundup of the best LLM gateway tools compares them side by side.
What Does Each Method Actually Cost?
Let's put real numbers on this. Here's what you'll pay per 1M tokens across the most common model/method combinations:
| Model / Method | Input (per 1M tokens) | Output (per 1M tokens) | Monthly Estimate (moderate use) |
|---|---|---|---|
| Claude Opus 4.6 (direct) | $5.00 | $25.00 | ~$50-150 |
| Claude Sonnet 4.6 (direct) | $3.00 | $15.00 | ~$30-60 |
| Claude Sonnet via OpenRouter | $3.00 | $15.00 | Same (pass-through) |
| DeepSeek V3.2 (OpenRouter) | $0.27 | $1.10 | ~$3-8 |
| Gemini 2.5 Flash (OpenRouter) | $0.15 | $0.60 | ~$2-5 |
| Ollama (local) | Free | Free | $0 (hardware costs) |
Pricing data from Anthropic's official pricing page and OpenRouter. Moderate use assumes ~5-10M tokens/month for daily coding sessions.
"Cost per 1M Output Tokens by Model"
Data table
| "Model" | "Output Cost" |
|---|---|
| "Opus 4.6" | 25 |
| "Sonnet 4.6" | 15 |
| "DeepSeek V3.2" | 1.1 |
| "Gemini 2.5 Flash" | 0.6 |
| "Ollama (local)" | 0 |
The cost gap is significant, but cheaper models produce lower quality code, especially for complex agentic tasks where tool calling reliability matters. DeepSeek at $1.10/MTok output is great for routine edits. For a multi-file refactor where Claude Code needs to read, plan, edit, and verify across 20 files? You'll still want Sonnet or Opus.
Verdict: For cost savings with acceptable quality, OpenRouter + DeepSeek offers the best ratio. For zero cost, Ollama is unbeatable if you have the hardware. For maximum quality, Anthropic direct remains king.
Which Method Should You Use?, Decision Framework
Here's the section every other tutorial skips. Instead of telling you "just use OpenRouter," let's match methods to actual needs:
| If You Need... | Use This Method | Why |
|---|---|---|
| Maximum model variety, one API key | OpenRouter | 300+ models, easy setup, pay-per-use |
| Free, unlimited, code stays local | Ollama | Zero cost, full privacy, offline capable |
| Local models with a visual interface | LM Studio | GUI model browser, easy quantization selection |
| Smart per-task routing | claude-code-router | Route reasoning to Opus, quick edits to DeepSeek |
| Gemini or OpenAI models specifically | claude-code-proxy | Translates Anthropic format to OpenAI/Gemini |
| Enterprise: load balancing, audit | LiteLLM | Rate limiting, fallback chains, team controls |
Should You Even Switch?
Honest take: for most developers doing complex agentic coding, multi-file refactors, architecture planning, debugging tricky concurrency issues, Claude Sonnet via direct Anthropic remains the best option. Native Claude has the most reliable tool calling, the best understanding of its own agentic workflow, and zero proxy latency.
Model switching is an optimization for specific scenarios:
- You're burning through tokens on background tasks that don't need Sonnet quality, or you keep hitting Claude's 2x usage limits
- You need Gemini's 1M context window for a massive monorepo
- Your code can't leave your machine (government, healthcare, finance)
- You're on a tight budget and acceptable quality beats perfect quality
If none of those apply, opusplan (Anthropic's built-in routing) might be all you need.
Tool calling reliability is the quality metric that matters most here. The hierarchy goes: native Claude >> OpenRouter Claude >> DeepSeek/Gemini >> local models. The further down you go, the more you'll see failed tool calls, hallucinated file paths, and incomplete edits.
Troubleshooting Common Issues
"Model not found" error
This is the single most common problem. It happens when you set ANTHROPIC_BASE_URL but forget to map all three model slots. Claude Code tries to call Haiku or Opus through your proxy, the proxy doesn't recognize the default Anthropic model name, and you get the error.
# Fix: set ALL three model slot variables
export ANTHROPIC_DEFAULT_HAIKU_MODEL="your-model"
export ANTHROPIC_DEFAULT_SONNET_MODEL="your-model"
export ANTHROPIC_DEFAULT_OPUS_MODEL="your-model"Connection refused / timeout
Your proxy isn't running, or Claude Code is hitting the wrong port. Verify the proxy process is active and double-check the port in ANTHROPIC_BASE_URL. Firewalls and VPNs can also block localhost connections.
Tool calling failures
The model doesn't support Anthropic's tool use format properly. Symptoms: Claude Code hangs, produces empty tool calls, or edits the wrong files. Switch to a model with verified tool calling support, Qwen 2.5 Coder and devstral handle it best among local models.
Streaming issues
Some proxies don't handle server-sent events correctly. Responses appear all at once instead of streaming, or they truncate mid-response. Make sure your proxy version is current and check its GitHub issues for known streaming bugs.
Context window exceeded
Local models often default to 4K-8K context windows. Claude Code needs much more than that for real coding sessions. Use models with 32K+ context, or route long-context tasks to Gemini (1M window) through OpenRouter.
Must log out first for OpenRouter
If you're switching from direct Anthropic to OpenRouter, run claude /logout first. Claude Code caches authentication and will keep trying to use your Anthropic key even after you change the base URL.
# Clear cached authentication before switching providers
claude /logoutFAQ
Can you use Claude Code with GPT-4 or Gemini?
Yes. claude-code-proxy translates Anthropic-format requests to OpenAI or Gemini format. You can also access both through OpenRouter with a single API key. Tool calling may be less reliable than native Claude, especially for complex multi-step tasks.
How do I use Claude Code without an Anthropic API key?
Use Ollama for fully free local usage, no API key from any provider needed. Alternatively, use OpenRouter with their API key format. You still set the ANTHROPIC_API_KEY environment variable, but it points to your proxy's key (e.g., sk-or-v1-... for OpenRouter, "ollama" for Ollama).
How do I set up Claude Code with Ollama?
Install Ollama, pull a model like qwen2.5-coder:14b, run ollama launch claude, and set three environment variables: ANTHROPIC_BASE_URL, ANTHROPIC_API_KEY, and the three model slot vars. See the Ollama section above for the complete code.
What models work best with Claude Code?
For cloud: Claude Sonnet 4 delivers the best coding quality, while DeepSeek V3.2 offers the best value. For local: Qwen 2.5 Coder 14B is the current gold standard. The deciding factor is tool calling reliability, Claude Code depends heavily on it for file operations, and native Claude models handle it best.
How do I fix the "model not found" error in Claude Code?
Set all three model slot environment variables: ANTHROPIC_DEFAULT_HAIKU_MODEL, ANTHROPIC_DEFAULT_SONNET_MODEL, and ANTHROPIC_DEFAULT_OPUS_MODEL. This error happens because Claude Code uses three internal model slots, and an unmapped slot tries to call a model name your proxy doesn't recognize.
Can Claude Code work offline with local models?
Yes. Both Ollama and LM Studio run models entirely on your machine with no internet connection required. Your code never leaves your hardware. You'll need adequate specs -- 16GB RAM minimum for 7B models, 32GB+ for anything larger.
Can I use LM Studio with Claude Code?
Yes. LM Studio runs a local server that Claude Code connects to by setting ANTHROPIC_BASE_URL to http://localhost:1234/v1 and ANTHROPIC_API_KEY to lm-studio. Download a coding model like Qwen 2.5 Coder, start the server from LM Studio's GUI, then map your model slot. It's the visual alternative to Ollama's CLI.
What is ANTHROPIC_BASE_URL and how do I use it?
It's the environment variable that tells Claude Code where to send API requests. By default, it points to https://api.anthropic.com. Change it to any Anthropic-compatible endpoint, OpenRouter (https://openrouter.ai/api), Ollama (http://localhost:11434), LiteLLM (http://localhost:4000), etc.
Is claude-code-router better than LiteLLM?
Different tools for different problems. claude-code-router is simpler (npm install, edit config.json, done) and designed specifically for Claude Code. LiteLLM is enterprise-grade with load balancing, rate limiting, fallback chains, and audit logging. Solo developer? Use claude-code-router. Team with governance needs? Use LiteLLM.
How much can I save using non-Anthropic models?
Switching from Claude Opus 4.6 to DeepSeek V3.2 cuts output token costs by about 95% ($25 vs $1.10 per million tokens). Ollama brings that to zero if you have the hardware. But quality trade-offs are real, expect lower accuracy on complex multi-step agentic tasks, less reliable tool calling, and more hallucinated file paths.
Does tool calling work with all proxy methods?
Not reliably across the board. Native Claude has the best tool calling support. OpenRouter with Claude models works almost as well. DeepSeek and Gemini have partial support, simple tool calls work, but complex chains can fail. Local models via Ollama have the least reliable tool calling. This is the number one quality concern when switching away from Anthropic.