claude-code - 💡(How to fix) Fix [Feature Request] Allow programmatic/agent-initiated compaction [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#52002Fetched 2026-04-23 07:39:11
View on GitHub
Comments
0
Participants
1
Timeline
3
Reactions
0
Participants
Timeline (top)
labeled ×3

Fix Action

Fix / Workaround

Current Workaround

RAW_BUFFERClick to expand / collapse

Feature Description

Currently /compact is only available as a user-initiated slash command. The agent cannot trigger compaction itself, even when it detects that context is approaching the limit.

For long-running autonomous sessions (loops, background agents, overnight runs), there is no way for the agent to proactively manage its own context window. It works until it hits the ceiling, then stops and waits for a human to intervene.

Proposed Solution

Expose compaction as a tool the agent can invoke, or allow it to be triggered via a hook or MCP tool. This would let autonomous agents manage their own context lifecycle without human intervention.

Use Case

  • Autonomous agents running /loop or background tasks that may exceed context limits
  • Multi-agent workflows where a coordinator can't constantly monitor each agent's context usage
  • Any session where the human is away and the agent hits the context ceiling

Current Workaround

None for autonomous sessions. The agent cannot call /compact, cannot programmatically trigger it via hooks, and cannot continue once the context limit is reached. The only option is a human running /compact or /clear manually.

Additional Context

The PreCompact and PostCompact hooks are excellent — they let external systems preserve state around compaction events. The missing piece is letting the agent initiate the compaction itself when it determines the time is right.

extent analysis

TL;DR

Exposing compaction as a tool for the agent to invoke or triggering it via a hook could allow autonomous agents to manage their context lifecycle without human intervention.

Guidance

  • Consider adding an API or interface for the agent to programmatically trigger compaction, allowing it to manage its own context window.
  • Evaluate the feasibility of using existing hooks (e.g., PreCompact and PostCompact) as a foundation for implementing autonomous compaction triggering.
  • Assess the potential impact of autonomous compaction on external systems that rely on the current manual compaction process.
  • Investigate the requirements for determining when an agent should trigger compaction, such as context usage thresholds or session duration.

Example

No code example is provided due to the lack of specific technical details in the issue.

Notes

The proposed solution requires careful consideration of the agent's autonomy and potential interactions with external systems. The implementation should ensure that autonomous compaction does not introduce unintended consequences, such as excessive compaction or data loss.

Recommendation

Apply a workaround by implementing an API or interface for the agent to trigger compaction programmatically, as this would allow for more flexible and autonomous context management.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING