部署 CLIProxyAPI 和 CPA Usage Keeper

docker

最近在折腾本地 AI 代理服务,把 CLIProxyAPI 和 CPA Usage Keeper 部署到了 Docker 里,记录一下过程。

背景

CLIProxyAPI

CLIProxyAPI 是一个开源的 AI API 代理服务,主要用来:

  • 转发 OpenAI/Gemini/Claude/Codex 等模型的 API 请求
  • 支持 OAuth 登录,获取官方模型的配额
  • 可以对接 OpenRouter 等第三方 API 聚合平台
  • 提供 Web 管理界面和 API

对于我这种无法直接访问 OpenAI/Gemini 官方 API 的人来说,它是个不错的解决方案。可以把请求转发到 OpenRouter,由 OpenRouter 帮我调用意愿的模型。

CPA Usage Keeper

CPA Usage Keeper(简称 CPA)是 CLIProxyAPI 的配套管理工具,主要功能:

  • 同步 CLIProxyAPI 的使用数据(请求数、Token 消耗等)
  • 管理 API Keys 和凭证
  • 提供使用量统计和可视化界面
  • 支持多用户/多项目统计

简单说就是给 CLIProxyAPI 做一个”后台”,方便查看花了多少钱、用了多少 Token。

部署步骤

1. 准备目录和配置文件

创建工作目录:

mkdir -p /root/docker/cliproxyapi
cd /root/docker/cliproxyapi

配置文件 config.yaml

host: "0.0.0.0"
port: 8317

remote-management:
  allow-remote: true
  secret-key: "my-simple-key"

api-keys:
  - "cliproxy-default-key"

openai-compatibility:
  - name: "openrouter"
    prefix: "openrouter"
    base-url: "https://openrouter.ai/api/v1"
    headers:
      Content-Type: "application/json"
    api-key-entries:
      - api-key: "你的 OpenRouter API Key"
    models:
      - name: "minimax/minimax-m2.5:free"
        alias: "minimax-m2.5-free"

2. config.yaml 完整配置

记得开启 usage-statistics-enabled,否则不会记录使用量:

host: "0.0.0.0"
port: 8317

remote-management:
  allow-remote: true
  secret-key: "my-key"

api-keys:
  - "cliproxy-key"

openai-compatibility:
  - name: "openrouter"
    prefix: "openrouter"
    base-url: "https://openrouter.ai/api/v1"
    headers:
      Content-Type: "application/json"
    api-key-entries:
      - api-key: "你的 OpenRouter API Key"
    models:
      - name: "minimax/minimax-m2.5:free"
        alias: "minimax-m2.5-free"
  - name: DeepSeek
    base-url: https://api.deepseek.com
    models:
      - name: deepseek-v4-pro
      - name: deepseek-v4-flash
    headers:
      Authorization: Bearer 你的DeepSeek-API-Key
      Content-Type: application/json
    prefix: ds
  - name: lmstudio
    prefix: studio
    base-url: http://本地IP:1234/v1
    models:
      - name: qwen3.6-35b-a3b@8bit

usage-statistics-enabled: true

3. docker-compose.yml

version: '3'
services:
  cli-proxy-api:
    image: eceasy/cli-proxy-api:latest
    container_name: cli-proxyapi
    network_mode: bridge
    volumes:
      - ./config.yaml:/CLIProxyAPI/config.yaml
      - ./auths:/root/.cli-proxy-api
      - ./logs:/CLIProxyAPI/logs
    restart: unless-stopped

  cpa-usage-keeper:
    image: ghcr.io/willxup/cpa-usage-keeper:latest
    container_name: cli-cpa
    restart: unless-stopped
    network_mode: bridge
    environment:
      - TZ=Asia/Shanghai
      - CPA_BASE_URL=http://cliproxyapi.local:8317
      - REDIS_QUEUE_ADDR=cliproxyapi.local:8317
      - CPA_MANAGEMENT_KEY=my-key
      - AUTH_ENABLED=true
      - LOGIN_PASSWORD=password
    volumes:
      - ./keeper:/data

4. 启动服务

docker-compose up -d

5. 验证

检查容器状态:

docker ps | grep cli-

CLIProxyAPI 默认监听在 8317,CPA 默认监听在 8080

调用示例

配置好模型后,就可以像调用普通 OpenAI API 一样调用 CLIProxyAPI:

curl http://cliproxyapi:8317/v1/chat/completions \
  -H "Authorization: Bearer cliproxy-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "minimax-m2.5-free",
    "messages": [
      {"role": "user", "content": "Hello!"}
    ]
  }'

参数说明:

参数
地址 http://cliproxyapi:8317
API Key cliproxy-key (config.yaml 中配置的 api-keys)
Model minimax-m2.5-free (config.yaml 中定义的 alias)

如果需要添加更多模型,只需要在 config.yaml 的 openai-compatibility.models 中添加即可。

其他模型示例:

curl http://cliproxyapi:8317/v1/chat/completions \
  -H "Authorization: Bearer cliproxy-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen3.6-35b-a3b@8bit",
    "messages": [
      {"role": "user", "content": "你好"}
    ]
  }'

一些坑

1. 密钥配置

CLIProxyAPI 的 secret-key 支持明文配置,启动时会自动 bcrypt 哈希。所以直接写明文就行,别像我一开始那样直接贴个 bcrypt 哈希上去,结果两边密钥对不上。

2. 免密登录

试了下把 secret-key 设为空想免密,结果管理 API 直接返回 404 不可用了。CPA 还需要管理 API 来同步数据,所以没法完全免密。好在明文密钥两边会自动哈希匹配,保持一致很简单。

. 使用量统计默认关闭

一开始 CPA 怎么都拉不到数据,后来发现 CLIProxyAPI 默认关闭了使用量统计。需要在 config.yaml 里加上:

usage-statistics-enabled: true

4. CPA 登录保护

CPA 默认不带密码,如果暴露在公网很危险。启用登录保护:

environment:
  - AUTH_ENABLED=true
  - LOGIN_PASSWORD=password

访问

  • CLIProxyAPI API: http://CLIProxyAPI:8317

  • CLIProxyAPI Dashboard: http://CLIProxyAPI:8317/v0/management

    image-20260503下午45248789

  • CPA 管理界面: http://CPA:8080

    image-20260503下午45318458

小结

这两个工具配合起来挺香的,CLIProxyAPI 做代理转发,CPA 做使用量统计和密钥管理。内部网络环境下,通过 OpenRouter 访问各种模型还是很方便的。


macmon - Apple Silicon Mac 的性能监控利器