claude-code-router使用文档

circle-info

本文档介绍如何安装、配置和使用 Claude Code Router,以实现 Claude Code 多模型路由和灵活集成。

1. 简介

Claude Code 是 Anthropic 公司开发的强大 AI 编码助手,可直接在终端和 IDE 中使用,帮助开发者提高编码效率。Claude Code Router 是一个为 Claude Code 提供增强功能的代理工具。它可以让你在使用 Claude Code 时,自由地路由到不同的 AI 模型提供商,而不仅仅是 Anthropic。

  • 主要功能

    • 模型路由:按场景选择不同模型(默认、后台、思考模式、长上下文)。

    • 多厂商支持:支持 OpenRouter、DeepSeek、Ollama、Gemini、Volcengine、SiliconFlow 等。

    • 请求/响应转换:通过 Transformer 插件对不同 API 的输入输出进行转换。

    • 动态切换模型:在会话中使用 /model 命令切换模型。

    • CI/CD 集成:支持 GitHub Actions 等自动化工作流。

    • 插件系统:可扩展自定义路由逻辑和转换规则。

2. 本地安装

  • 安装 Claude Code

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

  • 安装 Router

3. Docker 安装

  • 拉取项目并进入目录

  • 3.2 修改 Dockerfile

  • 修改 docker-compose.yml(根据实际需求)

  • 启动 Docker 容器

  • 进入 Docker 容器

4. 配置

安装后,需要在本地创建配置文件:~/.claude-code-router/config.json

  • 配置示例

  • 4.2 字段说明

    字段名称
    说明

    Providers

    定义可用的模型提供商,每个提供商可有多个模型,可在 UI 页面中配置。

    └ name

    提供商别名

    └ type

    API 兼容模式(通常为 openai-compatible)

    └ base_url

    API 请求地址

    └ api_key

    API Key(可用环境变量 ${VAR_NAME})

    Router

    定义不同场景对应的默认模型。

    └ default

    普通对话的默认模型

    └ background

    后台运行任务用的模型

    └ think

    需要推理时使用的模型

    └ longContext

    需要长上下文时使用的模型

    HOST

    启动服务绑定的地址,默认 127.0.0.1。如需对外提供服务,建议设置 APIKEY来保护。

    LOG / LOG_LEVEL

    日志开关和级别,支持 error / warn / info / debug。

    NON_INTERACTIVE_MODE

    设置为 true 时启用非交互模式,适合 CI/CD、GitHub Actions 等场景。

注:更多详细配置请查看官方文档。

5. 使用方法

  • 打开 UI 配置页

在浏览器打开:http://ip:port/ui,可直接在网页端可视化配置 Claude-code-router。

  • 启动 Router

  • 在对话中动态切换模型

6. 参考

  • claude-code-router musistudio/claude-code-router: Use Claude Code as the foundation for coding infrastructure, allowing you to decide how to interact with the model while enjoying updates from Anthropic.

  • claude-code anthropics/claude-code: Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

最后更新于