Repository navigation
fix(xai): include reasoning tokens in completion usage - #3493
Merged
Merged
Conversation
xAI reports reasoning_tokens beside completion_tokens (total = prompt + completion + reasoning). Fold them into Usage.completion_tokens and keep thinking_tokens as the breakdown, matching the OpenAI, Anthropic and Gemini mappers.
4 of 6 tasks
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.
Why are these changes needed?
xAI reports reasoning tokens beside the completion, not inside it, so
Usage.completion_tokensfor Grok reasoning models leaves them out andprompt + completionfalls short oftotal. Fold reasoning intocompletion_tokensand keepthinking_tokensas its breakdown, as the OpenAI, Anthropic and Gemini mappers already do. Follow-up to #3446, which fixes the same gap for Gemini.Evidence, from the live xAI API (
grok-4-fast-reasoning):SamplingUsageisprompt=204, completion=32, reasoning=300, total=536. The old mapper passedcompletion=32through, soprompt + completion = 236 != 536.prompt + completion + reasoning == totaldoes hold.Agent.ask()on the patched mapper givesp+c == totalin both modes: plain201 + 283 = 484, streaming201 + 313 = 514, each withthinking_tokenskept as the breakdown (282 / 312).test_completion_includes_reasoning_tokens(the numbers above) andtest_reasoning_without_completion_tokens. The existing reasoning test'stotal_tokensis corrected to a consistent value.Merge danger:
completion_tokens(and AG-UIoutput_tokens) now grow by the reasoning count. Pricing and cross-provider comparisons become correct, but anything that budgets oncompletion_tokenswill reach its limit sooner for reasoning models.response_token_countalready includes thoughts. ACP'sthought_tokenswas not verified against a live agent and is left unchanged.Related issue number
Follows up #3446 (fixes #3443 for Gemini).
Checks
AI assistance