Feature request: Compact tool for programmatic context compaction

Resolved 💬 2 comments Opened Mar 26, 2026 by gpalmgg Closed Apr 26, 2026

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:

  1. Trigger the same compaction logic that /compact uses
  2. Accept an optional instructions parameter (same as /compact <instructions>) to guide what context to preserve
  3. 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 /clear instead: too destructive, loses all context rather than summarizing it

🤖 Generated with Claude Code

View original on GitHub ↗

This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗