Feature request: Compact tool for programmatic context compaction
Summary
Claude Code should expose a Compact tool (alongside Read, Edit, Bash, etc.) that allows the assistant to programmatically trigger context compaction during a session.
Problem
Currently /compact is a REPL-only command that only the user can invoke. The assistant cannot trigger compaction even when it recognizes the context is getting bloated. This creates a workflow friction where the assistant must ask the user to type /compact — breaking flow, especially in long autonomous runs (forges, swarms, multi-step builds).
Hooks can react to compact events (on_compact) but cannot cause them, and there's no user-facing setting to adjust the auto-compact threshold.
Proposed solution
Add a Compact tool to the assistant's tool list, similar to how Read, Edit, Bash, etc. are available. The tool would:
- Trigger the same compaction logic that
/compactuses - Accept an optional
instructionsparameter (same as/compact <instructions>) to guide what context to preserve - Return confirmation of successful compaction
Use cases
- Long autonomous sessions: Forges and swarms that run many steps would benefit from the assistant managing its own context window
- Proactive context management: Instead of asking the user to type
/compact, the assistant can act on its own recognition that context is bloated - Plan recovery: After heavy exploration phases, the assistant could compact before switching to implementation, preserving only the plan
Alternatives considered
- Lowering the auto-compact threshold: not currently configurable, and a static threshold doesn't capture when compaction is strategically useful (e.g., after exploration, before a phase switch)
- Using
/clearinstead: too destructive, loses all context rather than summarizing it
🤖 Generated with Claude Code
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗