Route each task to the cheapest model that can do it
Send the grunt work to a cheap open model and keep the hard thinking on Claude, automatically, with one small local router.
The outcome
By the end you'll have a router that sends routine, high-volume work to a cheap model and the hard reasoning to a strong one, picking which is which on its own, so you stop overpaying for easy work and stop under-powering the hard work.
- The OpenRouter setup from the open-source-models guide, already working
- Node and npm on your machine (to install one command-line tool)
- An OpenRouter key, and about fifteen minutes
Pointing Claude Code at one open model is already a big saving, but it is a blunt instrument: the same model answers a one-line rename and a gnarly multi-file refactor, so you either overpay on the easy stuff or under-power the hard stuff. A router fixes that. It is a small program that runs on your own machine, sits between Claude Code and the models, looks at each request, sorts it into a tier (quick background task, everyday work, hard thinking, huge context), and forwards it to whichever model you put on that tier. It is the automatic version of the manual split from the open-source-models guide: cheap models for the grunt work, Claude for the thinking, decided for you, request by request.
Note
Do the open-source-models guide first. This one adds a piece that has to keep running in the background, so it is a step up. Reach for it once a single model starts feeling too expensive on easy work or too weak on hard work, not before.
In a hurry? Let Claude set it up
Paste this into a working Claude Code or claude.ai session. It installs the router, writes the config for your models, and starts it, one step at a time.
I want to set up claude-code-router so Claude Code automatically sends cheap, routine work to an open model and hard reasoning to a strong one. Walk me through it on my machine, one step at a time, and wait for me to confirm each step. 1. Confirm I have Node and npm, then help me install the router globally with npm. The package is @musistudio/claude-code-router and the command it gives me is ccr. 2. Help me create ~/.claude-code-router/config.json with a Providers block for OpenRouter (name openrouter, api_base_url https://openrouter.ai/api/v1/chat/completions, my OpenRouter key, a models list, and the transformer use openrouter) and a Router block that maps background to a cheap model, default to z-ai/glm-5.2, think to anthropic/claude-opus-4-8, and longContext to a long-context model with longContextThreshold 60000. 3. Remind me the keys sit in plaintext in that file, so never commit it. 4. Show me how to start it with ccr code, that it runs on 127.0.0.1:3456 and must stay alive, and that after any config edit I run ccr restart because changes are not picked up live. 5. Help me confirm it is routing, then show me how to override the model for one session with /model openrouter,a-model-id. 6. Show me how to stop it (ccr stop) and go back to plain Claude (just run claude instead of ccr code). 7. If I use VS Code, explain running ccr code in the integrated terminal versus pointing the sidebar at the local router. Start by checking Node and npm.
1. Install the router
The router is one small command-line tool, installed once. It needs Node and npm, which most machines set up for modern tools already have (if npm is missing, install Node from nodejs.org first). The install gives you a new command called ccr, short for Claude Code Router. You do not point Claude Code at anything yet; that happens when you start it in step 3.
Install once. It adds a new command, ccr. If npm is unfamiliar, it ships with Node from nodejs.org.npm install -g @musistudio/claude-code-router # you now have the `ccr` command. You'll start it in step 3 with: ccr code
2. Tell it your models and your routing
The router reads one file: ~/.claude-code-router/config.json. It has two parts. Providers is where models come from: a name you make up, the address, your key, the list of model ids you'll use, and a transformer line that reshapes requests for that provider (openrouter for OpenRouter). Router is the actual routing: each tier name points at one provider,model-id pair. The block below is the hybrid setup, a free model for throwaway background calls, GLM for everyday work, Claude for the hard thinking, and a long-context model for anything over the threshold. Swap the ids for whatever you like (copy exact ids from OpenRouter's models page), then save.
~/.claude-code-router/config.json. Background goes cheap, the hard tier stays on Claude. Your keys live here, so never commit this file.{ "Providers": [ { "name": "openrouter", "api_base_url": "https://openrouter.ai/api/v1/chat/completions", "api_key": "sk-or-your-openrouter-key", "models": [ "z-ai/glm-5.2", "anthropic/claude-opus-4-8", "openai/gpt-oss-120b:free" ], "transformer": { "use": ["openrouter"] } } ], "Router": { "default": "openrouter,z-ai/glm-5.2", "background": "openrouter,openai/gpt-oss-120b:free", "think": "openrouter,anthropic/claude-opus-4-8", "longContext": "openrouter,z-ai/glm-5.2", "longContextThreshold": 60000 } }What each tier means, so you can tune the split
- background: the small automatic calls Claude Code fires constantly (summaries, titles, quick lookups) plus low-stakes grunt work. Put the cheapest model here; it runs the most.
- default: your everyday work, anything that doesn't match a more specific tier. A cheap-but-capable model like GLM 5.2 belongs here.
- think: hard reasoning and plan-mode work. This is the tier to keep on a strong model like Claude, even though it costs more, because it runs least and matters most.
- longContext: requests larger than longContextThreshold tokens (60000 above). Point it at a model that handles big context well.
- You override any of these for a single session with the /model command in step 5.
Routing the think tier to anthropic/claude-opus-4-8 still goes through OpenRouter, so it carries OpenRouter's small markup. If you'd rather pay Anthropic directly for that tier, you can add a second provider that points at your Claude account; see the repo for the exact shape.
3. Start Claude Code through the router
One command does it: ccr code starts the router (if it isn't already up) and opens a Claude Code session pointed at it. The router runs locally at 127.0.0.1:3456 and has to stay alive for routing to work, so leave it running. One thing trips everyone up: editing the config does not take effect live. After any change to the file, run ccr restart. The other commands are ccr start (run it in the background), ccr stop, ccr status, and ccr ui (a small web page for editing the config).
ccr code starts the router and launches Claude Code through it, in one step. ›4. Use it in VS Code
Same split as the other guides. In VS Code's integrated terminal, run ccr code and you're routing, exactly like the native terminal. The chat sidebar does not go through ccr, so to route it you keep the router running with ccr start and point the extension at the local router by setting ANTHROPIC_BASE_URL to the router's address in claudeCode.environmentVariables, then reload the window. If that's more bother than it's worth, just use the integrated terminal, which needs none of it.
To route the VS Code sidebar too: keep the router running (ccr start), add this to VS Code's own settings, then reload the window.// VS Code settings.json (the router must be running: ccr start) { "claudeCode.environmentVariables": [ { "name": "ANTHROPIC_BASE_URL", "value": "http://127.0.0.1:3456" } ] }5. Switch on the fly, and switch back
To override the route for one session without touching the config, use /model with the same provider,model-id form: handy when you want one task on a bigger model. To stop routing entirely, run ccr stop and go back to a plain claude session, which talks to Claude directly again; a normal session never goes through the router. Your config file stays put for next time, so turning the router back on is just ccr code.
Override for one session, or stop the router and return to plain Claude. ›
Watch out
Cheap models earn their keep on background and routine work, but they fumble real code edits, producing malformed diffs and half-applied changes. Keep default and think on a capable model, and reserve the cheapest one for the background tier. As always, prompt caching may not survive the trip, so watch the provider's own dashboard for the real cost.
Note
Your API keys sit in plaintext in ~/.claude-code-router/config.json. Never commit it or drop it in a synced folder. And remember the two-part rhythm: edit the file, then ccr restart, or the change is ignored.
Tip
If you only ever reach for one model, you don't need this. The single-file setup in the open-source-models guide is simpler and has nothing to keep alive. The router pays off when your work genuinely splits into cheap-and-frequent versus hard-and-rare.