[FEATURE] Let the agent trigger /compact itself (agent-invokable compaction)
Preflight Checklist
- [x] I have searched existing requests and this feature hasn't been requested yet
- [x] This is a single feature request (not multiple features)
Searched first. The closest existing issues are about adjacent things — #46086 (fine-grained compaction controls), #65114 (manual user-initiated/compactin Cowork), #70364 (/compactshouldn't block user flow), #71625 (blocking PreCompact hook). None of them is what this asks for: the agent itself initiating compaction. See Additional Context.
Problem Statement
When I work in a long-running interactive session, I frequently want to compact context at a moment of my choosing — a clean checkpoint before the agent starts a big new file, or when I can see context filling up. Today I can't get the agent to do this for me.
/compact is a harness-intercepted command, and it can only be triggered three ways:
- I manually type
/compactmyself. - The harness auto-compacts when context fills (I don't control when).
- A driver injects it via the Agent SDK (
query({ prompt: "/compact", options: { continue: true } })).
The asymmetry that bites me: the agent can already invoke my custom skills/commands autonomously — I can nudge it in natural language ("run my X command") and it does it. But it has no way to invoke /compact. So I can nudge the agent to run everything except compaction.
And the programmatic escape hatch (#3) only exists for SDK/driver-spawned sessions. In the two interactive surfaces I actually sit in front of, there is no way to inject /compact at a chosen point:
- VS Code extension —
/compactonly works typed in the UI prompt box; thevscode://URI handler only pre-fills text (no auto-submit), and there's no external injection path. - TUI (
claude) —/compactonly works typed at the interactive prompt; nothing lets an outside process inject input into a running session.
Net effect: in VS Code and the TUI I'm stuck manually typing /compact, and a long-running agent can never decide "this is a good point to compact" on its own.
Proposed Solution
Make compaction agent-invokable — the same way custom skills support autonomous invocation by Claude — so I can nudge the agent in natural language ("compact before the next file", "you're getting full, compact now") and it runs /compact itself. The model triggers it; the harness still does the actual summarization.
Ideal UX:
- I type a plain-language nudge mid-task → the agent invokes compaction itself → I get the normal
compact_boundaryresult and we keep going. No UI typing, no SDK driver required. - The agent could also optionally self-trigger at a sensible boundary in long autonomous runs.
Preferred shape: an agent-invokable compaction primitive — expose compaction as a built-in tool, or treat /compact as a skill the model can call autonomously. Cleanest fit with the existing "skills are autonomously invokable" model, and it fixes VS Code + TUI for free.
Alternative Solutions
Alternatives that would also solve it (any one):
- A controllable trigger — a setting/hook/command that lets the agent cause compaction rather than only react to it. (The existing
PreCompacthook is reactive — it fires when compaction happens; it can't initiate it.) - Interactive-surface parity — a documented way to inject
/compactinto a running VS Code-extension or TUI session from outside the process (e.g.claude compact --session <id>, or an extension command/IPC), matching what the SDK already allows for drivers.
Workarounds I currently use / have considered:
- In SDK/driver-spawned sessions I inject
/compactviaquery({ prompt: "/compact", options: { continue: true } }). This works, but it only helps headless/driver sessions — not the interactive VS Code or TUI sessions where I actually feel the context pressure. - Manually typing
/compactat the right moment — but that defeats the point of a long autonomous run, and I can't always predict the right moment before context is already polluted.
Priority
Medium - Would be very helpful
Feature Category
Interactive mode (TUI)
Use Case Example
Concrete scenario:
- I'm deep into a long refactor across several files in the VS Code extension (or the TUI). Context is ~70% full.
- The agent is about to open a large file and start a big, self-contained new unit of work — a natural checkpoint where the earlier exploration is no longer needed verbatim.
- I type a plain-language nudge: "compact first, then start on the parser."
- With this feature: the agent invokes compaction itself, summarizing the now-stale earlier history, then proceeds into the parser work with a clean, focused context.
- Today: the agent can't do that. I have to stop, manually type
/compact, wait, then re-issue the instruction — and in a long autonomous run I'm not even watching at the right moment, so it auto-compacts later at a worse boundary and loses the framing I cared about.
This would save time and produce better compactions, because the agent (which knows where the logical task boundaries are) gets to choose the compaction point instead of the human guessing or the harness firing blindly on a token threshold.
Additional Context
Verified current state (as of 2026-06, against official docs — SDK slash commands, How Claude Code Works, VS Code):
| Surface | Can /compact be injected programmatically / agent-triggered today? |
|---|---|
| Driver (Agent SDK query) | ✅ Yes — documented (prompt: "/compact", continue: true) |
| VS Code extension | ❌ No external injection; URI handler only pre-fills, doesn't submit |
| TUI (claude) | ❌ No documented input injection into a running session |
| Agent (model) self-invoke | ❌ Not possible — built-in /compact isn't autonomously invokable (unlike custom skills) |
Related but distinct existing issues (this one is specifically about the agent initiating compaction):
- #46086 — Fine-grained compaction controls (partial / boundary-aware) — about how compaction behaves, not who can trigger it.
- #65114 — Manual user-initiated
/compactin Cowork — user-triggered, not agent-triggered. - #70364 —
/compactshould not block user flow — UX of the existing command. - #71625 — PreCompact hook: add blocking mode — reacts to compaction, doesn't initiate it.
This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗