一个高性能的 Claude API 代理服务,提供 API 密钥管理、使用量跟踪、配额控制和请求转发功能。
- 🔑 API 密钥管理:创建和管理代理 API 密钥,独立计费
- 🔄 请求转发:无缝代理到 Anthropic Claude API,带重试机制
- 📊 使用量跟踪:全面的 token 使用量监控和请求日志
- 🎯 配额控制:每个密钥的 token 限制和可配置等级的速率限制
- 🔒 安全性:哈希存储 API 密钥,支持 IP/域名白名单
- 📈 监控:导出 Prometheus 指标供 Grafana 可视化
- 基于 JWT 的管理员认证
- 创建、禁用和删除 API 密钥
- 查看所有密钥的使用统计
- 配置速率限制等级
- 查询个人 API 密钥使用统计
- 获取每日使用量明细
- 访问详细的请求日志
- 实时配额状态监控
- Python 3.11+
- PostgreSQL 14+
- Redis 6+(可选)
- Docker & Docker Compose(可选)
- 克隆并配置:
git clone <repository-url>
cd claude-code-switch
cp .env.example .env
# 编辑 .env 文件,配置必要的环境变量- 使用自动化脚本部署:
chmod +x deploy.sh
./deploy.sh- 或者手动使用 Docker Compose:
docker-compose up -d
docker-compose exec relay-station alembic upgrade head- 安装依赖:
pip install -r requirements.txt- 配置环境:
cp .env.example .env
# 编辑 .env 文件,配置你的设置- 运行数据库迁移:
alembic upgrade head- 启动服务器:
python run.py在 .env 文件中配置以下关键变量:
# 核心配置
UPSTREAM_API_KEY=你的-anthropic-api-密钥
ADMIN_PASSWORD=你的安全密码
SECRET_KEY=你的-jwt-密钥
# 数据库
DATABASE_URL=postgresql://postgres:postgres@localhost:15432/relay_station
# Redis(可选)
REDIS_URL=redis://localhost:16379/0
# 外部服务(Docker)
# PostgreSQL: localhost:15432 (用户: postgres, 密码: postgres)
# Redis: localhost:16379 (密码: 123456)生成安全的 JWT 密钥:
openssl rand -hex 32编辑 config.yaml 进行详细设置:
upstream:
primary:
url: "https://api.anthropic.com/v1"
api_key: "${UPSTREAM_API_KEY}"
timeout: 60
max_retries: 3
rate_limit:
by_tier:
basic:
requests: 100
period: 60
premium:
requests: 1000
period: 60- 登录获取管理员令牌:
curl -X POST http://localhost:8000/admin/login \
-u admin:你的管理员密码- 为用户创建 API 密钥:
curl -X POST http://localhost:8000/admin/api-keys \
-H "Authorization: Bearer <管理员令牌>" \
-H "Content-Type: application/json" \
-d '{
"name": "用户名称",
"description": "描述",
"token_limit": 1000000,
"rate_limit_tier": "basic"
}'响应包含生成的 API 密钥(仅显示一次):
{
"id": "uuid",
"api_key": "sk-proxy-xxx...",
"name": "用户名称",
"token_limit": 1000000,
"tokens_used": 0,
"is_active": true
}对于 Claude Code 用户,配置环境变量:
export ANTHROPIC_API_KEY="sk-proxy-xxx..." # 你的中转站生成的密钥
export ANTHROPIC_BASE_URL="http://localhost:8000" # 中转站 URL- 查看使用统计:
curl -X GET http://localhost:8000/api/usage \
-H "Authorization: Bearer sk-proxy-xxx..."- 检查配额状态:
curl -X GET http://localhost:8000/api/usage/quota \
-H "Authorization: Bearer sk-proxy-xxx..."- 获取每日使用明细:
curl -X GET http://localhost:8000/api/usage/daily \
-H "Authorization: Bearer sk-proxy-xxx..."POST /admin/login- 管理员登录POST /admin/api-keys- 创建 API 密钥GET /admin/api-keys- 列出所有密钥GET /admin/api-keys/{id}- 获取密钥详情POST /admin/api-keys/{id}/disable- 禁用密钥POST /admin/api-keys/{id}/enable- 启用密钥DELETE /admin/api-keys/{id}- 删除密钥
GET /api/usage- 获取使用统计GET /api/usage/daily- 获取每日使用明细GET /api/usage/logs- 获取使用日志GET /api/usage/quota- 获取配额状态
/*- 所有其他请求转发到上游 API
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Claude Code │───▶│ 中转代理 │───▶│ Anthropic API │
│ 客户端 │ │ 服务 │ │ │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│
▼
┌─────────────────┐
│ PostgreSQL │
│ 数据库 │
└─────────────────┘
- API 层 (
app/api/):管理和代理操作的 REST 接口 - 服务层 (
app/services/):API 密钥、使用跟踪和代理的业务逻辑 - 数据层 (
app/models/):数据库实体的 SQLAlchemy 模型 - 核心层 (
app/core/):配置、安全、日志和指标
-
代理服务 (
app/services/proxy_service.py):- 将用户的代理密钥替换为真实的 Anthropic API 密钥
- 实现指数退避的重试逻辑
- 从 API 响应中提取 token 使用量
- 在需要时回退到 tiktoken 估算
-
Token 管理:
- 使用 tiktoken 进行精确的 token 计数
- 跟踪 prompt_tokens、completion_tokens、total_tokens
- 执行每个密钥的配额和速率限制
-
安全性:
- 使用 bcrypt 哈希存储 API 密钥
- JWT 令牌用于管理员认证
- 请求/响应过滤以防止数据泄露
服务导出以下指标:
relay_requests_total- 请求总数proxy_duration_seconds- 代理请求延迟tokens_used_total- 消耗的 token 总数token_quota_usage_ratio- 配额使用比例
访问指标:http://localhost:8000/metrics
监控服务健康状态:
curl http://localhost:8000/health- 使用强密码和复杂的 SECRET_KEY
- 通过反向代理(Nginx/Apache)启用 HTTPS
- 通过 IP 限制管理员接口访问
- 设置适当的防火墙规则
- 定期轮换管理员密码
- 为不同应用创建独立的 API 密钥
- 设置合理的 token 限制和过期时间
- 监控异常使用模式
- 设置配额使用告警
- 监控可疑请求模式
- 定期审计使用日志
- 跟踪认证失败尝试
数据库连接失败:
- 验证 PostgreSQL 是否运行
- 检查 DATABASE_URL 配置
- 确保数据库用户有正确的权限
上游 API 超时:
- 验证 UPSTREAM_API_KEY 是否有效
- 检查网络连通性
- 在 config.yaml 中调整超时配置
Token 计数不准确:
- 确保 tiktoken 已安装:
pip install tiktoken - 验证模型名称是否正确识别
# 运行测试
pytest tests/
# 带覆盖率运行
pytest --cov=app tests/# 格式化代码
black app/
# 代码检查
flake8 app/
# 类型检查
mypy app/# 创建新迁移
alembic revision --autogenerate -m "描述"
# 应用迁移
alembic upgrade head
# 回滚迁移
alembic downgrade -1- Fork 本仓库
- 创建功能分支:
git checkout -b feature-name - 进行你的修改
- 运行测试和代码检查
- 提交 Pull Request
MIT 许可证 - 详见 LICENSE 文件。
如有问题和疑问:
- 在仓库中创建 issue
- 查看现有文档
- 联系维护者