[FEATURE] Allow Claude to self-initiate context compaction

Status Closed — not planned
Maintainer reply None cached
Activity 9 comments · opened Mar 11, 2026 · closed May 9, 2026

Problem

Currently, context compaction is triggered automatically by the system when context usage reaches a threshold. Claude has no ability to initiate compaction proactively. This creates a real workflow problem:

Claude cannot prepare for compaction. When working on complex multi-step tasks, Claude often needs to:

  1. Write critical state to handoff files before context is compressed
  2. Complete a logical checkpoint (e.g., finish a code review cycle, update documentation)
  3. Ensure all in-flight reasoning has been persisted to disk

Because compaction timing is unpredictable, Claude frequently loses important context mid-task — details like naming conventions, file numbering schemes, prior decisions, and subtle constraints that were discussed but not yet written to files.

Real-world impact

In a session today, after an automatic compaction, Claude:

  • Forgot a test log naming convention that was documented in a memory file (but not re-read after compaction)
  • Created files with wrong names 3 times before the user corrected it
  • Had to be reminded of context that was discussed extensively pre-compaction

The user had established a file-based persistence system (handoff files, session state, memory files) specifically to survive compaction. But because Claude can't control when compaction happens, there's always a gap between "last file write" and "compaction trigger" where in-context reasoning is lost.

Proposed solution

Add a tool or command that allows Claude to self-initiate compaction at a chosen moment. For example:

/compact   (user-initiated, already exists)

New: allow Claude to call a Compact tool (or similar mechanism) that:

  1. Triggers compaction immediately
  2. Allows Claude to prepare — write all critical state to files before compacting
  3. Returns control to Claude post-compaction, with a clean context that can re-read the files it just wrote

This is analogous to how a programmer manually commits before switching branches, rather than having an auto-save overwrite at random intervals.

Why this matters more than it seems

The 75% auto-compact threshold is a blunt instrument. It optimizes for "don't run out of context" but not for "don't lose important reasoning." Claude with self-compact ability would:

  • Persist before compress: Write handoff → compact → re-read handoff (clean recovery)
  • Choose natural breakpoints: Compact after completing a logical unit of work, not mid-reasoning
  • Reduce post-compact confusion: Fewer "I lost track of what we were doing" moments
  • Enable longer autonomous workflows: Complex multi-step tasks (code review cycles, multi-file refactors) could run more reliably

Alternatives considered

  • Pre-compact hooks (PostToolUse at 75%): Already implemented by users, but can only remind Claude to write files — can't delay or control compaction timing
  • More aggressive file persistence: Helps, but adds overhead to every step. Claude shouldn't need to write every intermediate thought to disk "just in case"
  • Larger context windows: Delays the problem but doesn't solve it. Complex sessions will always eventually need compaction

Additional context

Related issues: #19872 (param separation), #26488 (partial compaction), #24867 (compaction blocks work). This proposal is complementary — it's about agency over timing, not compaction mechanics.

View original on GitHub ↗

8 Comments

github-actions[bot] · 5 months ago

Found 3 possible duplicate issues:

  1. https://github.com/anthropics/claude-code/issues/28559
  2. https://github.com/anthropics/claude-code/issues/14512
  3. https://github.com/anthropics/claude-code/issues/12665

This issue will be automatically closed as a duplicate in 3 days.

  • If your issue is a duplicate, please close it and 👍 the existing issue instead
  • To prevent auto-closure, add a comment or 👎 this comment

🤖 Generated with Claude Code

maboroshi-appdev · 5 months ago

This is not a duplicate of the linked issues. Here's why:

#28559 — Closest match, but focuses on /compact as a tool with customizable instructions. Our proposal is about agency over compaction timing — the ability for Claude to choose when to compact based on workflow state, not just having a compact tool. The key differentiator is the persist → compact → re-read pattern: Claude writes critical state to files, triggers compaction at a natural breakpoint, then recovers cleanly. #28559 doesn't address this workflow.

#14512 (CLOSED) — Requests a compact tool as a cleanup step in skills. Much narrower scope — no discussion of timing control, state persistence, or the fundamental problem of losing in-flight reasoning.

#12665 (CLOSED) — Proposes starting a fresh context window with self-prompting — a fundamentally different mechanism (session restart vs. in-place compaction). Different solution to a different problem.

What this issue uniquely contributes:

  1. Real-world failure case with specific symptoms (naming convention loss, 3x wrong file creation post-compact)
  2. Analysis of why pre-compact hooks are insufficient — they can remind Claude to write files but can't delay or control compaction timing
  3. The "commit before branch switch" analogy — framing compaction as a deliberate checkpoint, not an emergency measure
  4. Complementary positioning with #19872, #26488, #24867 — this is about timing agency, not compaction mechanics

The core insight here is that having a compact tool is necessary but not sufficient. The value is in Claude being able to choose the moment — after completing a logical unit of work, after persisting state, at a natural breakpoint — rather than being interrupted mid-reasoning by an automatic threshold.

jacobcxdev · 5 months ago

+1, this would be incredibly useful for orchestration workflows.

physics91 · 5 months ago

The persist → compact → re-read workflow framing here really nails the core problem. I had a similar issue and ended up closing it as duplicate in favor of the existing discussions.

I've posted a concrete design proposal on #28559 that addresses both the timing agency problem you describe here and the multi-agent orchestration needs — including a deterministic trigger policy, user safety model, and phased rollout plan. Might be worth a look if you're interested.

ashutoshuiuc · 5 months ago

Automatic and intelligent compaction is definately v useful for automated workflows.

github-actions[bot] · 3 months ago

Closing for now — inactive for too long. Please open a new issue if this is still relevant.

zdaar · 2 months ago

I wish this was a thing for orchestration.

basnijholt · 24 days ago

I feel like this has become even more important now with Opus 5, who pretty often just stops working (even though a /goal has been set) because its context window is nearly full...

Showing cached comments. Read the full discussion on GitHub ↗