fix(statusline): correct context percentage when switching models mid-session#1358
Open
klausagnoletti wants to merge 1 commit into
Open
Conversation
…-session When a Claude Code session starts with Opus (1M context window) and the user switches to Sonnet/Haiku (200K window) — or vice versa — the statusline may display an incorrect context percentage. Claude Code's context_window_size JSON field can reflect the session's starting model rather than the currently active model. Root cause: used_percentage is calculated by Claude Code against context_window_size from session start. After a model switch, the denominator is wrong. Fix: derive the expected context window from model_name (which IS current), then recalculate context_pct using total_input_tokens (absolute, model-independent) against the correct window. Falls back to proportional rescaling if total_input is unavailable. No-op when context_max already matches the model's expected size. Model→window mapping: Opus 4.x → 1,048,576 tokens Sonnet 4.x → 200,000 tokens Haiku 4.x → 200,000 tokens Fable → 200,000 tokens Unknown → no correction (display as-is)
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.
Problem
When a Claude Code session starts with one model (e.g. Opus, 1M context window) and the user switches to another model (e.g. Sonnet, 200K context window) mid-session, the context percentage displayed in the status line becomes incorrect.
Root cause:
context_window_sizein the Claude Code session JSON can reflect the session's starting model rather than the currently active model. Theused_percentagefield is calculated against that stale denominator, so switching from Opus → Sonnet shows ~14% when the real usage is ~75%.Fix
After the defaults block, derive the expected context window from
model_name(which IS current), then recalculatecontext_pctusingtotal_input_tokens(absolute token count, model-independent) against the correct window. Falls back to proportional rescaling iftotal_inputis zero. No-op whencontext_maxalready matches the model's expected size.Model → context window mapping:
Example
Session started as Opus, 150K tokens consumed, user switches to Sonnet:
context_max=1048576,context_pct=14%(wrong)context_max=200000,context_pct=75%(correct)Change
21 lines added to
Releases/v4.0.3/.claude/statusline-command.sh, immediately after the defaults block. Non-destructive: the block only fires when the reportedcontext_maxmismatches the model's known window size.