ClawRouter - 智能 LLM 路由器
约 836 字大约 3 分钟
2026-03-04
智能 LLM 路由器,根据查询复杂度自动选择最合适的模型,优化成本和性能。
🎯 核心功能
智能路由
- 复杂度分析: 自动评估查询难度
- 模型匹配: 选择最适合的模型
- 成本优化: 简单查询使用便宜模型
- 性能保证: 复杂任务使用强大模型
负载均衡
- 多实例支持: 分散请求到多个后端
- 健康检查: 自动检测服务可用性
- 故障转移: 自动切换到备用服务
- 权重配置: 灵活的流量分配
成本控制
- 预算管理: 设置每日/每月预算
- 使用统计: 实时追踪 API 消耗
- 成本预警: 超额自动通知
- 优化建议: 智能推荐配置
🚀 快速开始
安装
npm install -g claw-router基础配置
创建 router.config.json:
{
"models": [
{
"name": "gpt-4",
"provider": "openai",
"apiKey": "sk-xxx",
"complexity": "high",
"costPerToken": 0.00003
},
{
"name": "gpt-3.5-turbo",
"provider": "openai",
"apiKey": "sk-xxx",
"complexity": "low",
"costPerToken": 0.000002
},
{
"name": "deepseek-chat",
"provider": "deepseek",
"apiKey": "sk-xxx",
"complexity": "medium",
"costPerToken": 0.000001
}
],
"routing": {
"strategy": "auto",
"fallback": "gpt-3.5-turbo"
},
"budget": {
"daily": 10,
"monthly": 200
}
}启动服务
claw-router start --config router.config.json --port 8080在 OpenClaw 中使用
修改 OpenClaw 配置,将模型 API 指向 ClawRouter:
{
"models": {
"default": {
"provider": "openai",
"baseURL": "http://localhost:8080/v1",
"apiKey": "router-key"
}
}
}📊 路由策略
自动路由(推荐)
根据查询特征自动选择模型:
{
"routing": {
"strategy": "auto",
"rules": [
{
"condition": "tokens < 100 && !code",
"model": "gpt-3.5-turbo"
},
{
"condition": "code || tokens > 500",
"model": "gpt-4"
},
{
"condition": "chinese",
"model": "deepseek-chat"
}
]
}
}轮询路由
均匀分配请求:
{
"routing": {
"strategy": "round-robin",
"models": ["gpt-3.5-turbo", "deepseek-chat"]
}
}权重路由
按权重分配流量:
{
"routing": {
"strategy": "weighted",
"weights": {
"gpt-3.5-turbo": 70,
"gpt-4": 20,
"deepseek-chat": 10
}
}
}成本优先
优先使用便宜模型:
{
"routing": {
"strategy": "cost-optimized",
"maxCostPerRequest": 0.01
}
}🔧 高级配置
缓存策略
减少重复请求成本:
{
"cache": {
"enabled": true,
"ttl": 3600,
"maxSize": "1GB",
"strategy": "lru"
}
}重试机制
提高可靠性:
{
"retry": {
"maxAttempts": 3,
"backoff": "exponential",
"timeout": 30000
}
}监控告警
{
"monitoring": {
"enabled": true,
"webhook": "https://your-webhook.com",
"alerts": [
{
"type": "budget",
"threshold": 80,
"action": "notify"
},
{
"type": "error-rate",
"threshold": 5,
"action": "switch-model"
}
]
}
}📈 使用统计
查看实时统计
claw-router stats输出示例:
┌─────────────────┬──────────┬─────────┬──────────┐
│ Model │ Requests │ Tokens │ Cost │
├─────────────────┼──────────┼─────────┼──────────┤
│ gpt-3.5-turbo │ 1,234 │ 456,789 │ $0.91 │
│ gpt-4 │ 89 │ 123,456 │ $3.70 │
│ deepseek-chat │ 567 │ 234,567 │ $0.23 │
├─────────────────┼──────────┼─────────┼──────────┤
│ Total │ 1,890 │ 814,812 │ $4.84 │
└─────────────────┴──────────┴─────────┴──────────┘
Daily Budget: $4.84 / $10.00 (48%)
Monthly Budget: $145.20 / $200.00 (73%)Web 控制台
访问 http://localhost:8080/dashboard 查看可视化统计。
🎨 使用场景
场景 1: 个人开发者
节省 API 成本,简单问题用便宜模型:
{
"routing": {
"strategy": "auto",
"rules": [
{
"condition": "tokens < 200",
"model": "deepseek-chat"
},
{
"condition": "tokens >= 200",
"model": "gpt-4"
}
]
}
}场景 2: 团队协作
多成员共享预算,按优先级分配:
{
"users": {
"alice": { "priority": "high", "quota": 100 },
"bob": { "priority": "normal", "quota": 50 }
},
"routing": {
"strategy": "priority-based"
}
}场景 3: 生产环境
高可用部署,自动故障转移:
{
"models": [
{
"name": "primary",
"instances": [
"https://api1.example.com",
"https://api2.example.com"
]
},
{
"name": "backup",
"instances": ["https://backup.example.com"]
}
],
"routing": {
"strategy": "failover",
"healthCheck": {
"interval": 30,
"timeout": 5
}
}
}🔗 相关链接
- GitHub: openclaw/claw-router
- 文档: 完整文档
- 示例: 配置示例
🤝 贡献
欢迎提交 PR 或报告问题!
git clone https://github.com/openclaw/claw-router.git
cd claw-router
npm install
npm run dev📄 许可证
MIT License