[Bug]: Context Window Calculation wrong for claude code
Before submitting
- [x] I searched existing issues and did not find a duplicate.
- [x] I included enough detail to reproduce or investigate the problem.
Area
apps/desktop
Steps to reproduce
Hi, I've been running into this issue for a couple of days. The context window calculation was stuck on 5% after multiple turns for a day and then suddenly shot up to 100% even though the usage was only 15% (confirmed with claude code cli) -see attached images.
Expected behavior
Other threads with codex seems okay no issues.
Actual behavior
Active and Working context window calculator.
Impact
Blocks work completely
Version or commit
No response
Environment
MACOS Tahoe 26.4
Logs or stack traces
Screenshots, recordings, or supporting files
No response
Workaround
No response
facing the exact same issue, how did you open the chat in claude code terminal?
claude -r should show the sessions.
-r, --resume [value] Resume a conversation by session ID, or open
interactive picker with optional search term
same for me
same here
Traced this to apps/web/src/lib/contextWindow.ts deriveLatestContextWindowSnapshot. It walks activities newest->oldest and returns the first context-window.updated payload where usedTokens > 0, then computes percent as Math.min(100, (usedTokens / maxTokens) * 100).
The 5% stuck -> 100% spike pattern while claude code cli reports 15% points at the Claude Code adapter emitting different context-window.updated payloads than Codex: either a stale usedTokens between turns, or a post-compaction snapshot that sends a cumulative usedTokens without a matching maxTokens bump.
To pin it, it would help to see a console dump of the last 3-4 context-window.updated activities around the spike:
useOrchestrationStore.getState().activities.filter(a => a.kind === 'context-window.updated').slice(-5)
That shows which field is off (usedTokens spiking, maxTokens missing, or a mismatch between inputTokens + outputTokens and usedTokens). Without that, any fix here is guessing at which of those three paths the Claude Code adapter is hitting.
cc @shresthkapoor7 @fireMarshall88 @madebyjulz (all reported the same symptom).
we rely on Claude emitting task_progress events to get the compacted context window. Sometimes it doens't emit these, causing us to fallback to the accumulative token spend (not greate). Not sure if there's a better way?
this bugged me as well - so I created a PR: https://github.com/pingdotgg/t3code/pull/2551
Facing the same issue.
Sorry for the slow reply, @juliusmarminge - missed the ping.
@amitbet's PR #2551 looks like the right answer. Normalizing at ClaudeAdapter.ts (the BE emitter) is cleaner than what I was thinking (downstream handling in contextWindow.ts) - it treats the accumulated total as processed-token telemetry only when there's no context-accurate used-token field, which is exactly the fallback you described. Same fix, applied at the source.
Might be worth a quick look from a maintainer; coderabbit and macroscope already flagged it as approvable.
Claude usage accounting now separates active context usage from cumulative processed tokens and preserves the last context-accurate reading when only cumulative totals arrive. This removes the 5%-to-100% fallback behavior reported here.
This was closed as part of a large repo-wide maintenance sweep. If you think it's still relevant, please reopen.