r/ClaudeCode • u/NextSoftware8102 • 59m 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 59m 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.
2
u/thomheinrich 56m 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?
1
u/KAMIKAZEE93 45m ago
Don't compact. Just work with hand-offs in your current system.
1
u/mcmoonrays 16m 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.