[FEATURE] Programmatic / fleet compaction: compact-on-idle, cross-session /compact, runtime-adjustable autocompact
Use case
I run several coordinated Claude Code sessions concurrently (desktop app) as a working team on one program — a lead session, a review session, worker sessions. At usage-window boundaries the fleet pauses gracefully: every session commits durable state and idles. The natural last step of that pause is compact every session at this clean point, so each resumes lean next window.
Today that requires a human to type /compact into each session individually. Verified against current docs and claude --help:
/compactis strictly client-side — no tool, SDK method, or MCP surface lets the model compact its own context at a chosen quiet point;- nothing can trigger compaction on another session (no
claude compact <session-id>); the closest workaround,claude -p --resume <id> "/compact", re-engages an app-owned session's transcript from outside the app, which risks state divergence; --autocompact <tokens>sets the threshold only at launch; it cannot be adjusted at runtime, and auto-compact fires only on context fullness — never on a schedule or an idle signal;PreCompact/SessionStart(compact)hooks fire around compaction but cannot cause it.
Asks (any subset would help)
- Compact-on-idle / compact-on-demand for the session itself — a supported way for an agent (or a hook) to request compaction of its own context at a clean point, e.g. a
Compacttool gated by permission, or a hook return value that requests it. - Cross-session compaction —
claude compact <session-id>(or SDK equivalent) that safely compacts a stored session without interactively resuming it. - Runtime-adjustable autocompact threshold — let
/configor an API change the--autocompactvalue on a live session, so a fleet can be told "compact aggressively now."
The session-owned design is understandable for a single interactive session; it breaks down for coordinated multi-session workflows, which the session-messaging features otherwise support well.
🤖 Generated with Claude Code on behalf of the account owner.
Showing cached comments. Read the full discussion on GitHub ↗
3 Comments
One piece of this works today, from inside each session: the model compacting its own context at a quiet point you chose. sub-agent-compact (disclosure: mine, MIT) gives Claude a
compacttool with afocusfor the summarizer. The last step of your pause can then be "commit durable state, then call compact with a focus pointing at it", run by each session itself. Its thresholds are per party, set insettings.jsonand per agent type in frontmatter.What it does not do: compact another session, or compact on idle from outside. Cross-session
/compactstill needs this issue. Also, the plugin can only let through a compaction Claude Code asks for. WithCLAUDE_CODE_AUTO_COMPACT_WINDOW=100000that's from about 65k, so a session paused below that stays as it is until it grows past it.Related, from the model's side: let the model trigger compaction itself, write its working state to files first, and decide when to compact from what the context holds and what comes next, instead of from a token threshold. Details: https://github.com/anthropics/claude-code/issues/81691#issuecomment-5870257974
Adding a +1 from a simpler but related use case: single-user, long-running sessions over multiple days.
When working on a long session, automatic compaction can fire mid-conversation — right when you're in the middle of something important. The workaround is to run
/compactmanually before stepping away (e.g. for lunch, or end of day), but that only works if you remember while the session is still open.A
claude compact <session-id>CLI flag would let me script this: run it on Friday before the weekend across any sessions active in the last N days, so auto-compaction is less likely to interrupt active work the following week.The core ask is the same as item 2 above — just wanted to add a lightweight single-user scenario to show the value extends beyond multi-session fleet workflows.