claude-code-token-optimizer
A session can spend real money without any obvious moment where a read was wasteful or a search should have been scoped. Claude Code writes every turn to a local JSONL transcript including full usage metadata — input tokens, output tokens, cache reads/writes, tool calls, compactions — but surfaces none of it.
This extension reads those transcripts locally and answers two questions: where is the money going, and what specifically should I do differently?
Ten detectors
Each rule is one file, each carries its own token and cost estimate:
- Context compactions
- Context-pressure warnings before an auto-compaction
- Broad or unscoped
grep/rg/findsearches - Uncapped
cat/git diff/git logoutput dumps - Oversized or repeated reads of the same file
- Reading a file back immediately after writing it
- Redundant read-only inspection commands
- Failed or interrupted tool calls whose output was billed and discarded
- Low cache reuse — the signature of a volatile prompt prefix
- High-input, low-output round trips
Findings roll up into recoverable-dollar totals by category, split into one-off fixes versus habits, alongside a daily efficiency series (cache-reuse percentage, cost per day, compactions) that is persisted so history survives transcript pruning.
Decisions worth noting
The data layer has no vscode dependency — parser, model, pricing, rules, and
aggregation are pure TypeScript tested against fixtures and a temporary home directory
tree. That’s what makes the rules engine testable at all.
Cost estimates are honest. Claude Code’s local stats report costUSD: 0, so the
extension ships an editable rate table with cache reads at 0.1x and cache writes at
1.25–2x. The README says plainly that on an already-efficient setup recoverable waste
can legitimately be small. I didn’t inflate the numbers.
Regression alerts require at least three active sessions and 20k tokens per comparison window, so a quiet week doesn’t trigger a false alarm.
Fully local. No telemetry, no external calls, zero runtime dependencies, 38 unit tests.