Session Token Ledger
Analyze local OpenClaw session-token usage from a generated ledger stored under this skill.
What lives here
scripts/rebuild_sqlite.py— rebuild the local ledger from OpenClaw session transcriptsscripts/report.py— generate markdown reports for all sessions or one sessionreferences/overview.md— schema, views, and design notesreferences/queries.sql— canned SQLite queries for common auditsreferences/hook-setup.md— optional OpenClaw hook setup for automatic rebuildsreferences/ANOMALIES.md— populated after rebuild with suspicious or bad transcript linesassets/session_tokens.db— populated after rebuild; SQLite ledgerassets/index.json— populated after rebuild; machine-readable session indexassets/TOTAL_TOKENS.txt— populated after rebuild; quick aggregate summaryassets/YYYY-MM-DD_N.md— populated after rebuild; one markdown file per completed session
Default workflow
- Rebuild the ledger with
python3 scripts/rebuild_sqlite.pyif data looks stale. - Start with
assets/index.jsonfor session list and top-level totals. - If the user wants trends or rankings, inspect
assets/session_tokens.db. - If the user wants a narrative for one session, read the matching
assets/YYYY-MM-DD_N.mdfile. - If the user wants root-cause analysis, inspect these views first:
largest_sessionsusage_efficiencybloated_sessionstop_context_hogsdaily_efficiency
- If something looks wrong, read
references/ANOMALIES.md.
Read these references only when needed
- Read
references/overview.mdwhen you need schema or view meaning. - Read
references/queries.sqlwhen you need canned SQL. - Read
references/hook-setup.mdwhen you want automatic rebuilds after/new,/reset,/stop, or gateway startup.
Quick path
Generate reports:
python3 scripts/report.py
python3 scripts/report.py --session SESSION_ID
python3 scripts/report.py --save
python3 scripts/report.py --session SESSION_ID --save
Use read-only SQLite queries when available:
sqlite3 -readonly assets/session_tokens.db "SELECT * FROM overall_summary;"
sqlite3 -readonly assets/session_tokens.db "SELECT * FROM largest_sessions LIMIT 10;"
sqlite3 -readonly assets/session_tokens.db "SELECT * FROM bloated_sessions LIMIT 10;"
sqlite3 -readonly assets/session_tokens.db "SELECT * FROM top_context_hogs LIMIT 10;"
sqlite3 -readonly assets/session_tokens.db "SELECT * FROM daily_efficiency ORDER BY date DESC;"
For one session:
sqlite3 -readonly assets/session_tokens.db "SELECT * FROM usage_efficiency WHERE session_id='SESSION_ID';"
If sqlite3 CLI is unavailable, use the bundled Python scripts instead.
Reporting rules
- Lead with the plain-English conclusion.
- Separate total tokens, input tokens, output tokens, and cache read.
- Call out whether waste came from long context, too many topic switches, long outputs, or repeated tool/doc loading.
- For subscription-style billing, emphasize token totals and efficiency, not fake precision on dollar cost.
- When giving recommendations, prefer a short ranked list over a long essay.
Boundaries
- Treat this ledger as local analysis data, not ground truth for provider billing.
- Do not modify the database unless the user explicitly asks to rebuild or update the ledger.
- Rebuilds skip the currently active live
.jsonlsession when a matching.lockfile exists, so the ledger defaults to completed sessions only. - Prefer querying the DB over manually re-deriving totals from raw session logs unless the ledger appears stale or broken.