claude-code - 💡(How to fix) Fix Emit compact_started event in stream-json output before compaction begins [1 comments, 2 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#48740Fetched 2026-04-16 06:52:18
View on GitHub
Comments
1
Participants
2
Timeline
3
Reactions
0
Timeline (top)
labeled ×2commented ×1

We run a multi-agent orchestration gateway that spawns Claude CLI processes with --output-format stream-json and streams events to a web UI via WebSocket. When compaction happens, users currently see a frozen chat with no feedback. The existing PreCompact hook doesn't help because hooks execute in the shell environment, not in the stdout JSON stream that SDK consumers read.

Root Cause

We run a multi-agent orchestration gateway that spawns Claude CLI processes with --output-format stream-json and streams events to a web UI via WebSocket. When compaction happens, users currently see a frozen chat with no feedback. The existing PreCompact hook doesn't help because hooks execute in the shell environment, not in the stdout JSON stream that SDK consumers read.

Code Example

{
  "type": "system",
  "subtype": "compact_started",
  "timestamp": "2026-04-15T19:00:00.000Z",
  "compactMetadata": {
    "trigger": "auto",
    "preTokens": 967872
  }
}
RAW_BUFFERClick to expand / collapse

Problem

When using --output-format stream-json to consume Claude CLI output programmatically (e.g. in a gateway/orchestrator), there is no way to know that context compaction has started. The CLI goes silent for potentially 3+ minutes during compaction, then emits a compact_boundary system event after completion.

This makes it impossible to show real-time progress UI to end users — they see a frozen interface with no explanation.

Current behavior

  1. User sends a message
  2. CLI detects context limit and begins compaction
  3. No output for ~1-3 minutes (silent)
  4. compact_boundary event emitted with compactMetadata (preTokens, postTokens, durationMs)
  5. Compacted summary appears as a user message

Requested behavior

Emit a compact_started system event in the stream-json output before compaction begins:

{
  "type": "system",
  "subtype": "compact_started",
  "timestamp": "2026-04-15T19:00:00.000Z",
  "compactMetadata": {
    "trigger": "auto",
    "preTokens": 967872
  }
}

This would allow SDK consumers and gateway applications to:

  • Show a "Compacting context, please wait..." indicator immediately
  • Display the pre-compaction token count
  • Pair it with the existing compact_boundary event to show duration

Context

We run a multi-agent orchestration gateway that spawns Claude CLI processes with --output-format stream-json and streams events to a web UI via WebSocket. When compaction happens, users currently see a frozen chat with no feedback. The existing PreCompact hook doesn't help because hooks execute in the shell environment, not in the stdout JSON stream that SDK consumers read.

Related issues

  • #43946 (PreCompact hook event) — similar goal but hooks-based, not stream-json
  • #25689 (Context usage threshold hook)
  • #46431 (Background compaction)

extent analysis

TL;DR

Emit a compact_started system event in the stream-json output before compaction begins to provide real-time progress updates to end users.

Guidance

  • Modify the Claude CLI to emit a compact_started system event with compactMetadata before starting context compaction.
  • Update the gateway/orchestrator to listen for the compact_started event and display a "Compacting context, please wait..." indicator to end users.
  • Pair the compact_started event with the existing compact_boundary event to show duration and provide a better user experience.
  • Consider updating the SDK to handle the new compact_started event and provide a seamless integration with the gateway/orchestrator.

Example

{
  "type": "system",
  "subtype": "compact_started",
  "timestamp": "2026-04-15T19:00:00.000Z",
  "compactMetadata": {
    "trigger": "auto",
    "preTokens": 967872
  }
}

Notes

The proposed solution assumes that the Claude CLI can be modified to emit the compact_started event. If this is not possible, alternative solutions may need to be explored, such as using a different output format or implementing a custom solution to provide real-time progress updates.

Recommendation

Apply workaround: Implement the compact_started event emission in the Claude CLI and update the gateway/orchestrator to handle this new event, as this provides a clear and direct solution to the problem of providing real-time progress updates to end users.

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