Replaying 147 Claude Code & Codex Sessions to Tune autoCompactWindow (and Catching a 3x Token Overcount)
My first script for checking local Claude Code token usage summed raw assistant lines in ~/.claude/projects/**/*.jsonl and overcounted session 4baee81b by 3.0x (33 JSONL usage rows for 11 actual API turns). Whenever a single turn emits text plus multiple tool_use blocks, Claude Code writes separate streaming records sharing the same message.id , and every record repeats the cumulative prompt…
This report analyzes the local Claude Code and Codex sessions on a machine, totaling 147 sessions with 3,197 raw records. After deduplication, 2,284 real API turns, 255.79M input tokens, and a 43.9% cache hit rate were identified. Notably, 13.4% of turns accounted for 47.8% of weighted token cost, specifically those running above 200K context.
The report reveals that early tool outputs carry significant "Context Rent," with a single 12K-token file read or git diff output on turn 3 of a 45-turn session occupying the prompt window for 42 subsequent turns.
Compaction often occurs immediately after, causing the agent to re-read the exact same files on the next 3 turns due to the summary dropping active file paths. To mitigate this issue, the author developed compactsim, a zero-dependency Node.js CLI tool to audit ~/.claude/projects and ~/.codex/sessions offline and run counterfactual compaction simulations against actual turn history.
Running compactsim on the 147-session corpus revealed that adjusting the autoCompactWindow parameter can significantly reduce token usage and compaction frequency.
With the default autoCompactWindow, the simulator found that baseline token usage was 160.79M weighted token-equivalents (tok-eq), with 0% savings and 9 compactions. However, by setting autoCompactWindow to 250,000 tokens, the system achieved 114.93M tok-eq, a 28.5% reduction in token usage and only 10 compactions. Further adjustments up to 150,000 tokens resulted in even greater savings, with 89.49M tok-eq, a 44.3% reduction, and 17 compactions.
The report also highlights the impact of mid-session cache-bust spikes and the importance of considering both uncached re-prefill penalties and empirical post-compaction file re-read taxes when implementing compaction strategies.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.
This story
This is one outlet's version. Read the fullest account.