GrepBoard/Manuals
605 manuals ·

Fix Claude Code "Autocompact is thrashing" error

·Tested on Claude Code 2.1.302 (option existence only) · official docs · 2026-10·Test scope ↓·7 steps

"Autocompact is thrashing" means compaction worked but a large file or tool output refilled the context at once. Official recovery steps and a /context check.

output
Autocompact is thrashing: the context refilled to the limit...

Read big files in pieces and use /compact to drop large output. Automatic compaction succeeded, but a file or tool output refilled the context window right away, several times in a row. Per the official docs, Claude Code stops retrying so it does not waste API calls on a loop that makes no progress. The message appears in the docs with the trailing ellipsis. Compacting again will not help; remove whatever keeps refilling the window.

[1] If a large file was being read whole, read it in pieces. This is the docs' first step.

text
Read only lines 120-220 of src/big.ts

A line range or a single function keeps the window from filling again right after compaction.

[2] Tell the next compaction to drop the large output. The docs' example:

text
/compact keep only the plan and the diff

[3] Move the large-file work to a subagent. It runs in a separate context window, so the file contents do not pile up in the main conversation. If subagents do not show up, see Claude Code subagents not found.

[4] If the earlier conversation is no longer needed, run /clear.

[5] If the error returns after /clear, run /context and compare the Messages row with the rows above it. The docs split the two cases.

ResultMeaningAction
Messages is the largest rowA file or tool output in the new conversation is refilling the windowRepeat [1]-[3]
The other rows together are largerWhat loads at session start leaves too little room to work inTrim what loads at startup

/context prints tokens per category; compare Messages with the rows above it (system prompt, system tools, Memory files and so on). If those are large, shrink the CLAUDE.md files and tool definitions that load at start. The docs point to the "prompt is too long" entry of the errors page for how; that section could not be read here.

[6] If you set the window size yourself, doubt the value. In a small window, one file is enough to refill it. The option takes 100k to 1M tokens. Check the environment variable first. Per the official settings reference, CLAUDE_CODE_AUTO_COMPACT_WINDOW takes precedence over the --autocompact flag and the settings file, so if it is still set, the flag changes nothing.

console
$ echo $CLAUDE_CODE_AUTO_COMPACT_WINDOW

If a value prints, run unset CLAUDE_CODE_AUTO_COMPACT_WINDOW (and delete the line from your shell profile if it is set there), then use the flag.

output
  --autocompact <auto|tokens>           Auto-compact window size (auto, or                                        100k–1M tokens)
console
$ claude --autocompact auto

If you set it through autoCompactWindow in a settings file, remove that value or set it to "auto". The changelog also has a 2.1.304 line fixing CLAUDE_CODE_AUTO_COMPACT_WINDOW=200k silently setting a 100K window, so on an older build the window may have been smaller than intended.

The window can also shrink for another reason. The 2.1.286 changelog says the model fallback notice and this error now say when a fallback dropped the context window from 1M to 200K tokens. If the error mentions a fallback, you are working in a smaller window: go back to the original model or read less at a time (the changelog line is the only official source found; the settings docs do not cover it).

[7] Update. The 2.1.307 changelog line says it fixed some "Autocompact is thrashing" errors in turns that read many PDF pages, images or other large tool results. The build checked for this article was 2.1.302, which predates that fix, and the wording is "some", not all.

console
$ claude --version

How compaction works and what survives it is in Claude Code /compact vs /clear when the conversation gets long. Other messages are in Claude Code common errors.

#claude-code#context#autocompact