Login

Claude Code Router, allows routing Claude Code requests to different AI models, supporting multi-model collaboration and cost optimization.

MITTypeScript 5.1kmusistudioclaude-code-router Last Updated: 2025-07-17

Claude Code Router Project Introduction

Project Overview

Claude Code Router is an open-source project designed to route Claude Code requests to different AI models. It serves as the foundation for coding infrastructure, allowing users to decide how to interact with models while enjoying updates from Anthropic. The core goal of this project is to significantly reduce the cost of using Claude Code through intelligent routing and multi-model collaboration.

Project Motivation

Traditional AI programming tools suffer from the following issues:

  • High Costs: Directly using high-end models like Claude for all tasks is very expensive.
  • Model Limitations: A single model cannot perform optimally in all scenarios.
  • Resource Waste: Complex models are used even for simple tasks, leading to resource waste.

Claude Code Router addresses these problems through intelligent routing. The author noted that when using Claude-4 in VSCode Copilot to handle Flutter issues, three rounds of conversation messed up the files, requiring a rollback. In contrast, using Claude Code with DeepSeek, a task was successfully completed after three or four rounds of conversation, costing less than 1 RMB.

Core Features

1. Dual-Mode Operation

  • Normal Mode: Uses a single model to complete all tasks.
  • Router Mode: Uses multiple specialized models to collaborate on different types of tasks.

2. Intelligent Model Specialization

In Router Mode, the system uses four different specialized models:

  • Router Agent: qwen2.5-coder-3b

    • Responsible for request routing and dispatch.
    • Does not require high intelligence; a small model is sufficient.
    • Currently uses Alibaba Cloud's official free qwen2.5-coder-3b model.
  • Tool Agent: qwen-max-0125

    • Responsible for tool calling.
    • Must support function calling.
    • Needs to support a sufficiently large tool description length (ideally greater than 1754).
  • Coder Agent: deepseek-v3

    • Specifically responsible for code generation tasks.
    • Uses DeepSeek series models.
  • Think Agent: deepseek-r1

    • Responsible for complex reasoning tasks.
    • Uses the DeepSeek R1 reasoning model.

3. Cost Optimization

  • Benefits from Alibaba Cloud's free qwen2.5-coder-3b model and DeepSeek's KV-Cache technology.
  • Can significantly reduce the cost of using Claude Code.
  • Avoids using expensive models for simple tasks through task specialization.

Installation and Configuration

1. Install Claude Code

npm install -g @anthropic-ai/claude-code

2. Clone Project

git clone https://github.com/musistudio/claude-code-reverse.git

3. Install Dependencies

npm i

4. Configure Environment Variables

Normal Mode Configuration

# Disable router
ENABLE_ROUTER=false
OPENAI_API_KEY=""
OPENAI_BASE_URL=""
OPENAI_MODEL=""

Router Mode Configuration

# Enable router
ENABLE_ROUTER=true
export TOOL_AGENT_API_KEY=""
export TOOL_AGENT_BASE_URL=""
export TOOL_AGENT_MODEL="qwen-max-2025-01-25"
export CODER_AGENT_API_KEY=""
export CODER_AGENT_BASE_URL="https://api.deepseek.com"
export CODER_AGENT_MODEL="deepseek-chat"
export THINK_AGENT_API_KEY=""
export THINK_AGENT_BASE_URL="https://api.deepseek.com"
export THINK_AGENT_MODEL="deepseek-reasoner"
export ROUTER_AGENT_API_KEY=""
export ROUTER_AGENT_BASE_URL=""
export ROUTER_AGENT_MODEL="qwen2.5-coder-3b-instruct"

5. Start Service

node index.mjs

6. Configure Claude Code Environment Variables

export DISABLE_PROMPT_CACHING=1
export ANTHROPIC_AUTH_TOKEN="test"
export ANTHROPIC_BASE_URL="http://127.0.0.1:3456"
export API_TIMEOUT_MS=600000
claude

Technical Architecture

Router Mode Working Principle

  1. Request Reception: Claude Code sends requests to the local proxy server.
  2. Intelligent Routing: The Router Agent analyzes the request type and decides which specialized model to forward it to.
  3. Task Execution:
    • Tool calling tasks → Tool Agent
    • Code generation tasks → Coder Agent
    • Complex reasoning tasks → Think Agent
  4. Result Return: Each model returns the result after completing its task.

Key Advantages

  • Task Separation: Separates tool calling and coding tasks.
  • Model Specialization: Each model focuses on the tasks it excels at.
  • Cost Control: Selects the appropriate model based on task complexity.
  • Reasoning Model Support: Supports reasoning models like R1 that do not support function calling.

Notes

⚠️ Important Reminder:

  • This project is in the testing phase and may consume a large number of tokens.
  • Task execution failures may occur.
  • It is recommended to set appropriate ignorePatterns for the project.

Project Significance

Claude Code Router represents an important direction in the development of AI programming tools:

  • Cost Democratization: Makes AI programming assistants affordable for more developers.
  • Model Diversification: No longer relies on a single model; allows flexible selection of the most suitable model.
  • Architectural Innovation: Enhances overall efficiency through routing and specialized division of labor.

This project provides developers with a powerful tool that can significantly reduce usage costs while maintaining the powerful features of Claude Code, making an important contribution to the popularization of AI programming tools.

Star History Chart