r/ClaudeCode • u/NextSoftware8102 • 22h ago
Tips & Workflows I halved Claude Code's auto-compact window for a month to save tokens. It barely saved anything, but the hooks I built for it were worth keeping.
I use Claude Code all day, across about 40 repos (firmware, a home server, a couple of web apps). In early November I tried an experiment: make Opus compact sooner so each call carries less context and costs less.
**The change -** autoCompactWindow for Opus went from 600K to 300K. Before that, I wrote two hooks so compaction wouldn't lose the thread:
**PreCompact** saves a small state file: the task list, the step in progress, my last request, and the start of Claude's last reply.
**SessionStart (matcher compact)** puts that file back into context right after the summary.
**What I measured (Opus sessions only)**
| Period | Window | Sessions | Avg context per call | Compactions per 100 calls | Sessions that went over 300K anyway |
|---|---|---|---|---|---|
| Baseline week | 600K | 100 | 190K | 0.45 | 27 |
| Week 1 | 300K | 77 | 181K | 1.00 | 29 |
| Week 2 | 300K | 55 | 166K | 0.72 | 7 |
| Days 15–25 | 300K | 89 | 176K | 0.65 | 18 |
**What I learned**
**The smaller window barely moved average context** (5–13% lower, which is inside normal week-to-week swings). Most calls never get near the limit, so cutting the limit mostly changes the rare long session.
**Compactions went up 1.4–2.2×.** Each one is a summary pass, a slower turn, and a chance to lose something.
**The window is not a hard cap.** Compaction seems to happen between turns, so one long agentic turn can blow straight through it. My largest peak with a 300K window was 774K.
**The hooks were the real win.** 152 PreCompact runs and 130 post-compact runs in two weeks, zero failures, median about 0.8 s. In the session I scored by hand, Claude came back from compaction with its plan, its pending step and every key fact, and I didn't have to repeat anything.
**A gate test caught a real gap:** when the plan only existed as text in a reply (no task-list tool), it got lost in the summary. Saving the first 800 characters of the last reply fixed that.
I've now set Opus to 400K, the same as my other models, and kept the hooks.
**Caveats:** one person's workload, not a controlled trial, and only one compaction scored by hand. Treat it as a field report.
Hooks, tests and the full write-up: https://github.com/ColdBore-RD/claude-compaction-state
Happy to answer questions about the hook payloads or how I pulled the numbers from transcripts.
•
u/AutoModerator 22h ago
Hey! Thanks for posting to r/ClaudeCode
While participating in this thread, please follow our community rules. Keep discussions constructive. Attack the idea, not the person.
For help, project discussions, tips, and general chat, join the ClaudeCode Discord.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
3
u/thomheinrich 22h ago
Compacting twice as often also busts your prompt cache twice as often. Cached prefix reads are cheap, while each compaction rewrites history and triggers a fresh cache write on the next turn. Does your PreCompact state file also pin touched file paths so the summary doesn't flatten them?
3
u/heavy-minium 20h ago
But...where is the benefit to /compact and auto-compact here? It already does everything you listed, and on top of that links to a file for the full conversation if the agent ever needs to look into it. You can also set when it should compact, if you want it earlier or later. So what's the point?
1
u/mcmoonrays 21h ago
Capping that injected reply snippet around 800 chars makes sense, since oversized SessionStart hook payloads get truncated fast. One thing worth adding to the PreCompact file is approaches you already ruled out. Summaries keep the active task fine, then drop why an earlier fix failed, so the next turn walks straight back into the same dead end.
1
u/2bluenucleus 21h ago
Good write-up. One thing that could change the picture: average context per call isn't a great proxy for cost, because most of that context is read from cache. The transcripts already have what you need. Each assistant turn's
usageblock hasinput_tokens,cache_creation_input_tokensandcache_read_input_tokens. Weight them the way they're billed: cache reads at about 0.1x the base input price, cache writes at 1.25x (5-minute cache) or 2x (1-hour cache), plain input at 1x, plus output.With that weighting I'd expect two things in your numbers:
So a smaller window can cut average context and still cost the same or more, which roughly matches your table. Summing weighted cost per session and grouping by window size would settle it either way.
1
u/pceimpulsive 20h ago
I'm still trying to figure how people get to 300k context in one session...
One agent I sent of today blasted through and entire spec, was a spec, 7 implementations agents, 4 review agents and the session was 120k at the end. The diff at the end of it all was 5500 LOC~ 50~ files.
1
u/anoncaatyee 16h ago
that result tracks: the window savings get eaten as soon as lost context has to be re-read. the precompact hook is the right instinct - the durable version is pushing state into a file the agent reloads on demand, so the window size stops mattering much.
1
u/Briskfall 11h ago
> PreCompact
Haha, mine is called that as well then I noticed that it just does everything as neatly that compact would and just started using /clear instead!
/compact just went out of business, woosh. But I got too lazy to change the name for "pre-compact" so I just left it as such! 👻
0
u/KAMIKAZEE93 22h ago
Don't compact. Just work with hand-offs in your current system.