musistudio/claude-code-router
Claude Code Router
A powerful tool to route Claude Code requests to different models and customize any request.
β¨ Features
- Model Routing: Route requests to different models based on your needs (e.g., background tasks, thinking, long context).
- Multi-Provider Support: Supports various model providers like OpenRouter, DeepSeek, Ollama, Gemini, Volcengine, and SiliconFlow.
- Request/Response Transformation: Customize requests and responses for different providers using transformers.
- Dynamic Model Switching: Switch models on-the-fly within Claude Code using the
/model
command. - GitHub Actions Integration: Trigger Claude Code tasks in your GitHub workflows.
- Plugin System: Extend functionality with custom transformers.
π Getting Started
1. Installation
First, ensure you have Claude Code installed:
|
|
Then, install Claude Code Router:
|
|
2. Configuration
Create and configure your ~/.claude-code-router/config.json
file. For more details, you can refer to config.example.json
.
The config.json
file has several key sections:
-
PROXY_URL
(optional): You can set a proxy for API requests, for example:"PROXY_URL": "http://127.0.0.1:7890"
. -
LOG
(optional): You can enable logging by setting it totrue
. The log file will be located at$HOME/.claude-code-router.log
. -
APIKEY
(optional): You can set a secret key to authenticate requests. When set, clients must provide this key in theAuthorization
header (e.g.,Bearer your-secret-key
) or thex-api-key
header. Example:"APIKEY": "your-secret-key"
. -
HOST
(optional): You can set the host address for the server. IfAPIKEY
is not set, the host will be forced to127.0.0.1
for security reasons to prevent unauthorized access. Example:"HOST": "0.0.0.0"
. -
Providers
: Used to configure different model providers. -
Router
: Used to set up routing rules.default
specifies the default model, which will be used for all requests if no other route is configured. -
API_TIMEOUT_MS
: Specifies the timeout for API calls in milliseconds.
Here is a comprehensive example:
|
|
3. Running Claude Code with the Router
Start Claude Code using the router:
|
|
Note: After modifying the configuration file, you need to restart the service for the changes to take effect:
1
ccr restart
4. UI Mode
For a more intuitive experience, you can use the UI mode to manage your configuration:
|
|
This will open a web-based interface where you can easily view and edit your config.json
file.
Providers
The Providers
array is where you define the different model providers you want to use. Each provider object requires:
name
: A unique name for the provider.api_base_url
: The full API endpoint for chat completions.api_key
: Your API key for the provider.models
: A list of model names available from this provider.transformer
(optional): Specifies transformers to process requests and responses.
Transformers
Transformers allow you to modify the request and response payloads to ensure compatibility with different provider APIs.
-
Global Transformer: Apply a transformer to all models from a provider. In this example, the
openrouter
transformer is applied to all models under theopenrouter
provider.1 2 3 4 5 6 7 8 9 10 11
{ "name": "openrouter", "api_base_url": "https://openrouter.ai/api/v1/chat/completions", "api_key": "sk-xxx", "models": [ "google/gemini-2.5-pro-preview", "anthropic/claude-sonnet-4", "anthropic/claude-3.5-sonnet" ], "transformer": { "use": ["openrouter"] } }
-
Model-Specific Transformer: Apply a transformer to a specific model. In this example, the
deepseek
transformer is applied to all models, and an additionaltooluse
transformer is applied only to thedeepseek-chat
model.1 2 3 4 5 6 7 8 9 10
{ "name": "deepseek", "api_base_url": "https://api.deepseek.com/chat/completions", "api_key": "sk-xxx", "models": ["deepseek-chat", "deepseek-reasoner"], "transformer": { "use": ["deepseek"], "deepseek-chat": { "use": ["tooluse"] } } }
-
Passing Options to a Transformer: Some transformers, like
maxtoken
, accept options. To pass options, use a nested array where the first element is the transformer name and the second is an options object.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
{ "name": "siliconflow", "api_base_url": "https://api.siliconflow.cn/v1/chat/completions", "api_key": "sk-xxx", "models": ["moonshotai/Kimi-K2-Instruct"], "transformer": { "use": [ [ "maxtoken", { "max_tokens": 16384 } ] ] } }
Available Built-in Transformers:
deepseek
: Adapts requests/responses for DeepSeek API.gemini
: Adapts requests/responses for Gemini API.openrouter
: Adapts requests/responses for OpenRouter API.groq
: Adapts requests/responses for groq API.maxtoken
: Sets a specificmax_tokens
value.tooluse
: Optimizes tool usage for certain models viatool_choice
.gemini-cli
(experimental): Unofficial support for Gemini via Gemini CLI gemini-cli.js.reasoning
: Used to process thereasoning_content
field.sampling
: Used to process sampling information fields such astemperature
,top_p
,top_k
, andrepetition_penalty
.enhancetool
: Adds a layer of error tolerance to the tool call parameters returned by the LLM (this will cause the tool call information to no longer be streamed).cleancache
: Clears thecache_control
field from requests.vertex-gemini
: Handles the Gemini API using Vertex authentication.
Custom Transformers:
You can also create your own transformers and load them via the transformers
field in config.json
.
|
|
Router
The Router
object defines which model to use for different scenarios:
default
: The default model for general tasks.background
: A model for background tasks. This can be a smaller, local model to save costs.think
: A model for reasoning-heavy tasks, like Plan Mode.longContext
: A model for handling long contexts (e.g., > 60K tokens).longContextThreshold
(optional): The token count threshold for triggering the long context model. Defaults to 60000 if not specified.webSearch
: Used for handling web search tasks and this requires the model itself to support the feature. If youβre using openrouter, you need to add the:online
suffix after the model name.
You can also switch models dynamically in Claude Code with the /model
command:
/model provider_name,model_name
Example: /model openrouter,anthropic/claude-3.5-sonnet
Custom Router
For more advanced routing logic, you can specify a custom router script via the CUSTOM_ROUTER_PATH
in your config.json
. This allows you to implement complex routing rules beyond the default scenarios.
In your config.json
:
|
|
The custom router file must be a JavaScript module that exports an async
function. This function receives the request object and the config object as arguments and should return the provider and model name as a string (e.g., "provider_name,model_name"
), or null
to fall back to the default router.
Here is an example of a custom-router.js
based on custom-router.example.js
:
|
|
π€ GitHub Actions
Integrate Claude Code Router into your CI/CD pipeline. After setting up Claude Code Actions, modify your .github/workflows/claude.yaml
to use the router:
|
|
This setup allows for interesting automations, like running tasks during off-peak hours to reduce API costs.
π Further Reading
β€οΈ Support & Sponsoring
If you find this project helpful, please consider sponsoring its development. Your support is greatly appreciated!
Our Sponsors
A huge thank you to all our sponsors for their generous support!
- @Simon Leischnig
- @duanshuaimin
- @vrgitadmin
- @*o
- @ceilwoo
- @*θ―΄
- @*ζ΄
- @K*g
- @R*R
- @bobleer
- @*θ
- @*ε
- @Clarence-pan
- @carter003
- @S*r
- @*ζ
- @*ζ
- @Z*z
- @*ηΆ
- @cluic
- @*θ
- @PromptExpert
- @*εΊ
- @yusnake
- @*ι£
- @θ£*
- @*ζ±
- @*ζΆ―
- @*:-οΌ
- @**η£
- @*η’
- @*ζ
- @Z*o
- @*η¨
- @congzhangzh
- @*_
- @Z*m
- @*ι«
- @c*y
- @*ζ
(If your name is masked, please contact me via my homepage email to update it with your GitHub username.)