Show context compaction events in studio code CLI#3019
Draft
Conversation
The Agent SDK emits system messages when it shrinks context (compacting status, compact_boundary, microcompact_boundary) but the CLI UI ignored them, so long sessions appeared to stop suddenly while the SDK was silently managing context. Surface these events as informational messages and as a loader status during compaction. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Track the model's input + cache token counts against the reported contextWindow on every result message and render the percentage in the prompt footer alongside the existing status message, so users can see how full the context window is before it triggers compaction. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The previous version divided result.modelUsage cumulative totals by contextWindow, which inflated the percentage in multi-iteration turns (each iteration's cached reads were summed, so an 11-iteration turn could report > 100% even when no single API call was close to the window). Track the last assistant message's usage snapshot instead — that reflects the actual prompt size sent on the most recent API call — and cache the context window from result messages so the label can update mid-turn as iterations stream in. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related issues
Proposed Changes
The Claude Agent SDK emits
systemmessages when it manages the context window, but thestudio codeCLI UI only handledassistant,user, andresultmessages. As a result, long agent turns appeared to "stop suddenly" while the SDK was silently compacting context in the background.This PR wires up the missing message types in
apps/cli/ai/ui.ts:handleMessage:status: compacting— sets the loader message to "Compacting context…" so users see why the turn has paused.compact_boundary— shows an info message when the SDK summarizes the transcript, including the trigger (auto/manual) and the token count before compaction.microcompact_boundary— shows an info message when the SDK drops old tool-result attachments (screenshots, large tool outputs) to free tokens without summarizing the full transcript. Includes the number of attachments dropped and tokens saved.The
microcompact_boundarytype isn't in the public SDK type definitions yet, so it's narrowed via a defensive cast.Testing Instructions
npm run cli:buildnode apps/cli/dist/cli/main.mjs codePre-merge Checklist