🌐 OpenClaw LLM Gateway配置指南
世界上有一种网关叫LLM Gateway,它就像一个翻译官,站在你和所有AI模型之间,替你选择最合适的那一个...
凌晨3点17分,我的API账单又爆了。三个模型同时跑,成本像脱缰的野马。我需要一个管家,一个能帮我精打细算、智能路由的管家——这就是OpenClaw LLM Gateway。
📋 功能介绍
🎯 核心能力
- 统一入口 - 一个API调用,背后是OpenAI、Claude、Gemini、DeepSeek等所有模型
- 智能路由 - 根据任务类型自动选择最优模型,省钱又省心
- 负载均衡 - 自动分配请求,避免单一模型限流
- 故障转移 - 主模型挂了?自动切换备用模型
- 成本追踪 - 实时监控Token消耗,预算可控
- 响应缓存 - 相同问题不重复计费
💡 为什么需要LLM Gateway?
想象一下,你是一个餐厅老板,要同时管理10个厨师——有的擅长中餐,有的擅长西餐,有的便宜但慢,有的贵但快。你需要一个智能派单系统,把合适的订单派给合适的厨师。
LLM Gateway就是这个派单系统。它知道:
- 写代码 → Claude Sonnet
- 创意写作 → GPT-4.5
- 简单问答 → DeepSeek(省钱)
- 多模态分析 → Gemini Pro
🚀 使用方法
1. 基础配置
# ~/.openclaw/gateway.yaml
gateway:
enabled: true
default_model: claude-sonnet-4
# 模型池配置
models:
- id: gpt-4.5-turbo
provider: openai
api_key: ${OPENAI_API_KEY}
priority: 1
max_tokens: 128000
- id: claude-sonnet-4
provider: anthropic
api_key: ${ANTHROPIC_API_KEY}
priority: 1
max_tokens: 200000
- id: gemini-2.5-pro
provider: google
api_key: ${GOOGLE_API_KEY}
priority: 2
- id: deepseek-v3
provider: deepseek
api_key: ${DEEPSEEK_API_KEY}
priority: 3 # 备用模型
2. 智能路由规则
# 路由策略配置
routing:
rules:
# 代码任务 → Claude
- match:
task_type: code
keywords: [debug, refactor, implement, fix]
model: claude-sonnet-4
# 创意写作 → GPT
- match:
task_type: creative
keywords: [write, story, article, blog]
model: gpt-4.5-turbo
# 简单任务 → DeepSeek省钱
- match:
task_type: simple
max_tokens: 500
model: deepseek-v3
# 多模态 → Gemini
- match:
has_image: true
model: gemini-2.5-pro
3. 负载均衡配置
load_balancing:
strategy: weighted_round_robin
# 同优先级模型的权重分配
weights:
claude-sonnet-4: 50
gpt-4.5-turbo: 30
gemini-2.5-pro: 20
# 限流配置
rate_limits:
openai: 1000/min
anthropic: 500/min
google: 600/min
4. 故障转移配置
failover:
enabled: true
# 重试策略
retry:
max_attempts: 3
backoff: exponential
base_delay: 1s
max_delay: 30s
# 故障检测
health_check:
interval: 60s
timeout: 10s
failure_threshold: 3
# 降级策略
fallback_chain:
- claude-sonnet-4
- gpt-4.5-turbo
- deepseek-v3
✨ 最佳实践
💡 省钱技巧
设置Token预算警报:当日消耗超过阈值时,自动切换到便宜模型。
budget:
daily_limit: $10
alert_threshold: 0.8
auto_downgrade: true
⚠️ 注意事项
- 不要把所有API Key写死在配置里,用环境变量
- 定期检查路由规则,模型能力在变
- 监控每个模型的响应时间,动态调整权重
📝 实战案例
一个Agent调用示例:
# 不指定模型,让Gateway智能路由
agents:
my-agent:
# 不写model,Gateway自动选择
tools: [code_analysis, web_search]
# 或明确指定路由规则
agents:
code-reviewer:
routing_rule: code # 使用代码路由规则
💻 代码示例
Python SDK调用
from openclaw import Agent, Gateway
# 初始化Gateway
gateway = Gateway(config="gateway.yaml")
# 创建Agent(自动路由)
agent = Agent(
name="smart-assistant",
gateway=gateway, # 使用Gateway
# 不指定model,让Gateway决定
)
# 执行任务
response = agent.run("帮我重构这段代码,优化性能")
print(f"使用模型: {response.model_used}")
print(f"Token消耗: {response.tokens_used}")
print(f"成本: ${response.cost:.4f}")
成本监控
# 查看实时统计
gateway.stats()
# 输出:
# {
# "total_requests": 1234,
# "by_model": {
# "claude-sonnet-4": {"requests": 500, "cost": 12.34},
# "gpt-4.5-turbo": {"requests": 400, "cost": 8.56},
# "deepseek-v3": {"requests": 334, "cost": 0.89}
# },
# "total_cost": 21.79,
# "cache_hit_rate": 0.23
# }
# 设置预算警报
gateway.set_budget_alert(
daily=10.0, # 日预算$10
callback=lambda msg: send_slack(msg)
)
自定义路由器
class SmartRouter:
def select_model(self, prompt: str, context: dict) -> str:
"""根据内容智能选择模型"""
# 代码相关
if any(kw in prompt.lower() for kw in ['code', 'debug', 'function']):
return 'claude-sonnet-4'
# 需要联网
if context.get('needs_web'):
return 'gpt-4.5-turbo' # 有更好的搜索插件
# 简单任务省钱
if len(prompt) < 200 and not context.get('complex'):
return 'deepseek-v3'
# 默认
return 'claude-sonnet-4'
# 使用自定义路由
gateway = Gateway(
config="gateway.yaml",
router=SmartRouter()
)
🔗 相关链接
- OpenClaw多模型路由策略 - 更深入的路由算法
- OpenClaw API成本优化 - 省钱秘籍
- OpenClaw模型故障转移 - 高可用架构
- OpenClaw响应缓存 - 相同请求不重复计费
- ClawHub入门指南 - 发现更多Skills
📊 效果对比
| 指标 | 使用Gateway前 | 使用Gateway后 |
|---|---|---|
| 月API成本 | $156 | $89 (-43%) |
| 请求失败率 | 2.3% | 0.1% (-95%) |
| 平均响应时间 | 3.2s | 2.1s (-34%) |
| 模型利用率 | 单一模型 | 4模型动态 |