成本优化——从"月薪烧Token"到"精打细算过日子"
收到API账单那天,我的表情和收到水电费的普通人一模一样——又涨价了?一算,上个月Agent跑了$847,其中60%花在不需要GPT-4的任务上。
Cost Optimization不是让你不花钱,而是把钱花在刀刃上。OpenClaw提供多层成本优化:模型路由让简单任务用便宜模型,缓存让重复请求零成本,批处理让单次调用最大化产出。
不是所有任务都需要GPT-4。OpenClaw的模型路由器根据任务复杂度自动选择最合适的模型:
| 任务类型 | 推荐模型 | 成本对比 |
|---|---|---|
| 简单分类/提取 | GPT-4o-mini | 省90% |
| 标准对话/写作 | GPT-4o | 省60% |
| 复杂推理/代码 | GPT-4 | 基准 |
| 超长上下文 | Claude 200K | 按需选择 |
# 模型路由配置
routing:
enabled: true
rules:
- match:
skill: simple_classification
model: gpt-4o-mini
max_tokens: 500
- match:
skill: content_generation
tokens_estimate: "< 2000"
model: gpt-4o
- match:
skill: complex_reasoning
model: gpt-4
thinking: enabled
# 默认规则
default:
model: gpt-4o
max_tokens: 4000
# 缓存配置
cache:
enabled: true
# LLM响应缓存
llm_cache:
backend: redis
ttl: 3600 # 1小时过期
max_size: 1000
similarity_threshold: 0.95 # 95%相似则命中
# 工具结果缓存
tool_cache:
backend: redis
ttl: 300 # 5分钟过期
max_size: 500
# 语义缓存(相似问题返回缓存结果)
semantic_cache:
enabled: true
embedding_model: text-embedding-3-small
similarity_threshold: 0.92
# 典型场景缓存命中率
场景 缓存命中率 成本节省
重复查询 85% 85%
定时报告生成 90% 90%
相似问题回答 60% 60%
工具调用结果 40% 40%
总体 65% 65%
# ❌ 浪费Token的Prompt
"请你作为一个专业的AI助手,帮我分析一下以下文本的主要内容和关键信息,
然后给出一个简洁的总结。注意要保持客观中立的立场,不要加入你个人的
主观判断和推测。文本内容如下:..."
# ✅ 精简后的Prompt
"总结以下文本要点(客观中立):..."
# Token节省: 120 → 18 (省85%)
# SOUL.md 输出控制
output:
max_tokens: 2000 # 限制最大输出
format: concise # 简洁模式
no_filler: true # 禁止废话
truncate: ellipsis # 超长截断
# 只传递必要信息给LLM
context:
strategy: selective
rules:
- include: user_message
- include: last_3_turns
- include: skill_instructions
- exclude: system_debug
- exclude: previous_tool_results
# 批处理配置
batch:
enabled: true
# 请求合并
merge:
enabled: true
max_batch_size: 20
max_wait_ms: 5000
merge_strategy: topic # 按主题合并
# 示例:10个独立请求 → 1个批处理请求
# 成本从 10 * $0.05 = $0.50 → 1 * $0.08 = $0.08 (省84%)
# 成本监控配置
cost_monitoring:
budget:
daily: $10
monthly: $200
alert_at: 80% # 80%时告警
breakdown:
by_agent: true
by_skill: true
by_model: true
by_time: hourly
reports:
schedule: daily
format: markdown
destination: feishu
# 每日成本报告示例
# 日期: 2026-05-07
# 总成本: $7.32 (预算$10, 剩余27%)
#
# 按模型:
# gpt-4o-mini: $1.20 (16%)
# gpt-4o: $4.12 (56%)
# gpt-4: $2.00 (27%)
#
# 按Skill:
# news_digest: $2.50 (34%)
# discord_post: $1.80 (25%)
# web_fetch: $1.20 (16%)