Skip to content

fix(grok): seed stream usage so Claude Code's bar is not zero - #154

Merged
raine merged 3 commits into
raine:mainfrom
RichAyotte:fix/grok-stream-input-tokens
Sep 14, 2026
Merged

raine merged 3 commits into
raine:mainfrom
RichAyotte:fix/grok-stream-input-tokens

Conversation

@RichAyotte

Copy link
Copy Markdown
Contributor

Grok streams left message_start.usage.input_tokens at 0 and omitted input_tokens from the terminal message_delta. Claude Code copies that delta field only when it is present, so the status bar stayed at zero input tokens after every Grok turn.

The stream now seeds message_start from the local token estimate and writes the provider's input_tokens on the finish delta, matching the Codex path. Docs also record that Grok 4.5/4.6 are 500k tokens and that Claude Code needs CLAUDE_CODE_MAX_CONTEXT_TOKENS=500000 for those IDs.

RichAyotte and others added 3 commits September 13, 2026 15:26
Claude Code copies message_delta.usage.input_tokens only when the field is present, and otherwise keeps the message_start value. Grok streams left that start value at 0 and omitted input_tokens from the terminal delta, so the status bar stayed at zero input tokens after every Grok turn.
The Grok stream seeds Claude Code's live input counter with a local request
estimate in message_start, then publishes the provider's exact usage in the
terminal message_delta. The reducer converted a missing, empty, partial, or
non-numeric input_tokens field into 0, and the stream unconditionally wrote
that 0 into the terminal usage object. Claude Code treats a present field as
authoritative, so an absent provider value erased the estimate and left the
status bar at zero, which is the outcome the seeding was meant to avoid. The
monitor received the same synthesized 0 and recorded a session with no input
tokens.

Carry input-token presence through ReducerEvent::Finish as Option<u64>. The
stream renderer now omits input_tokens from the terminal usage object when the
provider did not report it, so clients keep the seeded estimate, and it passes
None to the monitor instead of zero. A provider-reported zero remains Some(0)
and is still published, because an explicit zero is real usage.

The non-streaming accumulate path still emits zero for a missing field: its
single response object always includes input_tokens, and it has no seeded
estimate to protect.

Regression coverage spans the reducer, the rendered SSE stream, and the
monitor for absent, empty, output-only, non-numeric, exact, and explicit-zero
input usage.
Merge local main e7eed88 into the
grok usage fallback branch so the pull request can be pushed and merged
without rewriting history or forcing. The original contributor commit
210c69b and the usage correction 0121054 remain ancestors; this adds
only a merge commit on top and does not rebase either of them.

The merged main carries the compaction effort cap and Codex transport
test isolation changes, which are independent of the grok usage fix.
@raine
raine merged commit 0e9a6aa into raine:main Sep 14, 2026
2 checks passed
@RichAyotte
RichAyotte deleted the fix/grok-stream-input-tokens branch September 14, 2026 16:33
@raine

raine commented Sep 14, 2026

Copy link
Copy Markdown
Owner

Thanks for fixing the Grok usage display! Merged.

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