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:

  • Turns: 232
  • Total context processed: 40.08M tokens
  • Peak single-turn context: 352.7k tokens (crossed 80k on turn 11 and rode 221 turns without compacting)
  • Apparent cache hit rate: 97.4%
  • Effective cache hit rate: 10.9%
  • Estimated spend (list rates): $16.01
  • Recoverable waste: $12.06 (75.3%)
  • Detected events: 2 cache-bust cliffs (99% -> 0% on a 210k window), 6 consecutive Bash retry loops, 2 oversized tool blobs (including a 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.

Exact JSONL Fields Across Agent Logs

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):

  1. Anthropic / Claude Code (usage):
    • input_tokens, output_tokens
    • cache_read_input_tokens (fallback: cache_read_tokens)
    • cache_creation_input_tokens (fallback: cache_creation_tokens)
  2. OpenAI Responses / Codex CLI (event_msg token_count -> last_token_usage or diff of cumulative total_token_usage):
    • input_tokens, output_tokens, cached_input_tokens
    • Note: cached_input_tokens is a subset of input_tokens, so uncached input is max(0, input_tokens - cached_input_tokens).
  3. OpenAI Chat Completions (usage):
    • prompt_tokens, completion_tokens, prompt_tokens_details.cached_tokens
  4. Gemini (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 

How Effective Cache Hit Is Computed

To separate productive cached prefix reads from wasted re-reads, the engine tracks perTurnWastedCr[k] on every turn k across three waste buckets:

  1. Uncompacted tail past 80,000 tokens (default compactThreshold = 80_000, postCompactTarget = 20_000):
    • Once context crosses 80k, every subsequent turn carries excess = max(0, total_context - 20_000).
    • Wasted cached reads on turn k: round(cache_read_tokens * (excess / total_context)).
    • Estimated dollar waste on turn k: turn_cost * (excess / total_context) * 0.85.
  2. Consecutive identical tool-retry loops (loopThreshold = 3 turns):
    • When the same tool signature repeats >= 3 consecutive turns, every repeated turn after the first counts its full cache_read_tokens as wasted.
  3. Oversized tool output blobs (bloatCharThreshold = 24_000 chars or bloatDeltaThreshold = 15_000 context jump on a non-cache-bust turn):
    • Excess tokens above 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%.

Running It Locally (Zero Upload)

Private repo transcripts should never leave your machine, so both interfaces run offline:

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.