[Feature] Compact a running background session from outside (no external trigger exists)
What I'm trying to do
I run long-lived background agent sessions (claude --agent <role> --bg) as workers. A session finishes a task, reports, and stays alive to take the next one. Between tasks is exactly the "natural break" the docs recommend compacting at:
To choose when its overhead happens, run /compact at a natural break in your work, such as between tasks, instead of waiting for auto-compaction to trigger mid-task. — https://code.claude.com/docs/en/prompt-caching (Compacting the conversation)
The gap
There is no way to compact a running session from outside it, so that advice cannot be followed for background agents:
/compactis a slash command typed by a human inside an interactive session. A background session has no human at its prompt.claude agentshas no compact verb (checkedclaude agents --help, v2.1.241).- The "Resume from summary" dialog (https://code.claude.com/docs/en/sessions) applies to
claude --resumeof a session that is not running, and is interactive. - A message sent to the session over cross-session messaging is just a user turn; it cannot invoke a slash command.
That leaves --autocompact <tokens> as the only lever, and a threshold fires wherever the context happens to cross it, which is mid-task by construction. Concretely: my lanes measure 134k–188k tokens per task, so a threshold at 250k lands partway through the next task, every time. No value of the threshold changes that — lower fires earlier inside the task, higher fires later inside a subsequent one.
What I'd like
Any external trigger for compaction of a running session. In rough order of usefulness to me:
claude agents compact <session-id>(optionally with focus instructions, like/compact <instructions>).- A control message over the cross-session messaging channel that the receiving session interprets as
/compactrather than as user text. - A hook or SDK surface that can request compaction on the next turn boundary.
Why it matters
The cost case is already in your docs: after compaction, later requests carry the summary instead of the full history. For a worker that lives for hours across many tasks, being able to compact at a task boundary is the difference between every later turn carrying one task's history and carrying all of them. Today that saving is only reachable by killing the session and starting a new one, which throws away the state that made it worth keeping alive.
Environment
Claude Code 2.1.241, macOS (darwin 25.3), Claude subscription (1-hour cache TTL).
This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗