fix: use current model's context window for usage_update size#412
Open
timvisher-dd wants to merge 2 commits intozed-industries:mainfrom
Open
fix: use current model's context window for usage_update size#412timvisher-dd wants to merge 2 commits intozed-industries:mainfrom
timvisher-dd wants to merge 2 commits intozed-industries:mainfrom
Conversation
0962531 to
3abd1a0
Compare
4 tasks
36c524a to
ac1745a
Compare
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Previously usage_update.size used the minimum context window across all models in modelUsage. Now it looks up the context window for the model from the most recent top-level assistant message, with prefix matching to handle alias/versioned model ID mismatches. Also skips <synthetic> assistant messages (e.g. from /compact) so they don't corrupt the tracked model. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
ac1745a to
acdb28f
Compare
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.
The
usage_updatenotification reportssize: 200000even when the active model has a 1M context window (e.g.opus[1m]), causing clients to display incorrect context utilization (e.g.689k/200k (344.3%)instead of689k/1000k (68.9%)).Four bugs fixed:
Math.minacross allmodelUsageentries, so subagent models (Sonnet/Haiku with 200k windows) dragged down the reported size for the main Opus 1M model. Now tracks the top-level assistant model and looks up its context window specifically.modelUsageby the requested model alias (e.g.claude-opus-4-6) whileBetaMessage.modelon assistant messages has the resolved API response model (e.g.claude-opus-4-6-20250514). The exact-match lookup always missed, falling back to the hardcoded 200k default. Now falls back to prefix matching, preferring the longest/most-specific match./compactand similar commands emit assistant messages withmodel: "<synthetic>". These were updatinglastAssistantModel, causing the nextusage_updateto miss themodelUsagelookup and fall back to the 200k default. Now filters out<synthetic>models.usage_updatewas sent oncompact_boundary, so clients kept showing the pre-compaction context size (e.g.944k/1m) right after "Compacting completed" until the next full turn. Now sendsused: 0immediately on compaction. This is a deliberate approximation — the exact post-compaction size isn't known until the SDK's next API call, which replaces it within seconds. The alternative (no update) is worse UX: showing a full context bar right after compaction.Eight new tests cover: token sum correctness, current-model context window lookup, model switching, subagent isolation, prefix matching in both directions, and synthetic message filtering.
Note:
bin/test(local CI validation script) is cherry-picked from #353.Would fix
agent-shell's usage indicator which currently has to defend against this broken math: xenodium/agent-shell#364Testing