fix(claudecode): include cache tokens and per-model window in [ctx: ~N%]#765
Closed
AaronZ345 wants to merge 1 commit intochenhg5:mainfrom
Closed
fix(claudecode): include cache tokens and per-model window in [ctx: ~N%]#765AaronZ345 wants to merge 1 commit intochenhg5:mainfrom
AaronZ345 wants to merge 1 commit intochenhg5:mainfrom
Conversation
chenhg5
approved these changes
Apr 24, 2026
Owner
chenhg5
left a comment
There was a problem hiding this comment.
Review Summary
Fixes two real bugs in the [ctx: ~N%] indicator for Claude Code sessions. Well-tested and consistent with the existing codex implementation.
✅ Good:
- Correctly aggregates
input_tokens+cache_read_input_tokens+cache_creation_input_tokens— matches how Anthropic bills context modelContextWindow()cleanly detects[1m]/-1msuffix (case-insensitive)ContextWindowfield on Event is a clean extension — no breaking changescontextIndicator()gracefully falls back todefaultContextWindowwhen window <= 0- Two focused test additions: cache aggregation test and 7-case model window table test
- Consistent with agent/codex which already does the equivalent
LGTM. Both bugs verified, tests comprehensive, CI green.
Two bugs broke the context indicator in the claudecode path: 1. handleResult read only usage.input_tokens — the per-turn delta. In long sessions almost all input is cache_read, so the delta stays <100, sdkPlausible is false, and the [ctx: ~N%] suffix never appears. 2. contextIndicator divided by a hardcoded modelContextWindow = 200_000. Users on claude-opus-4-7[1m] saw 5x inflated percentages (e.g. 10% actual usage rendered as 50%). Fix: - handleResult now sums input_tokens + cache_read_input_tokens + cache_creation_input_tokens and stamps ContextWindow on the event via modelContextWindow(cs.model): "[1m]" / "-1m" suffix → 1_000_000, everything else → 200_000. - Event gains a ContextWindow int field (additive, no break). contextIndicator takes window as an argument; the old constant becomes defaultContextWindow, used only as a fallback. agent/codex already does the equivalent (reads total_token_usage and model_context_window from rollout events). This brings the claudecode path to parity.
76ac319 to
ff26684
Compare
4 tasks
Contributor
Author
|
Closing — #774 subsumes both fixes from this PR:
Thanks @Cigarrr for the catch. |
Contributor
Author
|
Closing — superseded by #774. The per-model context window fix ( |
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.
Two bugs broke the
[ctx: ~N%]indicator in the claudecode path:Cache tokens missing —
handleResultread onlyusage.input_tokens(per-turn delta). In long sessions almost all input iscache_read_input_tokens, so the delta stays <100,sdkPlausible := event.InputTokens >= 100is false, and the suffix never appears.Hardcoded 200k window —
contextIndicatordivided byconst modelContextWindow = 200_000. Users onclaude-opus-4-7[1m]saw 5x inflated percentages.agent/codexalready does the equivalent (readstotal_token_usage+model_context_windowfrom rollout events). This PR brings claudecode to parity.Changes
core/message.go: addEvent.ContextWindow int(additive).agent/claudecode/session.go: newmodelContextWindow(model)maps[1m]/-1m→ 1M, default 200k.handleResultsumsinput_tokens + cache_read_input_tokens + cache_creation_input_tokensand stamps the window on the event.core/engine.go: remove the constant, rename todefaultContextWindow(fallback only).contextIndicator(inputTokens, window int)takes window as a parameter.Tests
TestHandleResultAggregatesCacheTokensAnd1MWindow— verifies three-field sum + 1M window for[1m]model.TestModelContextWindow— table test across Opus/Sonnet/Haiku variants, empty string, uppercase.TestHandleResultParsesUsage/TestHandleResultNoUsageunchanged and green.go build ./...clean.Related: #547