@wengine-ai/claude-code-router-next 2.1.1 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +389 -340
- package/dist/cli.js +251 -251
- package/dist/index.html +19 -21
- package/package.json +3 -6
package/README.md
CHANGED
|
@@ -1,57 +1,58 @@
|
|
|
1
1
|

|
|
2
2
|
|
|
3
|
-
[** | **[🇬🇧 English](README_en.md)** | [](https://www.npmjs.com/package/@wengine-ai/claude-code-router-next)
|
|
4
|
+
|
|
5
|
+
> **说明**:原版 [claude-code-router](https://github.com/musistudio/claude-code-router) 仓库已不再活跃维护。本项目是基于原仓库 fork 的社区活跃版本,持续进行 Bug 修复、功能开发和长期维护。
|
|
6
6
|
|
|
7
7
|
<hr>
|
|
8
8
|
|
|
9
|
-
> [
|
|
9
|
+
> [从CLI工具风格看工具渐进式披露](/blog/zh/从CLI工具风格看工具渐进式披露.md)
|
|
10
10
|
|
|
11
|
-
>
|
|
11
|
+
> 一款强大的工具,可将 Claude Code 请求路由到不同的模型,并自定义任何请求。
|
|
12
12
|
|
|
13
13
|

|
|
14
14
|
|
|
15
|
-
## ✨ Features
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
-
|
|
20
|
-
-
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
16
|
+
## ✨ 功能
|
|
17
|
+
|
|
18
|
+
- **模型路由**: 根据您的需求将请求路由到不同的模型(例如,后台任务、思考、长上下文)。
|
|
19
|
+
- **多提供商支持**: 支持 OpenRouter、DeepSeek、Ollama、Gemini、Volcengine 和 SiliconFlow 等各种模型提供商。
|
|
20
|
+
- **请求/响应转换**: 使用转换器为不同的提供商自定义请求和响应。
|
|
21
|
+
- **动态模型切换**: 在 Claude Code 中使用 `/model` 命令动态切换模型。
|
|
22
|
+
- **GitHub Actions 集成**: 在您的 GitHub 工作流程中触发 Claude Code 任务。
|
|
23
|
+
- **用量统计与限额监控**: 追踪请求的 Token 数、缓存命中率、首 Token 延迟 (TTFT) 和生成速度,并实时展示主流服务商(如智谱、Qwen 等)的限额使用情况。
|
|
24
|
+
- **插件系统**: 使用自定义转换器扩展功能。
|
|
24
25
|
|
|
25
|
-
## 🚀
|
|
26
|
+
## 🚀 快速入门
|
|
26
27
|
|
|
27
|
-
### 1.
|
|
28
|
+
### 1. 安装
|
|
28
29
|
|
|
29
|
-
|
|
30
|
+
您可以从 npm 官方仓库安装 Claude Code Router,或者直接从本 GitHub 仓库安装以获取最新的开发版本。
|
|
30
31
|
|
|
31
|
-
####
|
|
32
|
+
#### 选项 A:从 npm 官方仓库安装(稳定版)
|
|
32
33
|
|
|
33
|
-
|
|
34
|
+
首先,请确保您已安装 [Claude Code](https://docs.anthropic.com/en/docs/claude-code/quickstart):
|
|
34
35
|
|
|
35
36
|
```shell
|
|
36
37
|
npm install -g @anthropic-ai/claude-code
|
|
37
38
|
```
|
|
38
39
|
|
|
39
|
-
|
|
40
|
+
然后,安装 Claude Code Router:
|
|
40
41
|
|
|
41
42
|
```shell
|
|
42
|
-
npm install -g claude-code-router-next
|
|
43
|
+
npm install -g @wengine-ai/claude-code-router-next
|
|
43
44
|
```
|
|
44
45
|
|
|
45
|
-
####
|
|
46
|
+
#### 选项 B:从 GitHub 安装(最新开发版)
|
|
46
47
|
|
|
47
|
-
|
|
48
|
+
如果您想直接使用源码中的最新功能和修复:
|
|
48
49
|
|
|
49
|
-
1.
|
|
50
|
+
1. **先卸载已安装的全局版本**(以避免指令冲突):
|
|
50
51
|
```shell
|
|
51
|
-
npm uninstall -g claude-code-router-next @musistudio/claude-code-router @wengine-ai/claude-code-router
|
|
52
|
+
npm uninstall -g @wengine-ai/claude-code-router-next @musistudio/claude-code-router @wengine-ai/claude-code-router
|
|
52
53
|
```
|
|
53
54
|
|
|
54
|
-
2.
|
|
55
|
+
2. **克隆本仓库并在本地进行 Link**(推荐开发者使用):
|
|
55
56
|
```shell
|
|
56
57
|
git clone https://github.com/xiaoliu10/claude-code-router-next.git
|
|
57
58
|
cd claude-code-router-next
|
|
@@ -60,67 +61,46 @@ If you want to use the latest features and bug fixes directly from the source co
|
|
|
60
61
|
npm link
|
|
61
62
|
```
|
|
62
63
|
|
|
63
|
-
|
|
64
|
+
*或者直接从 GitHub 进行全局安装:*
|
|
64
65
|
```shell
|
|
65
66
|
npm install -g github:xiaoliu10/claude-code-router-next
|
|
66
67
|
```
|
|
67
68
|
|
|
68
|
-
#### 🔄
|
|
69
|
+
#### 🔄 从官方原版社区版迁移 (@musistudio/claude-code-router)
|
|
69
70
|
|
|
70
|
-
|
|
71
|
+
如果您当前正在使用官方原版社区版本 `@musistudio/claude-code-router` 或之前的版本 `@wengine-ai/claude-code-router`,希望切换到 `@wengine-ai/claude-code-router-next`:
|
|
71
72
|
|
|
72
|
-
1.
|
|
73
|
+
1. **先卸载旧版本**:
|
|
73
74
|
```shell
|
|
74
|
-
npm uninstall -g @musistudio/claude-code-router
|
|
75
|
+
npm uninstall -g @musistudio/claude-code-router @wengine-ai/claude-code-router
|
|
75
76
|
```
|
|
76
77
|
|
|
77
|
-
2.
|
|
78
|
+
2. **安装本仓库增强版本**:
|
|
78
79
|
```shell
|
|
79
|
-
npm install -g claude-code-router-next
|
|
80
|
+
npm install -g @wengine-ai/claude-code-router-next
|
|
80
81
|
```
|
|
81
82
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
Create and configure your `~/.claude-code-router/config.json` file. For more details, you can refer to `config.example.json`.
|
|
85
|
-
|
|
86
|
-
The `config.json` file has several key sections:
|
|
83
|
+
> **说明**:卸载旧包**不会影响**您已有的配置文件 `~/.claude-code-router/config.json`,新版本会自动读取原有配置。
|
|
87
84
|
|
|
88
|
-
|
|
89
|
-
- **`LOG`** (optional): You can enable logging by setting it to `true`. When set to `false`, no log files will be created. Default is `true`.
|
|
90
|
-
- **`LOG_LEVEL`** (optional): Set the logging level. Available options are: `"fatal"`, `"error"`, `"warn"`, `"info"`, `"debug"`, `"trace"`. Default is `"debug"`.
|
|
91
|
-
- **Logging Systems**: The Claude Code Router uses two separate logging systems:
|
|
92
|
-
- **Server-level logs**: HTTP requests, API calls, and server events are logged using pino in the `~/.claude-code-router/logs/` directory with filenames like `ccr-*.log`
|
|
93
|
-
- **Application-level logs**: Routing decisions and business logic events are logged in `~/.claude-code-router/claude-code-router.log`
|
|
94
|
-
- **`APIKEY`** (optional): You can set a secret key to authenticate requests. When set, clients must provide this key in the `Authorization` header (e.g., `Bearer your-secret-key`) or the `x-api-key` header. Example: `"APIKEY": "your-secret-key"`.
|
|
95
|
-
- **`HOST`** (optional): You can set the host address for the server. If `APIKEY` is not set, the host will be forced to `127.0.0.1` for security reasons to prevent unauthorized access. Example: `"HOST": "0.0.0.0"`.
|
|
96
|
-
- **`NON_INTERACTIVE_MODE`** (optional): When set to `true`, enables compatibility with non-interactive environments like GitHub Actions, Docker containers, or other CI/CD systems. This sets appropriate environment variables (`CI=true`, `FORCE_COLOR=0`, etc.) and configures stdin handling to prevent the process from hanging in automated environments. Example: `"NON_INTERACTIVE_MODE": true`.
|
|
85
|
+
### 2. 配置
|
|
97
86
|
|
|
98
|
-
-
|
|
99
|
-
- **`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.
|
|
100
|
-
- **`API_TIMEOUT_MS`**: Specifies the timeout for API calls in milliseconds.
|
|
87
|
+
创建并配置您的 `~/.claude-code-router/config.json` 文件。有关更多详细信息,您可以参考 `config.example.json`。
|
|
101
88
|
|
|
102
|
-
|
|
89
|
+
`config.json` 文件有几个关键部分:
|
|
90
|
+
- **`PROXY_URL`** (可选): 您可以为 API 请求设置代理,例如:`"PROXY_URL": "http://127.0.0.1:7890"`。
|
|
91
|
+
- **`LOG`** (可选): 您可以通过将其设置为 `true` 来启用日志记录。当设置为 `false` 时,将不会创建日志文件。默认值为 `true`。
|
|
92
|
+
- **`LOG_LEVEL`** (可选): 设置日志级别。可用选项包括:`"fatal"`、`"error"`、`"warn"`、`"info"`、`"debug"`、`"trace"`。默认值为 `"debug"`。
|
|
93
|
+
- **日志系统**: Claude Code Router 使用两个独立的日志系统:
|
|
94
|
+
- **服务器级别日志**: HTTP 请求、API 调用和服务器事件使用 pino 记录在 `~/.claude-code-router/logs/` 目录中,文件名类似于 `ccr-*.log`
|
|
95
|
+
- **应用程序级别日志**: 路由决策和业务逻辑事件记录在 `~/.claude-code-router/claude-code-router.log` 文件中
|
|
96
|
+
- **`APIKEY`** (可选): 您可以设置一个密钥来进行身份验证。设置后,客户端请求必须在 `Authorization` 请求头 (例如, `Bearer your-secret-key`) 或 `x-api-key` 请求头中提供此密钥。例如:`"APIKEY": "your-secret-key"`。
|
|
97
|
+
- **`HOST`** (可选): 您可以设置服务的主机地址。如果未设置 `APIKEY`,出于安全考虑,主机地址将强制设置为 `127.0.0.1`,以防止未经授权的访问。例如:`"HOST": "0.0.0.0"`。
|
|
98
|
+
- **`NON_INTERACTIVE_MODE`** (可选): 当设置为 `true` 时,启用与非交互式环境(如 GitHub Actions、Docker 容器或其他 CI/CD 系统)的兼容性。这会设置适当的环境变量(`CI=true`、`FORCE_COLOR=0` 等)并配置 stdin 处理,以防止进程在自动化环境中挂起。例如:`"NON_INTERACTIVE_MODE": true`。
|
|
99
|
+
- **`Providers`**: 用于配置不同的模型提供商。
|
|
100
|
+
- **`Router`**: 用于设置路由规则。`default` 指定默认模型,如果未配置其他路由,则该模型将用于所有请求。
|
|
101
|
+
- **`API_TIMEOUT_MS`**: API 请求超时时间,单位为毫秒。
|
|
103
102
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
```json
|
|
107
|
-
{
|
|
108
|
-
"OPENAI_API_KEY": "$OPENAI_API_KEY",
|
|
109
|
-
"GEMINI_API_KEY": "${GEMINI_API_KEY}",
|
|
110
|
-
"Providers": [
|
|
111
|
-
{
|
|
112
|
-
"name": "openai",
|
|
113
|
-
"api_base_url": "https://api.openai.com/v1/chat/completions",
|
|
114
|
-
"api_key": "$OPENAI_API_KEY",
|
|
115
|
-
"models": ["gpt-5", "gpt-5-mini"]
|
|
116
|
-
}
|
|
117
|
-
]
|
|
118
|
-
}
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
This allows you to keep sensitive API keys in environment variables instead of hardcoding them in configuration files. The interpolation works recursively through nested objects and arrays.
|
|
122
|
-
|
|
123
|
-
Here is a comprehensive example:
|
|
103
|
+
这是一个综合示例:
|
|
124
104
|
|
|
125
105
|
```json
|
|
126
106
|
{
|
|
@@ -239,330 +219,301 @@ Here is a comprehensive example:
|
|
|
239
219
|
}
|
|
240
220
|
```
|
|
241
221
|
|
|
242
|
-
### 🔑
|
|
222
|
+
### 🔑 阿里云百炼用量 Token (Cookie) 获取引导
|
|
243
223
|
|
|
244
|
-
|
|
224
|
+
如果您想让 Claude Code Router 的后台 Web UI 实时拉取并可视化展示您的 **Qwen Coding Plan(Qwen 编程时限套餐)** 剩余用量额度条,您需要获取控制台的浏览器 `Cookie` 作为 `quotaToken` 填入配置:
|
|
245
225
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
3. Copy your API Key.
|
|
226
|
+
1. 登录 [阿里云百炼控制台](https://bailian.console.aliyun.com/)。
|
|
227
|
+
2. 按键盘 `F12` 打开浏览器开发者工具,并切换到 **Network (网络)** 标签页。
|
|
228
|
+
3. 点击页面用量模块右上角的 **用量刷新**(旋转循环箭头)按钮。
|
|
229
|
+
4. 在左侧网络请求列表中,找到一个以 `api.json?action=BroadScope...` 开头的接口调用请求并点击。
|
|
230
|
+
5. 在右侧 **Headers (标头)** 的 **Request Headers (请求头)** 中找到 **`Cookie`** 这一项,将其右侧的完整超长内容复制下来。
|
|
231
|
+
6. 在您的 `config.json` 中,将这个 cookie 填入阿里云 provider 下的 **`quotaToken`** 属性中即可!
|
|
253
232
|
|
|
254
|
-
|
|
233
|
+

|
|
255
234
|
|
|
256
|
-
|
|
257
|
-
* **Platform**: Alibaba Cloud Bailian (highly capable Qwen-Coder models)
|
|
258
|
-
* **Link**: [Alibaba Cloud Bailian Console](https://bailian.console.aliyun.com/)
|
|
259
|
-
* **Acquisition Methods**:
|
|
260
|
-
* **Method A: Standard Developer API Key (For Request Routing)**
|
|
261
|
-
1. Log in to Alibaba Cloud and enable the "Bailian" model service.
|
|
262
|
-
2. Go to the Bailian Console, click on your **profile icon** in the top-right corner.
|
|
263
|
-
3. Click on **API-KEY** in the dropdown.
|
|
264
|
-
4. Click **Create New API-KEY** and copy the generated key.
|
|
265
|
-
* **Method B: Aliyun Console Cookie (For Quota Visualization in UI)**
|
|
266
|
-
If you want the Claude Code Router UI to fetch and display your monthly **Qwen Coding Plan** quota progress bars, you need to configure your console session `Cookie` as `quotaToken` in your configuration:
|
|
267
|
-
1. Log in to the [Alibaba Cloud Bailian Console](https://bailian.console.aliyun.com/).
|
|
268
|
-
2. Open your browser's Developer Tools (F12) and switch to the **Network** tab.
|
|
269
|
-
3. Click the **Refresh** (用量刷新) button on the console's usage cards.
|
|
270
|
-
4. Look for an API request starting with `api.json?action=BroadScope...` in the network log.
|
|
271
|
-
5. Select the request, find the **`Cookie`** header under **Request Headers**, and copy its entire value.
|
|
272
|
-
6. Paste this copied cookie string as the **`quotaToken`** property inside the Alibaba Cloud provider block in your `config.json`.
|
|
235
|
+
配置成功后,Web UI 的 Provider 列表中将会实时展示您的套餐剩余用量额度条与刷新状态:
|
|
273
236
|
|
|
274
|
-
|
|
237
|
+

|
|
275
238
|
|
|
276
|
-
### 3.
|
|
239
|
+
### 3. 使用 Router 运行 Claude Code
|
|
277
240
|
|
|
278
|
-
|
|
241
|
+
使用 router 启动 Claude Code:
|
|
279
242
|
|
|
280
243
|
```shell
|
|
281
244
|
ccr code
|
|
282
245
|
```
|
|
283
246
|
|
|
284
|
-
>
|
|
285
|
-
>
|
|
247
|
+
> **注意**: 修改配置文件后,需要重启服务使配置生效:
|
|
286
248
|
> ```shell
|
|
287
249
|
> ccr restart
|
|
288
250
|
> ```
|
|
289
251
|
|
|
290
|
-
### 4. UI
|
|
252
|
+
### 4. UI 模式
|
|
291
253
|
|
|
292
|
-
|
|
254
|
+
为了获得更直观的体验,您可以使用 UI 模式来管理您的配置:
|
|
293
255
|
|
|
294
256
|
```shell
|
|
295
257
|
ccr ui
|
|
296
258
|
```
|
|
297
259
|
|
|
298
|
-
|
|
260
|
+
这将打开一个基于 Web 的界面,您可以在其中轻松查看和编辑您的 `config.json` 文件。
|
|
299
261
|
|
|
300
262
|

|
|
301
263
|
|
|
302
|
-
####
|
|
264
|
+
#### 用量统计
|
|
265
|
+
|
|
266
|
+
仪表盘在主页面底部内置了**用量统计**面板。当您的请求通过 Claude Code Router 进行路由时,系统会自动收集用量记录并在 UI 界面中展示。
|
|
303
267
|
|
|
304
|
-
|
|
268
|
+
您可以使用它来查看:
|
|
305
269
|
|
|
306
|
-
|
|
270
|
+
- 总请求数
|
|
271
|
+
- 输入和输出 Token 数
|
|
272
|
+
- 平均首 Token 延迟 (TTFT)
|
|
273
|
+
- 平均生成速度 (Tokens/秒)
|
|
274
|
+
- 请求成功率
|
|
275
|
+
- 每日用量图表
|
|
276
|
+
- 支持筛选和分页的详细请求历史记录
|
|
307
277
|
|
|
308
|
-
-
|
|
309
|
-
- Input and output tokens
|
|
310
|
-
- Average TTFT
|
|
311
|
-
- Average generation speed
|
|
312
|
-
- Success rate
|
|
313
|
-
- Daily usage chart
|
|
314
|
-
- Detailed request records with filters and pagination
|
|
278
|
+

|
|
315
279
|
|
|
316
|
-
|
|
280
|
+
如何使用:
|
|
317
281
|
|
|
318
|
-
1.
|
|
319
|
-
2.
|
|
320
|
-
3.
|
|
321
|
-
4.
|
|
282
|
+
1. 使用 `ccr start` 启动路由器服务
|
|
283
|
+
2. 使用 `ccr ui` 打开 Web 界面
|
|
284
|
+
3. 通过 Claude Code Router 发送请求(例如使用 `ccr code`)
|
|
285
|
+
4. 返回主仪表盘,查看**用量统计**面板
|
|
322
286
|
|
|
323
|
-
|
|
287
|
+
用量数据保存在:
|
|
324
288
|
|
|
325
289
|
```shell
|
|
326
290
|
~/.claude-code-router/data/usage.jsonl
|
|
327
291
|
```
|
|
328
292
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
If the `token-speed` plugin is enabled, the panel will also show TTFT and tokens-per-second metrics. Without that plugin, token counts and request statistics still work, but TTFT and speed may appear as `-`.
|
|
332
|
-
|
|
333
|
-
For API-based access, Claude Code Router also provides:
|
|
334
|
-
|
|
335
|
-
- `GET /api/usage` — paginated records with summary
|
|
336
|
-
- `GET /api/usage/summary` — summary only
|
|
337
|
-
- `DELETE /api/usage` — clear usage data
|
|
293
|
+
### 5. CLI 模型管理
|
|
338
294
|
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
For users who prefer terminal-based workflows, you can use the interactive CLI model selector:
|
|
295
|
+
对于偏好终端工作流的用户,可以使用交互式 CLI 模型选择器:
|
|
342
296
|
|
|
343
297
|
```shell
|
|
344
298
|
ccr model
|
|
345
299
|
```
|
|
346
|
-

|
|
347
300
|
|
|
348
|
-
|
|
301
|
+
该命令提供交互式界面来:
|
|
349
302
|
|
|
350
|
-
-
|
|
351
|
-
-
|
|
352
|
-
-
|
|
353
|
-
-
|
|
354
|
-
-
|
|
355
|
-
-
|
|
356
|
-
- API
|
|
357
|
-
-
|
|
358
|
-
- Transformer
|
|
359
|
-
-
|
|
360
|
-
- Transformer
|
|
361
|
-
-
|
|
303
|
+
- 查看当前配置
|
|
304
|
+
- 查看所有配置的模型(default、background、think、longContext、webSearch、image)
|
|
305
|
+
- 切换模型:快速更改每个路由器类型使用的模型
|
|
306
|
+
- 添加新模型:向现有提供商添加模型
|
|
307
|
+
- 创建新提供商:设置完整的提供商配置,包括:
|
|
308
|
+
- 提供商名称和 API 端点
|
|
309
|
+
- API 密钥
|
|
310
|
+
- 可用模型
|
|
311
|
+
- Transformer 配置,支持:
|
|
312
|
+
- 多个转换器(openrouter、deepseek、gemini 等)
|
|
313
|
+
- Transformer 选项(例如,带自定义限制的 maxtoken)
|
|
314
|
+
- 特定于提供商的路由(例如,OpenRouter 提供商偏好)
|
|
362
315
|
|
|
363
|
-
|
|
316
|
+
CLI 工具验证所有输入并提供有用的提示来引导您完成配置过程,使管理复杂的设置变得容易,无需手动编辑 JSON 文件。
|
|
364
317
|
|
|
365
|
-
### 6.
|
|
318
|
+
### 6. 预设管理
|
|
366
319
|
|
|
367
|
-
|
|
320
|
+
预设允许您轻松保存、共享和重用配置。您可以将当前配置导出为预设,并从文件或 URL 安装预设。
|
|
368
321
|
|
|
369
322
|
```shell
|
|
370
|
-
#
|
|
323
|
+
# 将当前配置导出为预设
|
|
371
324
|
ccr preset export my-preset
|
|
372
325
|
|
|
373
|
-
#
|
|
374
|
-
ccr preset export my-preset --description "
|
|
326
|
+
# 使用元数据导出
|
|
327
|
+
ccr preset export my-preset --description "我的 OpenAI 配置" --author "您的名字" --tags "openai,生产环境"
|
|
375
328
|
|
|
376
|
-
#
|
|
329
|
+
# 从本地目录安装预设
|
|
377
330
|
ccr preset install /path/to/preset
|
|
378
331
|
|
|
379
|
-
#
|
|
332
|
+
# 列出所有已安装的预设
|
|
380
333
|
ccr preset list
|
|
381
334
|
|
|
382
|
-
#
|
|
335
|
+
# 显示预设信息
|
|
383
336
|
ccr preset info my-preset
|
|
384
337
|
|
|
385
|
-
#
|
|
338
|
+
# 删除预设
|
|
386
339
|
ccr preset delete my-preset
|
|
387
340
|
```
|
|
388
341
|
|
|
389
|
-
|
|
390
|
-
-
|
|
391
|
-
-
|
|
392
|
-
-
|
|
393
|
-
-
|
|
394
|
-
-
|
|
342
|
+
**预设功能:**
|
|
343
|
+
- **导出**:将当前配置保存为预设目录(包含 manifest.json)
|
|
344
|
+
- **安装**:从本地目录安装预设
|
|
345
|
+
- **敏感数据处理**:导出期间自动清理 API 密钥和其他敏感数据(标记为 `{{field}}` 占位符)
|
|
346
|
+
- **动态配置**:预设可以包含输入架构,用于在安装期间收集所需信息
|
|
347
|
+
- **版本控制**:每个预设包含版本元数据,用于跟踪更新
|
|
395
348
|
|
|
396
|
-
|
|
349
|
+
**预设文件结构:**
|
|
397
350
|
```
|
|
398
351
|
~/.claude-code-router/presets/
|
|
399
352
|
├── my-preset/
|
|
400
|
-
│ └── manifest.json #
|
|
353
|
+
│ └── manifest.json # 包含配置和元数据
|
|
401
354
|
```
|
|
402
355
|
|
|
403
|
-
### 7. Activate
|
|
356
|
+
### 7. Activate 命令(环境变量设置)
|
|
404
357
|
|
|
405
|
-
|
|
358
|
+
`activate` 命令允许您在 shell 中全局设置环境变量,使您能够直接使用 `claude` 命令或将 Claude Code Router 与使用 Agent SDK 构建的应用程序集成。
|
|
406
359
|
|
|
407
|
-
|
|
360
|
+
要激活环境变量,请运行:
|
|
408
361
|
|
|
409
362
|
```shell
|
|
410
363
|
eval "$(ccr activate)"
|
|
411
364
|
```
|
|
412
365
|
|
|
413
|
-
|
|
366
|
+
此命令会以 shell 友好的格式输出必要的环境变量,这些变量将在当前的 shell 会话中设置。激活后,您可以:
|
|
414
367
|
|
|
415
|
-
-
|
|
416
|
-
-
|
|
368
|
+
- **直接使用 `claude` 命令**:无需使用 `ccr code` 即可运行 `claude` 命令。`claude` 命令将自动通过 Claude Code Router 路由请求。
|
|
369
|
+
- **与 Agent SDK 应用程序集成**:使用 Anthropic Agent SDK 构建的应用程序将自动使用配置的路由器和模型。
|
|
417
370
|
|
|
418
|
-
|
|
371
|
+
`activate` 命令设置以下环境变量:
|
|
419
372
|
|
|
420
|
-
- `ANTHROPIC_AUTH_TOKEN`: API
|
|
421
|
-
- `ANTHROPIC_BASE_URL`:
|
|
422
|
-
- `NO_PROXY`:
|
|
423
|
-
- `DISABLE_TELEMETRY`:
|
|
424
|
-
- `DISABLE_COST_WARNINGS`:
|
|
425
|
-
- `API_TIMEOUT_MS`: API
|
|
373
|
+
- `ANTHROPIC_AUTH_TOKEN`: 来自配置的 API 密钥
|
|
374
|
+
- `ANTHROPIC_BASE_URL`: 本地路由器端点(默认:`http://127.0.0.1:3456`)
|
|
375
|
+
- `NO_PROXY`: 设置为 `127.0.0.1` 以防止代理干扰
|
|
376
|
+
- `DISABLE_TELEMETRY`: 禁用遥测
|
|
377
|
+
- `DISABLE_COST_WARNINGS`: 禁用成本警告
|
|
378
|
+
- `API_TIMEOUT_MS`: 来自配置的 API 超时时间
|
|
426
379
|
|
|
427
|
-
>
|
|
380
|
+
> **注意**:在使用激活的环境变量之前,请确保 Claude Code Router 服务正在运行(`ccr start`)。环境变量仅在当前 shell 会话中有效。要使其持久化,您可以将 `eval "$(ccr activate)"` 添加到您的 shell 配置文件(例如 `~/.zshrc` 或 `~/.bashrc`)中。
|
|
428
381
|
|
|
429
382
|
#### Providers
|
|
430
383
|
|
|
431
|
-
|
|
384
|
+
`Providers` 数组是您定义要使用的不同模型提供商的地方。每个提供商对象都需要:
|
|
432
385
|
|
|
433
|
-
-
|
|
434
|
-
-
|
|
435
|
-
-
|
|
436
|
-
-
|
|
437
|
-
-
|
|
386
|
+
- `name`: 提供商的唯一名称。
|
|
387
|
+
- `api_base_url`: 聊天补全的完整 API 端点。
|
|
388
|
+
- `api_key`: 您提供商的 API 密钥。
|
|
389
|
+
- `models`: 此提供商可用的模型名称列表。
|
|
390
|
+
- `transformer` (可选): 指定用于处理请求和响应的转换器。
|
|
438
391
|
|
|
439
392
|
#### Transformers
|
|
440
393
|
|
|
441
|
-
Transformers
|
|
442
|
-
|
|
443
|
-
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
"transformer": {
|
|
480
|
-
"use": [
|
|
481
|
-
[
|
|
482
|
-
"maxtoken",
|
|
483
|
-
{
|
|
484
|
-
"max_tokens": 16384
|
|
485
|
-
}
|
|
486
|
-
]
|
|
487
|
-
]
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
```
|
|
491
|
-
|
|
492
|
-
**Available Built-in Transformers:**
|
|
493
|
-
|
|
494
|
-
- `Anthropic`:If you use only the `Anthropic` transformer, it will preserve the original request and response parameters(you can use it to connect directly to an Anthropic endpoint).
|
|
495
|
-
- `deepseek`: Adapts requests/responses for DeepSeek API.
|
|
496
|
-
- `gemini`: Adapts requests/responses for Gemini API.
|
|
497
|
-
- `openrouter`: Adapts requests/responses for OpenRouter API. It can also accept a `provider` routing parameter to specify which underlying providers OpenRouter should use. For more details, refer to the [OpenRouter documentation](https://openrouter.ai/docs/features/provider-routing). See an example below:
|
|
498
|
-
```json
|
|
499
|
-
"transformer": {
|
|
500
|
-
"use": ["openrouter"],
|
|
501
|
-
"moonshotai/kimi-k2": {
|
|
394
|
+
Transformers 允许您修改请求和响应负载,以确保与不同提供商 API 的兼容性。
|
|
395
|
+
|
|
396
|
+
- **全局 Transformer**: 将转换器应用于提供商的所有模型。在此示例中,`openrouter` 转换器将应用于 `openrouter` 提供商下的所有模型。
|
|
397
|
+
```json
|
|
398
|
+
{
|
|
399
|
+
"name": "openrouter",
|
|
400
|
+
"api_base_url": "https://openrouter.ai/api/v1/chat/completions",
|
|
401
|
+
"api_key": "sk-xxx",
|
|
402
|
+
"models": [
|
|
403
|
+
"google/gemini-2.5-pro-preview",
|
|
404
|
+
"anthropic/claude-sonnet-4",
|
|
405
|
+
"anthropic/claude-3.5-sonnet"
|
|
406
|
+
],
|
|
407
|
+
"transformer": { "use": ["openrouter"] }
|
|
408
|
+
}
|
|
409
|
+
```
|
|
410
|
+
- **特定于模型的 Transformer**: 将转换器应用于特定模型。在此示例中,`deepseek` 转换器应用于所有模型,而额外的 `tooluse` 转换器仅应用于 `deepseek-chat` 模型。
|
|
411
|
+
```json
|
|
412
|
+
{
|
|
413
|
+
"name": "deepseek",
|
|
414
|
+
"api_base_url": "https://api.deepseek.com/chat/completions",
|
|
415
|
+
"api_key": "sk-xxx",
|
|
416
|
+
"models": ["deepseek-chat", "deepseek-reasoner"],
|
|
417
|
+
"transformer": {
|
|
418
|
+
"use": ["deepseek"],
|
|
419
|
+
"deepseek-chat": { "use": ["tooluse"] }
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
- **向 Transformer 传递选项**: 某些转换器(如 `maxtoken`)接受选项。要传递选项,请使用嵌套数组,其中第一个元素是转换器名称,第二个元素是选项对象。
|
|
425
|
+
```json
|
|
426
|
+
{
|
|
427
|
+
"name": "siliconflow",
|
|
428
|
+
"api_base_url": "https://api.siliconflow.cn/v1/chat/completions",
|
|
429
|
+
"api_key": "sk-xxx",
|
|
430
|
+
"models": ["moonshotai/Kimi-K2-Instruct"],
|
|
431
|
+
"transformer": {
|
|
502
432
|
"use": [
|
|
503
433
|
[
|
|
504
|
-
"
|
|
434
|
+
"maxtoken",
|
|
505
435
|
{
|
|
506
|
-
"
|
|
507
|
-
"only": ["moonshotai/fp8"]
|
|
508
|
-
}
|
|
436
|
+
"max_tokens": 16384
|
|
509
437
|
}
|
|
510
438
|
]
|
|
511
439
|
]
|
|
512
440
|
}
|
|
513
441
|
}
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
-
|
|
519
|
-
-
|
|
520
|
-
-
|
|
521
|
-
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
**可用的内置 Transformer:**
|
|
445
|
+
|
|
446
|
+
- `Anthropic`: 如果你只使用这一个转换器,则会直接透传请求和响应(你可以用它来接入其他支持Anthropic端点的服务商)。
|
|
447
|
+
- `deepseek`: 适配 DeepSeek API 的请求/响应。
|
|
448
|
+
- `gemini`: 适配 Gemini API 的请求/响应。
|
|
449
|
+
- `openrouter`: 适配 OpenRouter API 的请求/响应。它还可以接受一个 `provider` 路由参数,以指定 OpenRouter 应使用哪些底层提供商。有关更多详细信息,请参阅 [OpenRouter 文档](https://openrouter.ai/docs/features/provider-routing)。请参阅下面的示例:
|
|
450
|
+
```json
|
|
451
|
+
"transformer": {
|
|
452
|
+
"use": ["openrouter"],
|
|
453
|
+
"moonshotai/kimi-k2": {
|
|
454
|
+
"use": [
|
|
455
|
+
[
|
|
456
|
+
"openrouter",
|
|
457
|
+
{
|
|
458
|
+
"provider": {
|
|
459
|
+
"only": ["moonshotai/fp8"]
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
]
|
|
463
|
+
]
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
```
|
|
467
|
+
- `groq`: 适配 groq API 的请求/响应
|
|
468
|
+
- `maxtoken`: 设置特定的 `max_tokens` 值。
|
|
469
|
+
- `tooluse`: 优化某些模型的工具使用(通过`tool_choice`参数)。
|
|
470
|
+
- `gemini-cli` (实验性): 通过 Gemini CLI [gemini-cli.js](https://gist.github.com/musistudio/1c13a65f35916a7ab690649d3df8d1cd) 对 Gemini 的非官方支持。
|
|
471
|
+
- `reasoning`: 用于处理 `reasoning_content` 字段。
|
|
472
|
+
- `sampling`: 用于处理采样信息字段,如 `temperature`、`top_p`、`top_k` 和 `repetition_penalty`。
|
|
473
|
+
- `enhancetool`: 对 LLM 返回的工具调用参数增加一层容错处理(这会导致不再流式返回工具调用信息)。
|
|
474
|
+
- `cleancache`: 清除请求中的 `cache_control` 字段。
|
|
475
|
+
- `vertex-gemini`: 处理使用 vertex 鉴权的 gemini api。
|
|
476
|
+
- `qwen-cli` (实验性): 通过 Qwen CLI [qwen-cli.js](https://gist.github.com/musistudio/f5a67841ced39912fd99e42200d5ca8b) 对 qwen3-coder-plus 的非官方支持。
|
|
477
|
+
- `rovo-cli` (experimental): 通过 Atlassian Rovo Dev CLI [rovo-cli.js](https://gist.github.com/SaseQ/c2a20a38b11276537ec5332d1f7a5e53) 对 GPT-5 的非官方支持。
|
|
478
|
+
|
|
479
|
+
**自定义 Transformer:**
|
|
480
|
+
|
|
481
|
+
您还可以创建自己的转换器,并通过 `config.json` 中的 `transformers` 字段加载它们。
|
|
531
482
|
|
|
532
483
|
```json
|
|
533
484
|
{
|
|
534
485
|
"transformers": [
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
486
|
+
{
|
|
487
|
+
"path": "/User/xxx/.claude-code-router/plugins/gemini-cli.js",
|
|
488
|
+
"options": {
|
|
489
|
+
"project": "xxx"
|
|
490
|
+
}
|
|
539
491
|
}
|
|
540
|
-
}
|
|
541
492
|
]
|
|
542
493
|
}
|
|
543
494
|
```
|
|
544
495
|
|
|
545
496
|
#### Router
|
|
546
497
|
|
|
547
|
-
|
|
498
|
+
`Router` 对象定义了在不同场景下使用哪个模型:
|
|
548
499
|
|
|
549
|
-
-
|
|
550
|
-
-
|
|
551
|
-
-
|
|
552
|
-
-
|
|
553
|
-
-
|
|
554
|
-
-
|
|
555
|
-
-
|
|
500
|
+
- `default`: 用于常规任务的默认模型。
|
|
501
|
+
- `background`: 用于后台任务的模型。这可以是一个较小的本地模型以节省成本。
|
|
502
|
+
- `think`: 用于推理密集型任务(如计划模式)的模型。
|
|
503
|
+
- `longContext`: 用于处理长上下文(例如,> 60K 令牌)的模型。
|
|
504
|
+
- `longContextThreshold` (可选): 触发长上下文模型的令牌数阈值。如果未指定,默认为 60000。
|
|
505
|
+
- `webSearch`: 用于处理网络搜索任务,需要模型本身支持。如果使用`openrouter`需要在模型后面加上`:online`后缀。
|
|
506
|
+
- `image`(测试版): 用于处理图片类任务(采用CCR内置的agent支持),如果该模型不支持工具调用,需要将`config.forceUseImageAgent`属性设置为`true`。
|
|
556
507
|
|
|
557
|
-
|
|
508
|
+
您还可以使用 `/model` 命令在 Claude Code 中动态切换模型:
|
|
558
509
|
`/model provider_name,model_name`
|
|
559
|
-
|
|
510
|
+
示例: `/model openrouter,anthropic/claude-3.5-sonnet`
|
|
560
511
|
|
|
561
|
-
####
|
|
512
|
+
#### 自定义路由器
|
|
562
513
|
|
|
563
|
-
|
|
514
|
+
对于更高级的路由逻辑,您可以在 `config.json` 中通过 `CUSTOM_ROUTER_PATH` 字段指定一个自定义路由器脚本。这允许您实现超出默认场景的复杂路由规则。
|
|
564
515
|
|
|
565
|
-
|
|
516
|
+
在您的 `config.json` 中配置:
|
|
566
517
|
|
|
567
518
|
```json
|
|
568
519
|
{
|
|
@@ -570,74 +521,54 @@ In your `config.json`:
|
|
|
570
521
|
}
|
|
571
522
|
```
|
|
572
523
|
|
|
573
|
-
|
|
524
|
+
自定义路由器文件必须是一个导出 `async` 函数的 JavaScript 模块。该函数接收请求对象和配置对象作为参数,并应返回提供商和模型名称的字符串(例如 `"provider_name,model_name"`),如果返回 `null` 则回退到默认路由。
|
|
574
525
|
|
|
575
|
-
|
|
526
|
+
这是一个基于 `custom-router.example.js` 的 `custom-router.js` 示例:
|
|
576
527
|
|
|
577
528
|
```javascript
|
|
578
529
|
// /User/xxx/.claude-code-router/custom-router.js
|
|
579
530
|
|
|
580
531
|
/**
|
|
581
|
-
*
|
|
532
|
+
* 一个自定义路由函数,用于根据请求确定使用哪个模型。
|
|
582
533
|
*
|
|
583
|
-
* @param {object} req -
|
|
584
|
-
* @param {object} config -
|
|
585
|
-
* @returns {Promise<string|null>} -
|
|
534
|
+
* @param {object} req - 来自 Claude Code 的请求对象,包含请求体。
|
|
535
|
+
* @param {object} config - 应用程序的配置对象。
|
|
536
|
+
* @returns {Promise<string|null>} - 一个解析为 "provider,model_name" 字符串的 Promise,如果返回 null,则使用默认路由。
|
|
586
537
|
*/
|
|
587
538
|
module.exports = async function router(req, config) {
|
|
588
|
-
const userMessage = req.body.messages.find(
|
|
539
|
+
const userMessage = req.body.messages.find(m => m.role === 'user')?.content;
|
|
589
540
|
|
|
590
|
-
if (userMessage && userMessage.includes(
|
|
591
|
-
//
|
|
592
|
-
return
|
|
541
|
+
if (userMessage && userMessage.includes('解释这段代码')) {
|
|
542
|
+
// 为代码解释任务使用更强大的模型
|
|
543
|
+
return 'openrouter,anthropic/claude-3.5-sonnet';
|
|
593
544
|
}
|
|
594
545
|
|
|
595
|
-
//
|
|
546
|
+
// 回退到默认的路由配置
|
|
596
547
|
return null;
|
|
597
548
|
};
|
|
598
549
|
```
|
|
599
550
|
|
|
600
|
-
#####
|
|
551
|
+
##### 子代理路由
|
|
601
552
|
|
|
602
|
-
|
|
553
|
+
对于子代理内的路由,您必须在子代理提示词的**开头**包含 `<CCR-SUBAGENT-MODEL>provider,model</CCR-SUBAGENT-MODEL>` 来指定特定的提供商和模型。这样可以将特定的子代理任务定向到指定的模型。
|
|
603
554
|
|
|
604
|
-
|
|
555
|
+
**示例:**
|
|
605
556
|
|
|
606
557
|
```
|
|
607
558
|
<CCR-SUBAGENT-MODEL>openrouter,anthropic/claude-3.5-sonnet</CCR-SUBAGENT-MODEL>
|
|
608
|
-
|
|
559
|
+
请帮我分析这段代码是否存在潜在的优化空间...
|
|
609
560
|
```
|
|
610
561
|
|
|
611
562
|
## Status Line (Beta)
|
|
612
|
-
|
|
613
|
-
To better monitor the status of Claude Code Router at runtime, the project includes a built-in status line tool that can be enabled from the UI.
|
|
614
|
-
|
|
563
|
+
为了在运行时更好的查看claude-code-router的状态,claude-code-router在v1.0.40内置了一个statusline工具,你可以在UI中启用它,
|
|
615
564
|

|
|
616
565
|
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
1. Open the UI with `ccr ui`
|
|
620
|
-
2. Enable **StatusLine** in the configuration panel
|
|
621
|
-
3. Save the configuration and restart the service with `ccr restart`
|
|
622
|
-
4. Start Claude Code with `ccr code`
|
|
623
|
-
|
|
624
|
-
> The built-in status line is injected automatically when Claude Code is launched with `ccr code`.
|
|
625
|
-
|
|
626
|
-
The status line supports token-related variables such as:
|
|
627
|
-
|
|
628
|
-
- `{{inputTokens}}`
|
|
629
|
-
- `{{outputTokens}}`
|
|
630
|
-
- `{{tokenSpeed}}`
|
|
631
|
-
|
|
632
|
-
This makes it possible to display input tokens, output tokens, and streaming speed directly in the terminal while requests are running.
|
|
633
|
-
|
|
634
|
-
The effect is as follows:
|
|
635
|
-
|
|
566
|
+
效果如下:
|
|
636
567
|

|
|
637
568
|
|
|
638
569
|
## 🤖 GitHub Actions
|
|
639
570
|
|
|
640
|
-
|
|
571
|
+
将 Claude Code Router 集成到您的 CI/CD 管道中。在设置 [Claude Code Actions](https://docs.anthropic.com/en/docs/claude-code/github-actions) 后,修改您的 `.github/workflows/claude.yaml` 以使用路由器:
|
|
641
572
|
|
|
642
573
|
```yaml
|
|
643
574
|
name: Claude Code
|
|
@@ -681,7 +612,7 @@ jobs:
|
|
|
681
612
|
|
|
682
613
|
- name: Start Claude Code Router
|
|
683
614
|
run: |
|
|
684
|
-
nohup ~/.bun/bin/bunx claude-code-router-next@latest start &
|
|
615
|
+
nohup ~/.bun/bin/bunx @wengine-ai/claude-code-router-next@latest start &
|
|
685
616
|
shell: bash
|
|
686
617
|
|
|
687
618
|
- name: Run Claude Code
|
|
@@ -693,28 +624,146 @@ jobs:
|
|
|
693
624
|
anthropic_api_key: "any-string-is-ok"
|
|
694
625
|
```
|
|
695
626
|
|
|
696
|
-
|
|
627
|
+
这种设置可以实现有趣的自动化,例如在非高峰时段运行任务以降低 API 成本。
|
|
628
|
+
|
|
629
|
+
## 🎯 高级功能
|
|
630
|
+
|
|
631
|
+
### 模型族映射 (Family Routing)
|
|
632
|
+
|
|
633
|
+
Claude Code Router 支持**模型族映射**,将 Claude Code 的模型分级(opus/sonnet/haiku)映射到不同服务商的模型。这实现了智能成本控制:主进程保持相同模型以最大化缓存命中,子代理可自动降级。
|
|
634
|
+
|
|
635
|
+
#### 配置示例
|
|
636
|
+
|
|
637
|
+
```json
|
|
638
|
+
{
|
|
639
|
+
"Router": {
|
|
640
|
+
"enableFamilyRouting": true,
|
|
641
|
+
"families": {
|
|
642
|
+
"opus": {
|
|
643
|
+
"default": "智谱 Coding Plan,glm-5",
|
|
644
|
+
"think": "DeepSeek,deepseek-reasoner",
|
|
645
|
+
"longContext": "阿里云,qwen3-plus",
|
|
646
|
+
"webSearch": "Gemini,gemini-2.5-flash",
|
|
647
|
+
"fallback": {
|
|
648
|
+
"default": ["阿里云,glm-4", "DeepSeek,deepseek-chat"],
|
|
649
|
+
"think": ["阿里云,qwen-plus", "DeepSeek,deepseek-reasoner"]
|
|
650
|
+
}
|
|
651
|
+
},
|
|
652
|
+
"sonnet": {
|
|
653
|
+
"default": "OpenRouter,deepseek/deepseek-v3",
|
|
654
|
+
"think": "DeepSeek,deepseek-reasoner",
|
|
655
|
+
"fallback": {
|
|
656
|
+
"default": ["阿里云,qwen-turbo", "Gemini,gemini-2.0-flash"]
|
|
657
|
+
}
|
|
658
|
+
},
|
|
659
|
+
"haiku": {
|
|
660
|
+
"default": "阿里云,qwen-turbo",
|
|
661
|
+
"fallback": {
|
|
662
|
+
"default": ["Gemini,gemini-2.0-flash-lite"]
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
```
|
|
669
|
+
|
|
670
|
+
#### 场景说明
|
|
671
|
+
|
|
672
|
+
| 场景 | 触发条件 | 说明 |
|
|
673
|
+
|------|----------|------|
|
|
674
|
+
| `default` | 默认 | 日常对话和代码生成 |
|
|
675
|
+
| `think` | Plan Mode | 复杂推理、架构设计 |
|
|
676
|
+
| `longContext` | token > 60000 | 大文件分析 |
|
|
677
|
+
| `webSearch` | web_search tool | 网络搜索任务 |
|
|
678
|
+
| `background` | 后台任务 | 自动提交、简单检查 |
|
|
679
|
+
|
|
680
|
+
### Fallback 机制
|
|
681
|
+
|
|
682
|
+
当主模型失败时,Router 会自动尝试 fallback 链中的备用模型,确保请求不中断。
|
|
683
|
+
|
|
684
|
+
#### 工作流程
|
|
685
|
+
|
|
686
|
+
1. **健康检查**:每个 provider/model 维护健康状态
|
|
687
|
+
- `closed`(健康)→ 绿色指示器
|
|
688
|
+
- `open`(失败池)→ 红色指示器,自动跳过
|
|
689
|
+
- `half-open`(恢复中)→ 黄色指示器
|
|
690
|
+
|
|
691
|
+
2. **失败判定**:连续 3 次失败后进入 `open` 状态
|
|
692
|
+
|
|
693
|
+
3. **拖动排序**:UI 支持拖动 fallback 模型调整优先级,排序越靠前越先尝试
|
|
694
|
+
|
|
695
|
+
4. **Fallback Promotion**:当主模型失败且 fallback 成功时,临时"晋升" fallback 模型(TTL 10 分钟),后续请求直接使用晋升模型,避免重复尝试失败的主模型
|
|
696
|
+
|
|
697
|
+
5. **自动恢复**:每 5 分钟探测失败模型,成功后恢复为 `half-open`,再成功 2 次后恢复为 `closed`
|
|
698
|
+
|
|
699
|
+

|
|
700
|
+
|
|
701
|
+
#### Fallback 配置层级
|
|
702
|
+
|
|
703
|
+
```
|
|
704
|
+
family fallback → global fallback
|
|
705
|
+
```
|
|
706
|
+
|
|
707
|
+
优先使用模型族专属的 fallback 配置,其次使用全局 fallback。
|
|
708
|
+
|
|
709
|
+
```json
|
|
710
|
+
{
|
|
711
|
+
"Router": {
|
|
712
|
+
"enableFallback": true,
|
|
713
|
+
"families": {
|
|
714
|
+
"opus": {
|
|
715
|
+
"fallback": {
|
|
716
|
+
"default": ["阿里云,glm-4", "DeepSeek,deepseek-chat"]
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
},
|
|
721
|
+
"fallback": {
|
|
722
|
+
"default": ["OpenRouter,deepseek/deepseek-v3", "Gemini,gemini-2.5-flash"],
|
|
723
|
+
"think": ["DeepSeek,deepseek-reasoner"]
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
```
|
|
727
|
+
|
|
728
|
+
### 用量统计
|
|
729
|
+
|
|
730
|
+
Router 提供完善的用量统计功能:
|
|
731
|
+
|
|
732
|
+
#### Quota 监控
|
|
697
733
|
|
|
698
|
-
|
|
734
|
+
UI 界面实时显示各服务商的额度使用情况:
|
|
699
735
|
|
|
700
|
-
|
|
736
|
+
- **5h 额度**:短窗口限额(5 小时重置)
|
|
737
|
+
- **7d 额度**:周度限额(7 天重置)
|
|
738
|
+
- **重置时间**:显示下次额度重置时间
|
|
701
739
|
|
|
702
|
-
|
|
703
|
-
- [Maybe We Can Do More with the Router](blog/en/maybe-we-can-do-more-with-the-route.md)
|
|
704
|
-
- [GLM-4.6 Supports Reasoning and Interleaved Thinking](blog/en/glm-4.6-supports-reasoning.md)
|
|
740
|
+

|
|
705
741
|
|
|
706
|
-
|
|
742
|
+
支持的服务商:
|
|
743
|
+
- 智谱 GLM Coding Plan
|
|
744
|
+
- 阿里云 Qwen Coding Plan
|
|
745
|
+
- Kimi Coding Plan
|
|
746
|
+
- MiniMax Coding Plan
|
|
747
|
+
- DeepSeek
|
|
748
|
+
- OpenRouter
|
|
749
|
+
- SiliconFlow
|
|
707
750
|
|
|
708
|
-
|
|
751
|
+
#### Usage 记录
|
|
709
752
|
|
|
710
|
-
|
|
753
|
+
每次请求都会记录详细统计信息:
|
|
711
754
|
|
|
712
|
-
|
|
755
|
+
| 字段 | 说明 |
|
|
756
|
+
|------|------|
|
|
757
|
+
| `inputTokens` | 输入 token 数 |
|
|
758
|
+
| `outputTokens` | 输出 token 数 |
|
|
759
|
+
| `cacheReadInputTokens` | 缓存读取 token |
|
|
760
|
+
| `cacheCreationInputTokens` | 缓存创建 token |
|
|
761
|
+
| `ttft` | 首 token 时间 (ms) |
|
|
762
|
+
| `tokensPerSecond` | 输出速度 |
|
|
763
|
+
| `durationMs` | 请求耗时 |
|
|
764
|
+
| `status` | success / error |
|
|
713
765
|
|
|
714
|
-
|
|
715
|
-
<tr>
|
|
716
|
-
<td><img src="/blog/images/alipay.jpg" width="200" alt="Alipay" /></td>
|
|
717
|
-
<td><img src="/blog/images/wechat.jpg" width="200" alt="WeChat Pay" /></td>
|
|
718
|
-
</tr>
|
|
719
|
-
</table>
|
|
766
|
+
数据存储位置:`~/.claude-code-router/data/usage.jsonl`
|
|
720
767
|
|
|
768
|
+
## 交流群
|
|
769
|
+
<img src="/blog/images/wechat_group.jpg" width="200" alt="wechat_group" />
|