ai newsFr., 16. Okt.
EilmeldungHow do I stop Claude Code from auto-compacting (and losing context)?

How do I stop Claude Code from auto-compacting (and losing context)?

Turn off auto-compact in /config (the Auto-compact toggle), or set "autoCompactEnabled": false in your settings file, or launch with DISABLE_AUTO_COMPACT=1 . That stops the automatic summary, but it doesn't give you a bigger context window, so you still need a plan for the moment the window fills.

dev.toFreitag 21:15 · 16.10.5 Min. Lesezeit
Bild: dev.to

Turn off auto-compact in /config (the Auto-compact toggle), or set "autoCompactEnabled": false in your settings file, or launch with DISABLE_AUTO_COMPACT=1. That stops the automatic summary, but it doesn't give you a bigger context window, so you still need a plan for the moment the window fills.

I keep seeing this question on r/ClaudeCode and in Claude Code's GitHub issues, usually right after someone loses an afternoon of decisions to a summary. So here's what Claude Code actually does, what the switches change, and the ways people deal with it, mine included.

Claude Code compacts on its own as you approach the context limit. Anthropic's context window docs put it plainly: "Claude Code compacts automatically as you approach the limit, so a full context window doesn't end your session." Compaction replaces the conversation history with a model-written summary and you keep going on top of that summary.

Where it triggers depends on the model. The model configuration page says models with a native 1M window compact at about 967K tokens by default, and some setups, like Sonnet 4.6 and Opus 4.6 without extended context, compact at the 200K boundary. You can check yours with /context, which shows what's filling the window.

The part that matters most is what survives. The docs have a table for it. The system prompt still applies. Project-root CLAUDE.md, unscoped rules and auto memory get re-injected from disk. Claude Code re-reads up to five of the files Claude touched, most recently modified first. Invoked skills come back, capped at 5,000 tokens each. Path-scoped rules, nested CLAUDE.md files and anything a hook added earlier get summarized along with everything else. And anything you only ever said in chat is at the mercy of the summary. The memory docs say the same thing from the other side: if an instruction vanished after compaction, it probably lived only in the conversation.

Anthropic documents several. autoCompactEnabled (default true ) turns automatic compaction off, and /config writes that key for you. DISABLE_AUTO_COMPACT=1 does the same for a session. In both cases manual /compact still works. DISABLE_COMPACT=1 goes further and disables manual /compact too. If you only want compaction to happen earlier, /autocompact 500k or the autoCompactWindow setting moves the trigger. CLAUDE_AUTOCOMPACT_PCT_OVERRIDE can only lower the threshold, never raise it.

There's also a hook-level option. The hooks reference says a PreCompact hook can block compaction by exiting with code 2. If the compaction was proactive, the session continues uncompacted. If it was recovering from a context-limit error the API already returned, the error surfaces instead.

One caution. In a long GitHub issue (anthropics/claude-code #42817), a user reports compaction firing even with the setting off. I couldn't reproduce that or confirm it either way, so if this matters to you, test it on your version.

The simplest fix is to leave compaction on and steer it. Put rules that have to survive in the project-root CLAUDE.md, add a "Compact Instructions" section there, and run /compact focus on the auth refactor yourself before the automatic pass picks for you. That's free and it's built in.

The most common workaround I see is a manual handoff. People turn auto-compact off, watch the status line, and somewhere around 70 to 75 percent ask Claude to write a handoff document, then /clear and tell the new session to read it. It works well. Its cost is your attention, and it fails exactly when you're deep in the work and forget.

Then there are tools that automate parts of this. claude-auto-handoff (alexknowshtml on GitHub) does the handoff for you: before the window fills it has Haiku write a structured brief, runs /clear, and seeds the fresh session with one line telling it to read the brief. claude-mem takes a different angle: it captures what your agent does, compresses it, and injects relevant context into later sessions. context-mode attacks the input side by sandboxing tool output so less of it lands in your window, and it persists session memory too. Using subagents for big reads helps for the same reason. These overlap, and you can run more than one.

Disclosure first: I built aigent-OS, so weigh this paragraph accordingly. It's an open-source Claude Code harness that refreshes its own context instead of compacting. At a threshold, 80% by default, it asks Claude to write a checkpoint I call a capsule. It waits for one exact acknowledgement, sends a single /clear, confirms through the SessionStart hook that a genuinely new session started, then resumes from the capsule. No acknowledgement, no clear. Memory is plain markdown in git, so you can read what it remembers with git log. It's MIT licensed, runs locally and needs Node 18+.

Here's what I can and can't tell you about it. Managed Auto-Refresh is in a release candidate, v1.1.0-rc.1. The public evidence is 11 of 11 clean refresh cycles on one machine, run at a deliberately low threshold so cycles came fast. On rc.1, a long session can still stall at the clear step and need a manual /clear. The fixes for that are merged on master but not in a tagged release yet, and two narrower edge cases around the acknowledgement are still open (issues #90 and #91). I'm not claiming it beats compaction. It's a different trade-off: an inspectable checkpoint and a clean window instead of a summary. Each cycle also costs some tokens to write and read the capsule. The repo is at https://github.com/wrg32786/aigent-os if you want to read the install script before running anything.

Does turning off auto-compact give me more room? It doesn't change the model's context window. Some users say turning it off frees space that was held back for compaction. I couldn't confirm that in Anthropic's docs.

Does /compact still work with auto-compact off? Yes. The settings reference says manual /compact keeps working. Only DISABLE_COMPACT turns it off.

What happens if I hit the limit with auto-compact off? From what I can tell you get a context-limit error instead of a compaction, though the docs only spell that out in one place. At that point you /compact or /clear by hand.

Is a handoff file better than compaction? Neither is better everywhere. A summary takes no effort. A handoff you control takes effort or tooling, but you decide what survives.

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