BaCheWork and records of a photonics research workhorse
Language

Setting the autocompact threshold is only step one: what compaction loses, and how to save before it runs

Lowering Claude Code's autocompact threshold does save usage, but the threshold only decides when compaction happens. This note covers what compaction drops, and the three things I added for long tasks: save a handoff first, specify what the summary must keep, and feed the handoff back after compaction.

Type
Essay
Published
2026-11-12
Updated
2026-11-12

Lately I keep seeing the claim that setting /autocompact 400k in Claude Code, a single command, saves a lot of usage. That is true. I have also set the threshold to 400k and used it for over two months (I used a settings file and an environment variable; the effect is the same).

But the threshold only decides when compaction happens. After compaction, the conversation is replaced by a summary, and some things go missing with it. What a long task really has to handle is “what got lost, and how to get it back.” This note lays out the methods I have actually used.

The behavior below follows the official documentation I read on 2026-11-12; where I measured things myself, I note the version at the time. Claude Code updates quickly, so read the latest documentation once before you start.

Conclusions first

  • Lowering the threshold pays off with my usage. Compaction itself costs one summarization, but each later turn reads less cached context, so it pays for itself after running a while longer.
  • The costs are pauses and lost detail. Each compaction stops for one and a half to three minutes; the summary most easily loses specific numbers.
  • There are three ways to patch this: have the model write a short handoff before compacting; state in CLAUDE.md what the summary must keep; use a hook to feed the handoff back after compaction.
  • The best timing is still your choice. The official advice is to run /compact manually at natural breaks in the work; my own approach is to leave autocompact on as a safety net.

1. How to set the threshold

If you do not set anything, a model with the native 1M window auto-compacts at about 967K (official: default thresholds). To compact earlier, there are several ways (official: set the auto-compact window):

Method Scope
/autocompact 400k The current model in user settings, stored in modelSettings (per model since v2.1.288)
autoCompactWindow in the settings file All models
--autocompact at startup This launch only
Environment variable CLAUDE_CODE_AUTO_COMPACT_WINDOW Highest priority; overrides the three above (except when the value is wrong and Claude Code ignores it)

The value can be set from 100K to 1M, and it cannot exceed the model’s own window.

Note the last row: if you set a value in an environment variable, and later use /autocompact to give a particular model a different threshold, the official documentation says it will only report that it was overridden and will not save it. If you want different thresholds for different models, do not use the environment variable; use the settings file with /autocompact instead.

2. Why it pays off

Claude Code resends the entire conversation every turn. The unchanged part is read from cache and billed at the lower cache rate. The longer the conversation, the more cache each turn has to read.

Compaction invalidates the conversation-level cache: the next request is a new, much shorter history that does not match the old one (official: compacting the conversation). The request that generates the summary can read the front part from a warm cache, so mid-session compaction costs much less than the conversation length suggests; after compaction, only the short summary needs its cache rebuilt.

I ran the numbers once, using public pricing, on the transcripts of a few of my long sessions. The cost of compaction has to be earned back slowly by the cache reads that compaction saves afterward. The cheaper a model’s cache reads, the more turns it takes to break even. In those sessions I kept running many turns after compaction, and in the compactions I measured none lost money, but the worst one only just broke even. This is a small sample, a calculation at my usage on public pricing (not the plan’s quota table itself). If you usually finish right after compacting, the outcome could be the opposite.

3. The costs: pauses and lost detail

Pauses. Producing the summary takes time. I measured one and a half to three minutes each time, and at that time (September 2026) the desktop app showed no progress indicator, so it looked like a hang.

The summary loses detail. The official documentation also says the summary keeps your requests and key code snippets, but detailed instructions from early in the conversation may disappear (official: when context fills up). When I compared before and after compaction myself, the most common loss was statistics: the model remembered that a comparison had been made, but forgot the numbers it produced, and had to read back again.

After compaction you do not start from zero. The summary itself is not long, but after compaction the system puts back the project’s CLAUDE.md, auto memory, the most recently read files (up to five), and the contents of skills in use (official: what survives compaction). The actual starting size I measured after compaction was about 100k, several times the figure labeled “post-compaction size” in the compaction log. The more you have configured, the higher the starting point; if the threshold is set too low, you will be compacting again soon after the last compaction.

4. Reinforcement 1: write a handoff before compacting

My approach is to set another line in front of the threshold: once the context passes 300k, remind the model to write a handoff snapshot of 2k or less (what step it is on, decisions made and their reasons, files changed, open problems) and save it as a file. That way, by the time autocompact triggers (I set 400k, and in practice it triggers a little below the set value), what matters is already in the file, not left to the summary.

I use a hook for this reminder. I hit a pitfall: at first I only checked the context length when the user sent a message. When the model itself ran dozens of tool calls in a row and crossed 300k in the middle, the reminder never arrived. I then changed it to check again after every tool call, which closed the gap. Most of the time in a long task, the model is the one running, so the checkpoints have to sit on the path it runs.

5. Reinforcement 2: write Compact Instructions in CLAUDE.md

The official documentation notes that you can add a section titled “Compact Instructions” to CLAUDE.md, or add a line of key points after a manual /compact, to control what is kept when compacting (official: when context fills up).

What I list there: the project’s current stage and next step, decisions made and their reasons, files changed, user requests still in force, which sources have been checked (write the names, not “already checked”), open problems, and “when a verification is claimed, keep it together with how it was verified.”

Does autocompact follow this section? The official documentation says autocompact works the same way as a manual /compact, but does not specifically say Compact Instructions apply during autocompact. I ran several probes in both the terminal and the desktop app, asking that a specific string be kept in CLAUDE.md, and the autocompact summaries kept it almost every time (2.1.317 to 2.1.319).

The other route is the PreCompact hook: the official documentation states that its standard output becomes instructions for the compaction summary (official: PreCompact output). I use it to attach one sentence before each compaction: where the handoff snapshot is and whether it is the latest.

6. Reinforcement 3: feed the handoff back after compaction

A SessionStart hook can fire only after compaction (set the matcher to compact), and its output is added to the context after compaction (official: what survives compaction). I use it to put back the path and contents of the handoff snapshot I just described, so the first thing the model does after compaction is read its own handoff.

Every few autocompactions, I also check whether the handoff dropped anything, for example whether the snapshot still lists the “already checked” sources. The handoff mechanism can fail too, so something has to look back at it.

7. A pitfall: using hooks to block compaction

The official documentation says a PreCompact hook can block this compaction: exiting with exit code 2 blocks it (official: hooks). My original idea was: if the snapshot has not been written, block once and let the model finish writing before compacting.

In the first test (2.1.257), I returned permissionDecision: "deny" in JSON to block autocompact, and autocompact ran anyway. Later (2.1.310) I retested with exit code 2, and both manual and automatic compaction were blocked. The current official documentation gives the JSON blocking form for PreCompact as decision: "block", which differs from the shape I used at the time. I did not retest the JSON route afterward, so I cannot say the platform ignores JSON; it is more likely that my earlier form was wrong.

So if you want to block compaction, use exit code 2 first, and confirm with one real autocompact that it is blocked; do not test only manual /compact. Also, blocking only postpones things: the context keeps growing. In the end I did not use blocking; I relied on the 300k reminder described above.

8. The best timing is still your choice

The official prompt caching documentation advises running /compact manually at natural breaks in the work (for example between two tasks), rather than letting autocompact trigger in the middle of a task (official: compacting the conversation). If what you want to discard is an entire wrong path, using /rewind to rewind is better: it returns to the front part that is already cached, so nothing needs rebuilding.

My current order is: if I can choose the timing, compact manually; if there is no time, the 300k reminder lets the model save its handoff first; if I really hit the threshold, autocompact follows the Compact Instructions, and afterward the hook feeds the handoff back in.

Closing

/autocompact is a good first step. But compaction is a lossy conversion, and rather than hoping the summary happens to keep what you need, it is better to write the important things to a file before compacting.

If you want to do the same, start with the cheapest step: add a Compact Instructions section to CLAUDE.md listing what you most often need to look back up after each compaction.