[Feature Request] Model-initiated context compaction during task execution
Bug Description
Can Claude Code compact context on its own initiative?
Short answer: not on its own initiative. Compaction in Claude Code happens in two ways.
- Automatic compaction. The harness compacts when the conversation nears the context limit. That is a size trigger, not a decision the model makes.
- Manual compaction. The user runs
/compact, with optional focus text such as/compact keep the asset status table. That is a user trigger.
The model itself has no tool to request a compaction mid-task. It cannot choose to compact because the conversation feels stale or a phase of work ended. The system prompt in this session confirms the model's role: when compaction occurs, a summary plus any remaining context comes back in the next window, and the model continues. The model reacts to compaction. It does not start it.
Two related things exist, but they are not self-initiated compaction:
- Context editing on the API. The Messages API can clear old tool results under a threshold. That is a configuration set by the caller, again a size rule, not a model choice.
- Subagents and forks. Spawning an agent keeps that agent's tool output out of the parent context. This is a way to avoid growth, not a way to shrink existing context.
If you want compaction at a specific point in a workflow, the practical options are to run /compact yourself at that point, or to use a hook in settings that reacts to an event and prompts you. A hook cannot call compaction directly either, so the answer for a fully automated "compact now" from the harness is no, apart from the size trigger.
I cannot verify whether a newer build added such a capability from a side question. If it matters, ask in the main conversation, where the claude-code-guide agent can check current docs.
Environment Info
- Platform: linux
- Terminal: xterm-256color
- Version: 2.1.284
- Feedback ID: 57aea8d5-5706-49b8-a7cf-d2266ed4b7d8
Errors
[]