Skip to content

fix: use context_window for [1m] model display instead of total_tokens#117

Merged
graykode merged 4 commits into
graykode:mainfrom
KorenKrita:fix/1m-context-window-detection
May 13, 2026
Merged

fix: use context_window for [1m] model display instead of total_tokens#117
graykode merged 4 commits into
graykode:mainfrom
KorenKrita:fix/1m-context-window-detection

Conversation

@KorenKrita
Copy link
Copy Markdown
Contributor

@KorenKrita KorenKrita commented May 11, 2026

Problem

Sessions were incorrectly displaying the [1m] suffix on models that only have a 200k context window.

Root cause: sessions.rs was using session.total_tokens() > 200_000 to detect 1M context, but total_tokens() accumulates all token usage across the entire session lifetime, including cache_read tokens. Since cache_read is reused on every turn, a long-running session on a standard 200k model can easily accumulate 200k+ total tokens and trigger a false [1m] display.

Additionally, fmt_tokens always rendered one decimal place, producing awkward labels like 200.0k instead of 200k.

Fixes

1. Use context_window for exact [1m] display (sessions.rs)

// Before (wrong): total_tokens includes cache_read and grows over session lifetime
let is_1m = session.total_tokens() > 200_000 || session.model.contains("[1m]");

// After: only render the literal [1m] suffix for actual 1M windows or explicit model suffixes
let is_1m = session.context_window >= 1_000_000 || session.model.contains("[1m]");

The collector already computes context_window. The UI should not infer a 1M label from cumulative token totals, and it should not treat every non-200k window as a literal 1M window. Codex can report windows such as 258.4k, which are larger than 200k but are not 1M.

2. Drop unnecessary decimal in fmt_tokens (mod.rs)

Before: 200.0k, 1.0M
After:  200k, 1M

Fractional values like 123.4k are preserved. Whole numbers get a clean display.

3. Regression coverage

Added a sessions-panel regression test for a Codex session with context_window: 258_400, verifying that the rendered model label does not include [1m].

Affected files

  • src/ui/sessions.rs — use exact 1M detection for the [1m] model suffix and add regression coverage
  • src/ui/mod.rs — drop unnecessary decimals in fmt_tokens()

Testing

  • cargo test — 137 passed
  • cargo clippy -- -D warnings — passed

KorenKrita and others added 4 commits May 11, 2026 17:18
total_tokens() includes cache_read which inflates far beyond 200k on
normal 200k-window sessions, causing false [1m] display.

context_window is already correctly set by context_window_for_model()
in the collector. Use it directly.
200.0k → 200k, 1.0M → 1M. Fractional values like 123.4k preserved.
> 200_000 is semantically correct (any window larger than 200k is
extended) and handles future window sizes like 500k correctly.
@graykode graykode merged commit cd97cf9 into graykode:main May 13, 2026
7 checks passed
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.

2 participants