Why a 97.4% Prompt-Cache Hit Rate Still Hid $12.06 of Waste in One 232-Turn Session A developer built an offline token-forensics tool, agent-token-forensics (Python stdlib CLI) and token-audit-engine.js (browser), that parses coding-agent JSONL transcripts from four provider log formats into a common token 4-tuple and separates productive cache reads from wasted re-reads. On a real 232-turn session with a 97.4% headline prompt-cache hit rate, the tool attributed $12.06 (75.3%) of $16.01 in spend to waste, including two cache-bust cliffs, six Bash retry loops, and a 16k-token command output re-read 219 times, dropping the effective cache hit rate to 10.9%. Last week I pulled a real 232-turn local transcript from our Windows/Linux ISP ops repo because weekly coding-agent quota kept draining even though the headline prompt-cache hit rate looked healthy at 97.4% . Parsing the raw JSONL turn-by-turn showed why aggregate cache hit percentages mislead you on long sessions: 232 40.08M tokens 352.7k tokens crossed 80k on turn 11 and rode 221 turns without compacting 97.4% 10.9% $16.01 $12.06 75.3% 2 cache-bust cliffs 99% - 0% on a 210k window , 6 consecutive Bash retry loops, 16k -token command output on turn 13 re-read 219 times A 97.4% hit rate sounds great until you notice that 90% of those cached reads are re-reading stale history and giant command dumps across 200+ turns. The offline parser in token-audit-engine.js browser and agent-token-forensics Python stdlib CLI normalizes four provider log shapes into the same 4-tuple input tokens, output tokens, cache read tokens, cache write tokens : usage : input tokens , output tokens cache read input tokens fallback: cache read tokens cache creation input tokens fallback: cache creation tokens event msg token count - last token usage or diff of cumulative total token usage : cached input tokens max 0, input tokens - cached input tokens . prompt tokens , completion tokens , prompt tokens details.cached tokens usageMetadata : promptTokenCount , candidatesTokenCount , cachedContentTokenCount Per turn, total context and apparent cache hit are: total context = input tokens + cache read tokens + cache write tokens cache hit ratio = total context 0 ? cache read tokens / total context : 0 To separate productive cached prefix reads from wasted re-reads, the engine tracks perTurnWastedCr k on every turn k across three waste buckets: 80,000 tokens compactThreshold = 80 000 , postCompactTarget = 20 000 : excess = max 0, total context - 20 000 . k : round cache read tokens excess / total context . turn cost excess / total context 0.85 . loopThreshold = 3 turns : = 3 consecutive turns, every repeated turn after the first counts its full bloatCharThreshold = 24 000 chars or bloatDeltaThreshold = 15 000 context jump on a non-cache-bust turn : 2,000 excessBlob = max 0, blobTokens - 2 000 are billed once at ingest cache write or input rate and then carried at cache read rate on every subsequent turn k pos , contributing min cache read tokens, excessBlob to perTurnWastedCr k . We take the per-turn maximum across those buckets so overlapping waste is never double-counted, clamp to cache read tokens , sum totalWastedCacheReadTokens , and compute: effective cache hit = total context sum 0 ? Math.max 0, total cache read - total wasted cache read / total context sum : 0 Separately, the cache-bust cliff detector fires whenever the previous turn was warm total context = 10 000 and cache hit ratio = 0.7 and the current turn has total context = 10 000 with cache hit ratio < 0.3 . Extra dollar cost is input tokens inRate - cacheReadRate + cache write tokens cacheWriteRate - cacheReadRate / 1 000 000 . Per-turn dollar waste is deduplicated and capped at 95% of the turn's cost: combinedTurnWaste = Math.min turnCost 0.95, Math.max loopWaste, bustWaste + Math.max tailWaste, bloatWaste In our 232-turn trace, total cache read / total context sum was 97.4% , while total cache read - total wasted cache read / total context sum dropped to 10.9% . Private repo transcripts should never leave your machine, so both interfaces run offline: v0.1.1 , 0 runtime deps, 39 tests on Win/Linux/macOS CI : pipx install agent-token-forensics Preview on built-in synthetic trace agent-token-forensics analyze --demo Auto-scan local Claude Code / Codex / Antigravity JSONL logs agent-token-forensics analyze Expose strictly read-only stdio MCP tools analyze transcripts, forensics about agent-token-forensics mcp-serve Scope note: this is strictly read-only offline forensics. Dollar figures are deterministic estimates from logged token counts and public list rates, not provider invoices, and the tool does not intercept or auto-compact live sessions. What worked best on our own repo after reading these traces: compacting hard around 80k-120k , piping noisy shell commands through head / tail / rg or writing to a scratch file, and stopping identical retry loops after 2 failures.