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%
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.
Needs Python 3.9+ and nothing else.
The usage block
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
}
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):
| what | multiplier |
|---|---|
| plain input | 1 |
| cache write, 5-minute TTL | 1.25 |
| cache write, 1-hour TTL | 2 |
| cache read | 0.1 (0.05 on Opus 5.5 and Sonnet 5.5) |
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}}
A rebuild right after one of these lines is the normal cost of compacting, and the cache behaved as designed.
Step 1: make the fake logs
python3 make_fake_logs.py fake_logs
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)
These files contain:
- session A on a 5-minute cache: a 7.5-minute pause, two subagents, and one turn where the prefix changed with no pause
- session B on a 1-hour cache: one 20-minute pause, which is fine, and one 65-minute pause, which is not
- session C on a 1-hour cache: a long conversation that auto-compacts, then a 70-minute pause that ends with a manual
/compact - one response written twice. Real transcripts do this: one response becomes several lines, one per content block, and each line repeats the same
usage.
Step 2: run the audit
python3 cache_audit.py fake_logs
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%
How to read it:
-
!marks a rebuild you could have avoided. First turns, subagent starts and turns right after a compaction always write a new prefix, so they get no!. -
+N unitsis a rough extra cost: the tokens that were written when they could have been read, times (write multiplier − read multiplier). - In this fake data, three avoidable rebuilds are 16% of the total.
-
c5came 71 minutes after the last call, so the 1-hour cache had expired anyway. The script still files it underafter compaction: the prefix was new, so it would have been written even with no pause.
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%
Same logs, and the avoidable share goes from 16% to 28%, only because two expected rewrites got blamed on the cache.
Step 3: what each reason means and what to do
| reason | what happened | what helps |
|---|---|---|
pause > TTL |
you stepped away longer than the cache lifetime | the next turn rewrites the whole context, so its cost grows with context size. Coming back to a short context (after /compact, or a new session started from a notes file) makes that rewrite smaller |
prefix changed |
something early in the prompt changed: CLAUDE.md, a tool list (an MCP server connecting mid-session), the model | don't edit CLAUDE.md or toggle MCP servers in the middle of a long session |
subagent start |
each subagent writes its own prompt once | fine for real work; costly when you fan out many tiny subagents |
after compaction |
/compact or auto-compact replaced the history with a summary |
nothing to fix on that turn. If it happens a lot, the lever is the session itself: shorter sessions compact less often |
Step 4: run it on your own logs
python3 cache_audit.py ~/.claude/projects/<your-project-folder> --read-mult 0.05 # Opus 5.5 / Sonnet 5.5
It only reads numbers, ids and timestamps. It never prints message text.
What it found on mine
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.
| avoidable share of total cost | |
|---|---|
without the compaction check (--no-compact) |
9.4% |
| with it | 3.5% |
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.
Exercises
- In
make_fake_logs.py, change the time of turna4fromm(9, 30)tom(6). Isa4still flagged, and with what reason? answerStill flagged, but now asprefix changed (4.0 min after last call). The gap is under 5 minutes, yet the log says the cache was rewritten, so the script blames the prefix. Timing can only rule causes out. It can't prove one. - Turn off the duplicate check: in
cache_audit.pychangeif key in seen:toif False:and run again. How much does the total go up? answer669,292 → 673,170 units. That is exactly one extra copy ofa2: 3 + 1,500 × 1.25 + 20,000 × 0.1 = 3,878. On a real session we tried, 152 assistant lines were only 77 API calls, so summing every line nearly doubles the count. - Switch session B to the 5-minute cache (
ttl="1h"→ttl="5m"). Turnb2comes 20 minutes afterb1. Why isn't it flagged? answerThe script judges from what the log says was written, not from the TTL. The fake numbers forb2still say "read 30,000", which is a hit. In a real 5-minute session, a 20-minute pause would show up as a big write. Onlyb3's extra cost changes, from 58,140 to 35,190, because a 5-minute write costs 1.25 instead of 2. - Run with
--min 1000. Do the small writes (a21,500,s1b2,000) start showing up? answerNo. A turn also needs to write more than it reads, and both of those read far more (20,000 and 15,000). Small writes on top of a big read are just the new part of the conversation being cached. - In session C, move the manual compaction from
m(75)tom(77), afterc5. What doesc5turn into? answerpause > TTL (71.0 min, ttl 1h)with!and +22,800 units. The boundary now comes after the call, so it can't explain the rewrite, and a 71-minute gap is over the 1-hour TTL.
Known limits
- The reason is a best guess from timing and boundary lines.
prefix changedcannot tell you what changed. -
after compactionstill costs money; the script just doesn't count it as avoidable on that turn. On my logs it was about half of all rebuild cost. Shorter sessions compact less often, which is the only way I know to shrink it. - Output tokens are left out on purpose. They don't depend on caching.
- This counts API-price units, not your subscription percentage. Anthropic does not publish how plan limits map to tokens.
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.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.