Bug: Autocompact is thrashing due to rapid context refill
Describe the bug
I am encountering an issue where the autocompact feature is thrashing. The system throws the following error/warning:
"Autocompact is thrashing: the context refilled to the limit within 3 turns of the previous compact, 3 times in a row. A file being read or a tool output is likely too large for the context window. Try reading in smaller chunks, or use /clear to start fresh."
This appears to happen when reading files or processing tool outputs that are relatively large, causing the context window to hit its limit almost immediately after a compact operation.
Steps to Reproduce
- [Explain what you were doing, e.g., "I asked the agent to read a specific large file using the file-read tool."]
- [Explain the subsequent interactions, e.g., "The agent attempted to process the output."]
- The "Autocompact is thrashing" error is triggered.
Expected Behavior
The agent or SDK should ideally handle large tool outputs more gracefully, perhaps by automatically chunking the content or preventing the context limit from being overwhelmed instantly after an autocompact.
skill number:64 subagent:6 command:2 dialog 1 count error:Autocompact is thrashing: the context refilled to the limit within 3 turns of the previous compact, 3 times in a row. A file being read or a tool output is likely too large for the context window. Try reading in smaller chunks, or use /clear to start fresh.
I have the same issue
session id d6317545-21ba-40b6-bd31-45ef835b6ece
It went in compacting cycle loop
+1
solution?
Not sure why... but i can't continue working like this
We hit this constantly in May and June running a Claude Code setup with a lot of skills and subagents (similar shape to the original report: dozens of skills, several subagents). It turned out not to be one oversized file. The subagents were starting most of the way to the limit before they did any work.
What we measured: every subagent inherited the parent's always-loaded context (project instructions, rules files, memory files) plus the schemas for every tool and MCP server the parent had, about 136 tools. That put each subagent's starting floor at roughly 75K to 100K tokens. We also had CLAUDE_AUTOCOMPACT_PCT_OVERRIDE lowered, which the subagents inherited. With that floor, one or two normal tool results (a web page, a long file, a big API response) fill the window, autocompact runs, and the floor is still there afterward, so it refills within a few turns. That is the "refilled to the limit within 3 turns, 3 times in a row" condition in the error.
What fixed it for us:
- Give each subagent an explicit
toolsallowlist in its definition instead of inheriting everything. Our research subagent went from ~136 tools to the 47 it uses, and a general worker to 4. - Stop loading personal or project memory into subagents. We moved it to Claude Code's native auto memory (
autoMemoryDirectory), which loads in the main session but not in subagents. - Remove
CLAUDE_AUTOCOMPACT_PCT_OVERRIDEso subagents use the default threshold. - Delete always-loaded rules files that only the main session needed.
Result: the subagent floor dropped from ~75–100K to ~30K tokens, and the same dispatches that thrashed every time (research agents doing several web fetches) ran 17 to 26 tool calls with no thrash, 5 out of 5, and then a full cycle of our scheduled jobs ran clean.
To check whether this is your case: run /context in the main session and look at how much is system prompt, tools, MCP tools, and memory before any conversation. Whatever is in those buckets is roughly what each subagent starts with.
On the SDK side, two things would have made this much faster to diagnose: the thrash error could report the post-compact token count (if it is still most of the window right after compacting, the problem is the floor, not one big read), and the docs for subagent definitions could note that omitting tools inherits every tool schema, with the token cost that implies.