feat(analytics): add cc-economics --audit for cache-lifecycle savings#2953
Open
thebigjc wants to merge 3 commits into
Open
feat(analytics): add cc-economics --audit for cache-lifecycle savings#2953thebigjc wants to merge 3 commits into
thebigjc wants to merge 3 commits into
Conversation
Adds an opt-in `--audit` mode to `rtk cc-economics` that attributes token savings by reading Claude Code session logs (~/.claude/projects/**/*.jsonl) instead of the flat weighted-CPT estimate. Each rtk-compressed command's saved tokens are credited across the session turns that follow it, bucketed by each turn's actual Anthropic pricing tier: - cache write (1.25x): tool-result appearance + cache-eviction rebuilds - cache read (0.10x): steady cached turns Matching is bounded by an authoritative cap: only Claude Bash events invoked as `rtk ...` can match, scoped to the same project (cwd), so matched count can never exceed the number of rtk commands Claude actually drove. The cap is surfaced in the output (`Claude rtk calls: X/Y matched`) for self-validation. Notes: - rtk records its timestamp after command completion, so the match window is 60s (recovers ~99.9% of invocations; the rtk-invocation + project filters keep it unambiguous). - Unmatched commands (terminal/codex) earn no savings: they never entered an LLM context. - Tool results always enter the cached prefix via cache_creation (validated with `claude -p`), so there is no separate input-rate savings bucket. Tracker gains `get_raw_commands()` (timestamp, project_path, saved_tokens) for the audit; unparseable timestamp rows are skipped rather than coerced.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
--auditmode tortk cc-economicsthat attributes token savings by reading Claude Code /pisession logs (~/.claude/projects/**/*.jsonland~/.pi/agent/sessions/**/*.jsonl) instead of using a flat weighted-CPT estimate.cwd) with a 60-second window, capped by the authoritative count of actualrtkcommands invoked.$0input savings row from the audited output since tool results structurally always enter the cached prefix via cache creation (writes), leaving plain input savings at$0.src/core/tracking.rstests by synchronizing database-touching tests with a sharedMutex.Example Output
Running
rtk cc-economics --auditdisplays the session-log audited prompt caching savings breakdown:Test plan
cargo fmt --all && cargo clippy --all-targets && cargo test(all 2437+ tests pass successfully)rtk cc-economics --auditparses session logs correctly, mapsrtkcommands to Claude turns, and displays precise caching savings.