ai newsDi., 1. Dez.

Where did my Claude Code usage go? Finding prompt-cache rebuilds in your own transcripts

The scripts are in https://github.com/jalnanco/claude-cache-audit ( cache_audit.py , make_fake_logs.py , test_cache_audit.py , check.sh ). A question that keeps coming up in r/ClaudeCode: "I barely did anything and burned X% of my weekly limit." Often the work itself was small and the prompt cache

dev.toSonntag 20:20 · 29.11.4 Min. Lesezeit
Bild: dev.to

The scripts are in https://github.com/jalnanco/claude-cache-audit ( cache_audit.py, make_fake_logs.py, test_cache_audit.py, check.sh ).

A question that keeps coming up in r/ClaudeCode: "I barely did anything and burned X% of my weekly limit." Often the work itself was small and the prompt cache got rebuilt. A rebuild bills the whole conversation again at write price instead of read price.

This exercise uses fake transcripts, so anyone can run it and get the same numbers. Then you point the same script at your own ~/.claude/projects/ folder.

Every API response that Claude Code logs carries a usage block:

"usage": { "input_tokens": 3, "cache_read_input_tokens": 21500, "cache_creation_input_tokens": 800, "cache_creation": { "ephemeral_5m_input_tokens": 800, "ephemeral_1h_input_tokens": 0 }, "output_tokens": 200 } Enter fullscreen mode Exit fullscreen mode On a healthy turn, most of the prompt is cache_read and only the new part is cache_creation. On a rebuilt turn it flips: almost everything is cache_creation.

Relative prices (Anthropic prompt-caching docs, per base input token):

So a 20,000-token prefix costs 2,000 units when it is read from cache (at 0.1) and 25,000 units when it is rewritten. That is 12.5 times more for the same turn.

The cache lives for 5 minutes by default (or 1 hour), and each hit resets the timer. A change to tools, the system prompt or an earlier message breaks the cache from that point on.

Compaction breaks it too, on purpose. When /compact runs, or auto-compact kicks in near the context limit, Claude Code swaps the history for a summary, and the next call has to write that new prefix. The transcript marks the spot with a line like this:

{ "type": "system", "subtype": "compact_boundary", "timestamp": "...", "sessionId": "...", "compactMetadata": { "trigger": "auto", "preTokens": 267114, "postTokens": 33830 } } Enter fullscreen mode Exit fullscreen mode A rebuild right after one of these lines is the normal cost of compacting, and the cache behaved as designed.

python3 make_fake_logs.py fake_logs Enter fullscreen mode Exit fullscreen mode wrote sess-a.jsonl (9 assistant lines) wrote sess-a/subagents/agent-sub1.jsonl (2 assistant lines) wrote sess-a/subagents/agent-sub2.jsonl (1 assistant lines) wrote sess-b.jsonl (3 assistant lines) wrote sess-c.jsonl (5 assistant lines) Enter fullscreen mode Exit fullscreen mode These files contain:

python3 cache_audit.py fake_logs Enter fullscreen mode Exit fullscreen mode 20 assistant lines -> 19 API calls (duplicates dropped) in 5 chains (session x agent) session agent msg written reason sess-a main a1 20,000 first turn sess-a main a4 23,000! pause > TTL (7.5 min, ttl 5m) (+26,450 units) sess-a main a7 20,500! prefix changed (1.0 min after last call) (+23,575 units) sess-a sub1 s1a 15,000 subagent start sess-a sub2 s2a 15,000 subagent start sess-b main b1 30,000 first turn sess-b main b3 30,600! pause > TTL (65.0 min, ttl 1h) (+58,140 units) sess-c main c1 150,000 first turn sess-c main c3 30,000 after compaction (auto, 1.2 min after last call) sess-c main c5 12,000 after compaction (manual, 71.0 min after last call) total cost: 669,292 units | avoidable rebuilds (!): 108,165 units = 16% Enter fullscreen mode Exit fullscreen mode How to read it:

Check it by hand once. Turn a4 wrote 23,000 tokens at 1.25 that could have been read at 0.1: 23,000 × 1.15 = 26,450.

To see what the old count looked like, add --no-compact. It ignores the boundary lines:

sess-c main c3 30,000! prefix changed (1.2 min after last call) (+57,000 units) sess-c main c5 12,000! pause > TTL (71.0 min, ttl 1h) (+22,800 units) total cost: 669,292 units | avoidable rebuilds (!): 187,965 units = 28% Enter fullscreen mode Exit fullscreen mode Same logs, and the avoidable share goes from 16% to 28%, only because two expected rewrites got blamed on the cache.

python3 cache_audit.py ~/.claude/projects/ < your-project-folder > --read-mult 0.05 # Opus 5.5 / Sonnet 5.5 Enter fullscreen mode Exit fullscreen mode It only reads numbers, ids and timestamps. It never prints message text.

I ran a wrapper around the same audit() over 7 days of my own transcripts on two personal accounts: 30,647 API calls, almost all Opus 5.5, read multiplier 0.05.

210 auto-compactions happened in that week. 194 of them were followed by a rebuild, and without the check 186 of those were counted as prefix changed and 8 as pause > TTL. What is left after the check is mostly pause > TTL (3.2%). Real prefix changed (a CLAUDE.md edit, an MCP server connecting) was 15 turns, 0.3%. With read at 0.1 the two numbers are 6.0% and 2.3%.

On my logs, then, about 60% of what the first version called avoidable (5.8 of the 9.4 points) was compaction. Most of what remains is coming back to a long session after more than an hour.

Run sh check.sh to run the unit tests ( test_cache_audit.py ), rebuild everything in a fresh temp folder and compare against expected_output.txt.

Written with AI help; numbers come from running the scripts in this folder.

Quelle: dev.to (dev.to). Original öffnen