fix(xai): negative text_output_tokens for xAI reasoning models#11536
Merged
fix(xai): negative text_output_tokens for xAI reasoning models#11536
Conversation
- Update convert-xai-chat-usage to treat completion_tokens as including reasoning tokens - Change outputTokens.total from (completion_tokens + reasoning) to just completion_tokens - Change outputTokens.text from completion_tokens to (completion_tokens - reasoning_tokens) - Update all tests to match the new OpenAI-compatible format - Add test case for the specific issue with grok-4-fast-reasoning Co-authored-by: lgrammel <205036+lgrammel@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix negative text_output_tokens in grok-4-fast-reasoning
Fix negative text_output_tokens for xAI reasoning models
Jan 5, 2026
Collaborator
|
@copilot add a patch changeset for the |
Co-authored-by: lgrammel <205036+lgrammel@users.noreply.github.com>
Contributor
Author
Collaborator
|
@copilot fix failing tests in |
Update inline snapshots in reasoning model tests to reflect the corrected token calculation where completion_tokens includes reasoning tokens. Co-authored-by: lgrammel <205036+lgrammel@users.noreply.github.com>
Contributor
Author
lgrammel
approved these changes
Jan 5, 2026
aayush-kapoor
approved these changes
Jan 5, 2026
ghost
pushed a commit
that referenced
this pull request
Jan 9, 2026
|
|
4 tasks
dancer
added a commit
that referenced
this pull request
Feb 11, 2026
…ls (#12425) ## background follow-up to #11536 — xAI's API reports `completion_tokens` as text-only, with `reasoning_tokens` separate in `completion_tokens_details` (unlike OpenAI which includes reasoning in `completion_tokens`). per [xAI docs](https://docs.x.ai/docs/key-information/consumption-and-rate-limits): "reasoning token consumption will be counted separately from completion_tokens, but will be counted in the total_tokens" this results in negative `textTokens` and underreported `outputTokens` for all reasoning models (grok-4, grok-3-mini, grok-3-mini-fast, grok-code-fast-1, etc.) ## summary - change `outputTokens.total` from `completion_tokens` to `completion_tokens + reasoningTokens` - change `outputTokens.text` from `completion_tokens - reasoningTokens` to `completion_tokens` - rewrite unit tests with real xAI API token values ## verification <details> <summary>before (negative tokens)</summary> ``` --- grok-3-mini --- raw: completion_tokens=2, reasoning_tokens=317, total_tokens=331 sdk: outputTokens=2, textTokens=-315, reasoningTokens=317, totalTokens=14 ``` </details> <details> <summary>after (correct)</summary> ``` --- grok-3-mini --- raw: completion_tokens=1, reasoning_tokens=386, total_tokens=399 sdk: outputTokens=387, textTokens=1, reasoningTokens=386, totalTokens=399 ``` </details> ## checklist - [x] tests have been added / updated (for bug fixes / features) - [ ] documentation has been added / updated (for bug fixes / features) - [x] a _patch_ changeset for relevant packages has been added (run `pnpm changeset` in root) - [x] i have reviewed this pull request (self-review)
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.
Background
xAI's Chat API returns
completion_tokensthat include both text and reasoning tokens, matching OpenAI's o1 format. The SDK was treating this value as text-only, causing incorrect calculations that resulted in negative text token counts when reasoning tokens exceeded the misinterpreted value.Summary
Updated token calculation in
convert-xai-chat-usage.ts:outputTokens.total:completion_tokens + reasoningTokens→completion_tokensoutputTokens.text:completion_tokens→completion_tokens - reasoningTokensExample:
Updated test cases:
convert-xai-chat-usage.test.tsto reflect correct xAI API format wherecompletion_tokensincludes reasoning tokensxai-chat-language-model.test.tsfor reasoning model tests to match the corrected token calculationAdded changeset for the
@ai-sdk/xaipackage to ensure proper versioning.Manual Verification
Verified with reported scenario:
completion_tokens: 95,reasoning_tokens: 89text: 6,reasoning: 89,total: 95(no negative values)Checklist
pnpm changesetin the project root)Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.