Skip to content

fix(statusline): correct context percentage when switching models mid-session#1358

Open
klausagnoletti wants to merge 1 commit into
danielmiessler:mainfrom
klausagnoletti:fix/statusline-model-aware-context-window
Open

fix(statusline): correct context percentage when switching models mid-session#1358
klausagnoletti wants to merge 1 commit into
danielmiessler:mainfrom
klausagnoletti:fix/statusline-model-aware-context-window

Conversation

@klausagnoletti

Copy link
Copy Markdown

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_size in the Claude Code session JSON can reflect the session's starting model rather than the currently active model. The used_percentage field 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 recalculate context_pct using total_input_tokens (absolute token count, model-independent) against the correct window. Falls back to proportional rescaling if total_input is zero. No-op when context_max already matches the model's expected size.

Model → context window mapping:

Model Tokens
Opus 4.x 1,048,576
Sonnet 4.x 200,000
Haiku 4.x 200,000
Fable 200,000
Unknown no correction

Example

Session started as Opus, 150K tokens consumed, user switches to Sonnet:

  • Before fix: context_max=1048576, context_pct=14% (wrong)
  • After fix: 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 reported context_max mismatches the model's known window size.

…-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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant