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 tokens352.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 tokenscompactThreshold = 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
agent-token-forensics analyze --demo
agent-token-forensics analyze
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.