Token分析
成本优化
预算管理
很多团队部署了 OpenClaw Agent 后,只知道「用了很多 Token」,但从来不清楚:
# openclaw-config.yaml
telemetry:
enabled: true
token_tracking: detailed # basic | detailed | full
exporters:
- type: prometheus
endpoint: "http://monitoring:9090"
- type: file
path: "/var/log/openclaw/token-usage.jsonl"
rotation:
size: "100MB"
retention: 7
- type: dashboard
port: 8080
# 定义成本模型
cost_models:
- model: "gpt-4o"
input_per_1M: $2.50
output_per_1M: $10.00
- model: "claude-3-5-sonnet"
input_per_1M: $3.00
output_per_1M: $15.00
# 启动 Token 仪表盘
openclaw cost dashboard --port 8080
# 输出示例
┌─────────────────────────────────────┐
│ Token Dashboard (最后24小时) │
├─────────────────────────────────────┤
│ 总消耗:12,847,394 Tokens │
│ 总花费:$28.43 │
│ │
│ 按 Agent 排名: │
│ 1. seo-agent 5.2M tokens $11.4 │
│ 2. code-review 3.1M tokens $6.8 │
│ 3. discord-bot 2.4M tokens $5.3 │
│ │
│ 上下文利用率: │
│ 平均:64.3% 浪费:35.7% │
└─────────────────────────────────────┘
| 策略 | 省 Token | 说明 |
|---|---|---|
| 上下文压缩 | 40-60% | 自动压缩历史对话,保留关键信息 |
| Token 预算控制 | 20-30% | 每个 Agent 设置对话 Token 上限 |
| 结果缓存 | 15-25% | 相同输入直接返回缓存结果 |
| 模型路由 | 30-50% | 简单任务用小模型,复杂任务用大模型 |
| 会话剪枝 | 10-20% | 定期清理超长会话 |
# budget-policy.yaml
budget_policies:
- agent: seo-agent
monthly_budget: $100
daily_limit: $10
actions:
on_exceed_daily: "reduce_context_to_50%"
on_exceed_monthly: "pause_agent"
- agent: code-review
per_task_budget: $0.50
max_retries: 2
actions:
on_budget_exceeded: "switch_to_cheaper_model"
# 智能模型路由
model_routing:
- skill: web-search
model: "claude-3-haiku" # 搜索任务用便宜模型
- skill: code-analysis
model: "gpt-4o" # 代码分析用强大模型
- skill: content-writing
model: "claude-3-5-sonnet" # 写作用均衡模型
# 定时生成 Token 报告
openclaw cron create \
--name "token-report" \
--schedule "0 8 * * 1" # 每周一早上8点
--action "openclaw cost report --format html --output weekly-report.html"
# 生成对比报告
openclaw cost compare --period "last_30_days" --output cost-trend.html
# 输出:显示 Token 消耗趋势、成本变化、优化建议
妙趣AI | OpenClaw 教程合集 | 最后更新:2026-05-22