feat: add cross-project session cost tracking ledger#1487
feat: add cross-project session cost tracking ledger#1487YoungMoneyInvestments wants to merge 2 commits intoruvnet:mainfrom
Conversation
Adds cost-ledger.cjs that tracks Claude Code session costs across all projects on a machine. Records cost, duration, model, and lines changed to a global JSONL ledger at ~/.claude/cost-ledger.jsonl. Features: - Automatic recording via session-end hook integration - Reports: --daily, --weekly, --monthly, --annual - Filter by --project <name> - JSON output for programmatic use - Installed globally during init so it works cross-project The hook handler's session-end now calls cost-ledger.cjs record with the session data from Claude Code's stdin, then the report command provides spend visibility that was previously unavailable. Co-Authored-By: claude-flow <ruv@ruv.net>
xkonjin
left a comment
There was a problem hiding this comment.
Code Review: Cost Ledger Feature
Overall this is a well-designed addition. A few observations:
Strengths:
- Clean separation between
recordandreportmodes - Good defensive programming with try/catch around the hook invocation (cost tracking should never break session end)
- Nice touch with the color-coded cost thresholds in reports ($50+ yellow warning)
- JSON output option for programmatic consumption
Minor considerations:
-
The
readStdinSync()function reads in 4KB chunks but the expected input is small JSON. Could simplify to a single read, but current approach is safe. -
In
record(), when both cost and duration are 0, the function returns early. Consider logging this case for debugging since it might indicate the hook isn't receiving proper cost data from Claude Code. -
The project name extraction from git remote uses a simple regex. Consider handling SSH-style URLs - the pattern should catch these but worth verifying.
-
The ledger grows unbounded (JSONL append). For long-term use, consider a rotation/compaction strategy. Not blocking for this PR.
The feature is well-tested in concept and the implementation follows good practices. LGTM pending any tests you want to add.
Summary
Adds
cost-ledger.cjs— a cross-project session cost tracking system that records and reports Claude Code API spend over time.Problem: Claude Code shows per-session cost in the statusline ($X.XX), but this data is lost when the session ends. There's no way to see daily, weekly, monthly, or annual spend across projects.
Solution: A JSONL-based cost ledger that:
session-endhook~/.claude/cost-ledger.jsonl(global, cross-project)Changes
helpers-generator.ts: AddedgenerateCostLedger()function (new helper), wired cost recording intosession-endhandler, added togenerateHelpers()mapexecutor.ts: Installscost-ledger.cjsto both project.claude/helpers/and global~/.claude/helpers/during initUsage
Ledger Format (JSONL)
{"date":"2026-03-30","timestamp":"2026-03-30T20:27:10.888Z","project":"matrix-lstm","session_id":"abc123","cost_usd":3.68,"duration_min":110.7,"model":"claude-opus-4-6","lines_added":552,"lines_removed":20}Design Decisions
~/.claude/) — works across all projects on the machinefs,path,os,child_processTest plan
npx @claude-flow/cli@latest init --only-claude --forcein a projectcost-ledger.cjsappears in both.claude/helpers/and~/.claude/helpers/~/.claude/cost-ledger.jsonlnode ~/.claude/helpers/cost-ledger.cjs reportand verify output--daily,--weekly,--monthly,--annual,--jsonflags--project <name>filteringcost-ledger.cjsis missing (graceful fallback)🤖 Generated with claude-flow