← 返回
安全合规 中文

Token Ledger (SQLite)

Audit-grade token and cost ledger for OpenClaw. Use when you need to (1) record every model call’s usage (input/output/cache read/cache write/cost) into SQLi...
OpenClaw 的审计级 Token 与费用账本。用于将每次模型调用的用量(输入/输出/缓存读取/缓存写入/费用)记录到 SQLite 数据库中。
jonathanjing
安全合规 clawhub v0.1.0 1 版本 100000 Key: 无需
★ 0
Stars
📥 684
下载
💾 10
安装
1
版本
#latest

概述

Token Ledger (SQLite)

What this skill provides

  • A SQLite ledger at ~/.openclaw/ledger.db with per-call usage rows.
  • A watcher daemon that tails OpenClaw session JSONL files and writes usage into SQLite (near-real-time).
  • Deterministic, low-token SQL-first finance reports (no JSONL rescans).

This skill is designed to be public/reusable: prefer stable paths, versioned pricing (price_versions table), and minimal assumptions.

Canonical usage definitions (do not mix these)

  • input_tokens: uncached input tokens for the call (can be tiny)
  • cache_write_tokens: tokens written to cache (can be huge)
  • cache_read_tokens: tokens read from cache (can be huge)
  • output_tokens: generated tokens
  • total_context_tokens (effective prompt size) = input_tokens + cache_write_tokens + cache_read_tokens

Files & paths

  • SQLite DB: ~/.openclaw/ledger.db
  • Checkpoint: ~/.openclaw/ledger-checkpoint.json
  • Sessions JSONL source: ~/.openclaw/agents/main/sessions/*.jsonl

Skill scripts:

  • scripts/ledger_watcher.py — watcher daemon (supports --once)
  • scripts/ledger_schema.sql — DDL
  • scripts/com.openclaw.token-ledger-watcher.plist — LaunchAgent template

Standard operations (use exec)

1) One-shot backfill (safe)

python3 ~/.openclaw/workspace/skills/token-ledger/scripts/ledger_watcher.py --once

2) Install / start daemon (macOS LaunchAgent)

This renders the plist with your local $HOME (no hard-coded username paths):

python3 ~/.openclaw/workspace/skills/token-ledger/scripts/render_plist.py \
  > ~/Library/LaunchAgents/com.openclaw.token-ledger-watcher.plist
launchctl load ~/Library/LaunchAgents/com.openclaw.token-ledger-watcher.plist
launchctl list | rg token-ledger-watcher

3) Stop daemon

launchctl unload ~/Library/LaunchAgents/com.openclaw.token-ledger-watcher.plist

4) Quick sanity query

sqlite3 ~/.openclaw/ledger.db \
  "select provider, model, 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-token Finance reports

Preferred flow:

1) Run SQL queries directly against ledger.db.

2) Format results with a deterministic template (no long reasoning).

3) Only if numbers look anomalous: drill into calls for the specific session/model.

For daily reports, use:

  • per-model totals
  • cached vs uncached mix
  • top sessions by cost
  • cost_source breakdown (provider|calculated|local|unknown)

Notes / caveats

  • Provider billing can still exceed ledger totals due to retries/timeouts/streaming interruptions. Ledger is auditable, not magical.
  • Keep pricing versioned. Do not retroactively reprice historical calls unless explicitly requested.

Preset queries (safe)

python3 ~/.openclaw/workspace/skills/token-ledger/scripts/ledger_query.py today
python3 ~/.openclaw/workspace/skills/token-ledger/scripts/ledger_query.py history --days 30
python3 ~/.openclaw/workspace/skills/token-ledger/scripts/ledger_query.py top-sessions --days 7 --limit 20

Deterministic daily report (no LLM)

python3 ~/.openclaw/workspace/skills/token-ledger/scripts/ledger_report_daily.py

版本历史

共 1 个版本

  • v0.1.0 当前
    2026-03-30 16:49 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

data-analysis

openclaw-dashboard

jonathanjing
OpenClaw实时运维仪表盘,监控会话、成本、定时任务及网关健康状态。用于安装、启动等服务操作。
★ 11 📥 4,613
security-compliance

OpenClaw Backup

alex3alex
备份与恢复 OpenClaw 数据。适用于创建备份、设置自动备份计划、从备份恢复或管理备份轮转。处理 ~/.openclaw 目录归档并包含适当的排除规则。
★ 89 📥 30,594
security-compliance

Skill Vetter

spclaudehome
AI智能体技能安全预审工具。安装ClawdHub、GitHub等来源技能前,检查风险信号、权限范围及可疑模式。
★ 1,212 📥 266,272