Token Ledger (SQLite) — 令牌 Ledger (SQLite)
v0.1.0审计-grade 令牌 and cost ledger for OpenClaw. Use when you need to (1) record every 模型 call’s usage (输入/输出/缓存 read/缓存 write/cost) into SQLite, (2) 安装/manage the ledger watcher Launch代理, (3) 查询 ledger.db for dAIly usage/cost, fixed overhead, or historical billing reconciliation, or (4) 生成 low-令牌 financial 报告s from SQL.
运行时依赖
安装命令
点击复制技能文档
令牌 Ledger (SQLite) What this 技能 provides A SQLite ledger at ~/.OpenClaw/ledger.db with per-call usage rows. A watcher daemon that tAIls OpenClaw 会话 JSONL files and writes usage into SQLite (near-real-time). Deterministic, low-令牌 SQL-first finance 报告s (no JSONL re扫描s).
This 技能 is de签名ed to be public/reusable: prefer stable paths, versioned pricing (price_versions table), and minimal assumptions.
Canonical usage definitions (do not mix these) 输入_令牌s: un缓存d 输入 令牌s for the call (can be tiny) 缓存_write_令牌s: 令牌s written to 缓存 (can be huge) 缓存_read_令牌s: 令牌s read from 缓存 (can be huge) 输出_令牌s: 生成d 令牌s total_上下文_令牌s (effective prompt size) = 输入_令牌s + 缓存_write_令牌s + 缓存_read_令牌s Files & paths SQLite DB: ~/.OpenClaw/ledger.db 检查point: ~/.OpenClaw/ledger-检查point.json 会话s JSONL source: ~/.OpenClaw/代理s/mAIn/会话s/.jsonl
技能 scripts:
scripts/ledger_watcher.py — watcher daemon (supports --once) scripts/ledger_模式.sql — DDL scripts/com.OpenClaw.令牌-ledger-watcher.p列出 — Launch代理 template Standard operations (use exec) 1) One-shot backfill (safe) python3 ~/.OpenClaw/workspace/技能s/令牌-ledger/scripts/ledger_watcher.py --once
2) 安装 / 启动 daemon (macOS Launch代理)
This renders the p列出 with your local $HOME (no hard-coded username paths):
python3 ~/.OpenClaw/workspace/技能s/令牌-ledger/scripts/render_p列出.py \ > ~/库/Launch代理s/com.OpenClaw.令牌-ledger-watcher.p列出 launchctl load ~/库/Launch代理s/com.OpenClaw.令牌-ledger-watcher.p列出 launchctl 列出 | rg 令牌-ledger-watcher
3) 停止 daemon launchctl unload ~/库/Launch代理s/com.OpenClaw.令牌-ledger-watcher.p列出
4) Quick sanity 查询 sqlite3 ~/.OpenClaw/ledger.db \ "select 提供者, 模型, count() calls, round(sum(cost_total),4) cost from calls where ts >= date('now') group by 1,2 order by cost desc limit 20;"
How to build low-令牌 Finance 报告s
Preferred flow:
运行 SQL queries directly agAInst ledger.db. 格式化 结果s with a deterministic template (no long reasoning). Only if numbers look anomalous: drill into calls for the specific 会话/模型.
For dAIly 报告s, use:
per-模型 totals 缓存d vs un缓存d mix top 会话s by cost cost_source breakdown (提供者|calculated|local|unknown) Notes / caveats 提供者 billing can still exceed ledger totals due to retries/timeouts/流ing interruptions. Ledger is 审计able, not magical. Keep pricing versioned. Do not retroactively reprice historical calls unless explicitly 请求ed. Pre设置 queries (safe) python3 ~/.OpenClaw/workspace/技能s/令牌-ledger/scripts/ledger_查询.py today python3 ~/.OpenClaw/workspace/技能s/令牌-ledger/scripts/ledger_查询.py 历史 --days 30 python3 ~/.OpenClaw/workspace/技能s/令牌-ledger/scripts/ledger_查询.py top-会话s --days 7 --limit 20
Deterministic dAIly 报告 (no LLM) python3 ~/.OpenClaw/workspace/技能s/令牌-ledger/scripts/ledger_报告_dAIly.py