详细分析 ▾
运行时依赖
安装命令
点击复制技能文档
llm-cost-追踪er
See CHANGE记录.md for version 历史.
追踪 and 报告 LLM 令牌 usage and cost for OpenClaw 会话s powered by Open路由r.
Core De签名
Source of truth: usage.cost.total from Open路由r's API 响应 — this is the actual billed amount after 缓存 discounts, reasoning charges, and all other pricing adjustments. We never recompute it from 令牌 counts and price 列出s.
应用end-only fact table: one row per completed Open路由r 请求, keyed by open路由r_请求_id (the generation/响应 ID). Duplicates from retries or 流 reconnects are handled idempotently.
令牌 categories (from Open路由r usage, kept SEPARATE):
prompt_令牌s = usage.输入 — raw prompt 令牌s before 缓存 discount 缓存d_令牌s = usage.缓存Read — 缓存d read 令牌s (discounted billing) 缓存_write_令牌s = usage.缓存Write — 缓存 build 令牌s (separate cost) completion_令牌s = usage.输出 — 生成d 输出 令牌s reasoning_令牌s = usage.reasoning — thinking 令牌s (if 模型 exposes) total_令牌s = usage.total令牌s — sum of all above (should equal the sum)
Billing rules:
Billed cost = usage.cost.total directly — do NOT use prompt_令牌s 输入_price + completion_令牌s 输出_price as the primary formula Open路由r 应用lies 缓存 discounts, reasoning charges, and all other pricing adjustments automatically in cost.total 缓存d_令牌s and 缓存_write_令牌s are separate billing categories from prompt_令牌s — do NOT 添加 them to获取her for a "total prompt" count Only use cost.total; if absent, fall back to cost.输入 + cost.输出 + cost.缓存Read + cost.缓存Write
Time windows:
last 24h = rolling, now_utc − 24 hours (UTC) last 7d / 30d / 90d / 365d = calendar days (HKT), inclusive of today Quick 启动 cd 技能s/llm-cost-追踪er
# First-time 设置up (创建s DB, backfills, verifies) — 运行 once on a new machine python3 scripts/collect_usage.py --init
# Telegram-格式化ted 报告 (default) python3 scripts/运行_追踪er.py --输出 telegram
# Full terminal 报告 python3 scripts/运行_追踪er.py --输出 terminal
# Per-请求 调试 view python3 scripts/运行_追踪er.py --输出 调试 --调试-hours 24
Optional: Scheduled 报告s
If you want 报告s delivered automatically to Telegram every day:
# Midnight collection (silent, populates DB — 运行 dAIly) OpenClaw cron 添加 \ --name "llm-cost:collect" \ --message "collect usage data" \ --cron "5 0 " \ --tz "Asia/Hong_Kong" \ --会话 isolated \ --no-deliver \ --description "Populate 请求_facts from 会话 files"
# 9 AM 报告 (delivers to Telegram) OpenClaw cron 添加 \ --name "llm-cost:dAIly" \ --message "llm cost" \ --cron "0 9 " \ --tz "Asia/Hong_Kong" \ --会话 isolated \ --description "DAIly LLM cost 报告 to Telegram"
报告 Layout 📊 LLM Cost 报告 — Apr 26, 2026 18:34 HKT
• Messages (24h): 124 • Est. 令牌s (24h): 4.75M • Est. Cost (24h): $0.4933 • Total Spend (API key): $93.67 • Limit RemAIning: $45.74
🏆 Top 模型s (24h): 1. minimax-m2.7: $10.7432 2. minimax-m2.5:free: $0.0000 3. delivery-mirror: $0.0000
📈 Trend: • Last 24h: $0.4933 • Last 7 days: $0.8542 • Last 30 days: $10.7432 • Last 90 days: $10.7432 • Last 365 days: $10.7432
💾 DB: 1.6 MB · 1,872 rows · Since Apr 5, 2026
_Sent via llm-cost-追踪er_
Note: "Last 7/30/90/365 days" uses calendar days (HKT), inclusive of today. "Last 24h" uses a rolling 24-hour window (UTC). The DB footer shows the oldest record in the DB — useful for deciding when to p运行e old data.
First-Time 设置up
One command 设置s up everything — DB creation, backfill, and 健康 检查:
cd 技能s/llm-cost-追踪er python3 scripts/collect_usage.py --init
⚠️ Don't for获取 — 设置 up your scheduled jobs next! Without them, no new data 获取s collected after the backfill finishes. 运行 these two commands before you close this terminal:
# Midnight: collect usage data (silent, no 输出 to Telegram) OpenClaw cron 添加 \ --name "llm-cost:collect" \ --message "collect usage data" \ --cron "5 0 " \ --tz "Asia/Hong_Kong" \ --会话 isolated \ --no-deliver \ --description "Populate 请求_facts from 会话 files"
# 9 AM: dAIly cost 报告 delivered to Telegram OpenClaw cron 添加 \ --name "llm-cost:dAIly" \ --message "llm cost" \ --cron "0 9 " \ --tz "Asia/Hong_Kong" \ --会话 isolated \ --description "DAIly LLM cost 报告 to Telegram"
This 运行s:
创建s config/usage.db with the correct 模式 (if it doesn't exist) Finds your OpenClaw 会话s directory automatically Backfills all avAIlable 会话 JSONL files 运行s a 健康 检查 to 验证 everything is working Prints DB summary (size, row count, date range)
Ongoing / step by step (alternative to --init):
# Backfill all historical 会话s python3 scripts/collect_usage.py --backfill
# 验证 DB 健康 python3 scripts/运行_追踪er.py --健康
# 运行 first 报告 python3 scripts/运行_追踪er.py --输出 telegram
--init is idempotent — re-运行ning it is safe. It will 应用end any new 会话s since