fix: use current model's context window for usage_update size#412
Open
timvisher-dd wants to merge 2 commits intoagentclientprotocol:mainfrom
Open
fix: use current model's context window for usage_update size#412timvisher-dd wants to merge 2 commits intoagentclientprotocol:mainfrom
timvisher-dd wants to merge 2 commits intoagentclientprotocol:mainfrom
Conversation
0962531 to
3abd1a0
Compare
4 tasks
ac1745a to
acdb28f
Compare
4 tasks
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
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) <[email protected]>
acdb28f to
fea5979
Compare
SteffenDE
reviewed
Mar 24, 2026
Comment on lines
+611
to
+616
| const matchingModelUsage = currentModel | ||
| ? (message.modelUsage[currentModel] ?? | ||
| Object.entries(message.modelUsage) | ||
| .filter(([key]) => currentModel.startsWith(key) || key.startsWith(currentModel)) | ||
| .sort((a, b) => b[0].length - a[0].length)[0]?.[1]) | ||
| : undefined; |
Contributor
There was a problem hiding this comment.
In my testing, the result modelUsage contained a claude-opus-4-6[1m] and the assistant message claude-opus-4-6 (no suffix). But assuming there can be a suffix - if the assistant message contains claude-opus-4-6-20250514 or similar, this would not match, because it checks for startsWith.
Maybe do something like this and actually calculate the common prefix:
function commonPrefixLength(a: string, b: string) {
let i = 0;
while (i < a.length && i < b.length && a[i] === b[i]) {
i++;
}
return i;
}
function getMatchingModelUsage(modelUsage: Record<string, ModelUsage>, currentModel: string) {
let bestKey: string | null = null;
let bestLen = 0;
for (const key of Object.keys(modelUsage)) {
const len = commonPrefixLength(key, currentModel);
if (len > bestLen) {
bestLen = len;
bestKey = key;
}
}
if (bestKey) {
return modelUsage[bestKey];
}
}and then use it as
const matchingModelUsage = lastAssistantModel ? getMatchingModelUsage(message.modelUsage, lastAssistantModel) : null;
const contextWindowSize = matchingModelUsage?.contextWindow ?? 200000;
lastContextWindowSize = contextWindowSize;
SteffenDE
reviewed
Mar 24, 2026
Contributor
SteffenDE
left a comment
There was a problem hiding this comment.
I'd do one change, other than that looks good to me. (I'd drop the bin/test file from the PR).
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