Skip to content

2.8 使用CCR等中轉工具

2.8.1 概述

Claude Code預設需要直接連線到Anthropic的API伺服器,但在某些情況下(如網路限制、訪問速度慢等),使用者可能需要使用中轉工具來代理Claude Code的請求。CCR(ClaudeCodeRouter)是一種常用的中轉工具,它可以將Claude Code的請求進行代理中轉,幫助使用者解決網路連線問題,提高訪問速度,甚至實現一些高階功能。本節將介紹如何使用CCR等中轉工具來最佳化Claude Code的使用體驗。

2.8.2 什么是ClaudeCodeRouter (CCR)

2.8.2.1 基本概念

ClaudeCodeRouter(簡稱CCR)是一個開源的Claude Code請求轉發工具,它可以:

  • 代理Claude Code的API請求
  • 最佳化網路連線,提高訪問速度
  • 支援多種大模型的統一接入
  • 提供請求日誌和監控功能
  • 支援自定義路由規則和流量控制

2.8.2.2 核心功能

  1. 請求代理 :將Claude Code的API請求轉發到目標伺服器
  2. 多模型支援 :支援同時接入多個大模型API
  3. 負載均衡 :在多個API端點之間分配請求負載
  4. 快取機制 :快取常用請求的響應,提高效能
  5. 安全控制 :提供API金鑰管理和訪問控制

2.8.3 安装ClaudeCodeRouter

2.8.3.1 系统要求

  • Node.js 18.0.0 或更高版本
  • npm 或 pnpm 包管理器
  • 足夠的系統記憶體和儲存空間

2.8.3.2 安裝步驟

  1. 安裝Node.js
     * 访问Node.js官方网站([https://nodejs.org/)下载并安装最新版本的Node.js](https://nodejs.org/%EF%BC%89%E4%B8%8B%E8%BD%BD%E5%B9%B6%E5%AE%89%E8%A3%85%E6%9C%80%E6%96%B0%E7%89%88%E6%9C%AC%E7%9A%84Node.js)
     * 安装完成后,打开终端验证安装:

           bash


           node --version
           npm --version
  1. 安裝ClaudeCodeRouter
         bash


         npm install -g @musistudio/claude-code-router

或使用pnpm:

         bash


         pnpm add -g @musistudio/claude-code-router
  1. 驗證安裝
         bash


         ccr --version

## 2.8.4 配置ClaudeCodeRouter

### 2.8.4.1 创建配置文件
  1. 建立配置目錄
         bash


         mkdir -p ~/.claude-code-router
  1. 建立配置檔案
         bash


         touch ~/.claude-code-router/config.json
  1. 編輯配置檔案
         json


         {
           "port": 3000,
           "logLevel": "info",
           "endpoints": [
             {
               "name": "anthropic",
               "type": "anthropic",
               "apiKey": "your-anthropic-api-key",
               "baseUrl": "https://api.anthropic.com/v1"
             },
             {
               "name": "glm",
               "type": "custom",
               "apiKey": "your-glm-api-key",
               "baseUrl": "https://api.glm.example.com/v1",
               "headers": {
                 "Content-Type": "application/json"
               }
             }
           ],
           "defaultEndpoint": "anthropic",
           "cache": {
             "enabled": true,
             "ttl": 3600
           }
         }

### 2.8.4.2 配置参数说明


  * **port** :CCR伺服器監聽的埠
  * **logLevel** :日誌級別(debug、info、warn、error)
  * **endpoints** :配置的API端點列表

    * **name** :端点名称
    * **type** :端点类型(anthropic、custom等)
    * **apiKey** :API密钥
    * **baseUrl** :API基础URL
    * **headers** :自定义请求头


  * **defaultEndpoint** :預設使用的端點
  * **cache** :快取配置

    * **enabled** :是否启用缓存
    * **ttl** :缓存过期时间(秒)

## 2.8.5 启动ClaudeCodeRouter

### 2.8.5.1 基本启动命令

    bash


    ccr start

### 2.8.5.2 自定义启动参数

    bash


    # 指定配置文件
    ccr start --config ~/my-ccr-config.json

    # 指定端口
    ccr start --port 4000

    # 启用调试模式
    ccr start --debug

### 2.8.5.3 后台运行

在Linux/macOS系統上,可以使用nohup或systemd來後臺執行CCR:

    bash


    nohup ccr start > ~/.claude-code-router/ccr.log 2>&1 &

## 2.8.6 配置Claude Code使用中转服务

### 2.8.6.1 在VS Code中配置
  1. 開啟VS Code
  2. 點選左側邊欄的Claude Code 圖示
  3. 點選右上角的設定 按鈕(齒輪圖示)
  4. 選擇高階設定
  5. API端點 欄位中輸入:
         bash


         http://localhost:3000/v1
  1. API金鑰 欄位中輸入:
         bash


         ccr:your-anthropic-api-key


  7. 點選**儲存** 按鈕

### 2.8.6.2 在命令列中配置

  1. 開啟終端或命令提示符
  2. 執行以下命令配置API端點:

         bash


         claude config set api-endpoint http://localhost:3000/v1
  1. 配置API金鑰:
         bash


         claude config set api-key "ccr:your-anthropic-api-key"

## 2.8.7 其他常用中转工具

### 2.8.7.1 AnyRouter
  • 特點 :功能強大的API路由和轉發工具
  • 優勢 :支援多種API協議,提供豐富的路由規則
  • 使用方式 :下載並部署AnyRouter,配置路由規則

2.8.7.2 Cloudflare Workers

  • 特點 :基於Cloudflare邊緣網路的無伺服器函式
  • 優勢 :全球邊緣節點,低延遲訪問
  • 使用方式 :編寫並部署Cloudflare Worker指令碼

2.8.8 注意事項

  1. 網路安全 :使用中轉工具時,注意保護API金鑰和敏感資料
  2. 效能影響 :中轉工具可能會對請求響應時間產生一定影響
  3. 可靠性 :自行部署的中轉服務需要考慮高可用性和故障恢復
  4. 合規性 :確保使用中轉工具符合相關法律法規和服務條款
  5. 成本控制 :使用第三方中轉服務時,注意瞭解相關費用和使用限制
  6. 更新維護 :定期更新中轉工具和配置,確保安全性和穩定性

基于 MIT 许可发布 | 永久导航