OpenClaw Cost Guard
Use this skill when you need:
- - accurate cost reports (daily/weekly/lifetime)
- top expensive sessions
- guardrails to reduce token burn (without changing config unless user asks)
1) Data source (important)
Prefer session JSONL logs (they contain per-call usage, often with real USD cost):
- - OpenClaw: INLINECODE1
- Legacy/compat: INLINECODE2
Do not estimate from “current context window” style token fields.
2) Quick commands
Daily costs (last 7 days)
CODEBLOCK0
Today / yesterday
CODEBLOCK1
Top expensive sessions
CODEBLOCK2
JSON output (for dashboards)
CODEBLOCK3
3) If cost is missing (fallback estimate)
Some providers may omit usage.cost. You can provide per-1M-token prices:
CODEBLOCK4
4) Budget alerts
The extractor can run as a budget check:
CODEBLOCK5
- - If budget is exceeded, it prints an ALERT and exits with code 2 (default).
- For non-failing checks:
CODEBLOCK6
Wiring it to a cron alert (recommended)
Run it daily (or hourly) and if exit code is 2, send yourself a Telegram message.
(Implementation depends on your OpenClaw channel setup; do not embed secrets in scripts.)
5) Token-saving playbook (teach the AI)
When the user says “use as few tokens as possible”, apply:
- - Default response budget: 1–6 lines, bullets > paragraphs
- Ask 1 question max (only if truly blocking)
- Progressive disclosure: offer details only if asked
- Tool calls: batch; avoid repeated
status/browser calls - No log dumps into chat; summarize + point to file path
- Hard limits: max 3 web iterations (search/fetch) per task
Optional phrasing to keep yourself in check:
"Answer in <=6 lines. If more is needed, ask permission."
技能名称: openclaw-cost-guard
详细描述:
OpenClaw 成本防护
当你需要以下内容时使用此技能:
- - 准确的成本报告(每日/每周/全周期)
- 最昂贵的会话
- 减少令牌消耗的防护措施(除非用户要求,否则不更改配置)
1) 数据源(重要)
优先使用 会话 JSONL 日志(其中包含每次调用的 usage,通常带有 实际美元成本):
- - OpenClaw:~/.openclaw/agents//sessions/.jsonl
- 旧版/兼容版:~/.clawdbot/agents//sessions/.jsonl
不要从“当前上下文窗口”类型的令牌字段进行估算。
2) 快速命令
每日成本(最近7天)
bash
python3 {baseDir}/scripts/extract_cost.py --last-days 7
今天/昨天
bash
python3 {baseDir}/scripts/extract_cost.py --today
python3 {baseDir}/scripts/extract_cost.py --yesterday
最昂贵的会话
bash
python3 {baseDir}/scripts/extract_cost.py --top-sessions 10
JSON 输出(用于仪表盘)
bash
python3 {baseDir}/scripts/extract_cost.py --last-days 30 --json
3) 如果成本缺失(回退估算)
某些提供商可能省略 usage.cost。你可以提供每百万令牌的价格:
bash
export PRICE_INPUT=1.75
export PRICE_OUTPUT=14
export PRICECACHEREAD=0.175
export PRICECACHEWRITE=0
python3 {baseDir}/scripts/extract_cost.py --last-days 7
4) 预算警报
提取器可以作为预算检查运行:
bash
python3 {baseDir}/scripts/extract_cost.py --today --budget-usd 5
- - 如果超出预算,它会打印一条警报并以代码 2 退出(默认)。
- 对于非失败检查:
bash
python3 {baseDir}/scripts/extract_cost.py --today --budget-usd 5 --budget-mode warn
将其连接到定时任务警报(推荐)
每天(或每小时)运行一次,如果退出代码为2,则向自己发送一条Telegram消息。
(具体实现取决于你的OpenClaw频道设置;不要在脚本中嵌入密钥。)
5) 令牌节省策略(指导AI)
当用户说“尽可能少用令牌”时,应用以下规则:
- - 默认响应预算: 1–6行,要点优于段落
- 最多问1个问题(仅在真正阻塞时)
- 渐进式披露: 仅在询问时提供详细信息
- 工具调用: 批量处理;避免重复的 status/浏览器调用
- 不要将日志转储到聊天中; 总结并指向文件路径
- 硬性限制: 每个任务最多3次网络迭代(搜索/获取)
可选的自我约束措辞:
“回答不超过6行。如果需要更多,请先请求许可。”