Skip to content

fix(xai): negative text_output_tokens for xAI reasoning models#11536

Merged
lgrammel merged 6 commits intomainfrom
copilot/fix-negative-text-output-tokens
Jan 5, 2026
Merged

fix(xai): negative text_output_tokens for xAI reasoning models#11536
lgrammel merged 6 commits intomainfrom
copilot/fix-negative-text-output-tokens

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 5, 2026

Background

xAI's Chat API returns completion_tokens that 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 + reasoningTokenscompletion_tokens
  • outputTokens.text: completion_tokenscompletion_tokens - reasoningTokens

Example:

// xAI API response
{
  completion_tokens: 95,  // includes both text (6) and reasoning (89)
  completion_tokens_details: { reasoning_tokens: 89 }
}

// Before: text = 6 - 89 = -83 ❌
// After:  text = 95 - 89 = 6 ✅

Updated test cases:

  • Updated all tests in convert-xai-chat-usage.test.ts to reflect correct xAI API format where completion_tokens includes reasoning tokens
  • Fixed 3 inline snapshots in xai-chat-language-model.test.ts for reasoning model tests to match the corrected token calculation

Added changeset for the @ai-sdk/xai package to ensure proper versioning.

Manual Verification

Verified with reported scenario:

  • Input: completion_tokens: 95, reasoning_tokens: 89
  • Output: text: 6, reasoning: 89, total: 95 (no negative values)
  • All 126 tests passing (both node and edge environments)

Checklist

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)
Original prompt

This section details on the original issue you should resolve

<issue_title>Negative text_output_tokens when using grok-4-fast-reasoning</issue_title>
<issue_description>### Description

I noticed that grok-4-fast-reasoning would result in a negative number in the usage field. I logged a request, here is the raw:

"{"prompt_tokens":279,"completion_tokens":6,"total_tokens":374,"prompt_tokens_details":{"text_tokens":279,"audio_tokens":0,"image_tokens":0,"cached_tokens":149},"completion_tokens_details":{"reasoning_tokens":89,"audio_tokens":0,"accepted_prediction_tokens":0,"rejected_prediction_tokens":0}}"

In the usage field, we end up with "-83" text_output_tokens which is the 6 completion tokens minus the 89 reasoning tokens. Shouldn't this actually be 6, text_output_tokens, 89 reasoning tokens, and 95 total output tokens?

AI SDK Version

"ai": "^6.0.3",
"@ai-sdk/xai": "3.0.1",

Code of Conduct

  • I agree to follow this project's Code of Conduct</issue_description>

<agent_instructions>Investigate the token counting in the xai provider and update if needed.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

- 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
Copilot AI requested a review from lgrammel January 5, 2026 20:36
@lgrammel
Copy link
Copy Markdown
Collaborator

lgrammel commented Jan 5, 2026

@copilot add a patch changeset for the @ai-sdk/xai package

Co-authored-by: lgrammel <205036+lgrammel@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 5, 2026

@copilot add a patch changeset for the @ai-sdk/xai package

Done in e9782d1. Added patch changeset for @ai-sdk/xai package.

@lgrammel
Copy link
Copy Markdown
Collaborator

lgrammel commented Jan 5, 2026

@copilot fix failing tests in packages/xai

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>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 5, 2026

@copilot fix failing tests in packages/xai

Fixed in e0fd0eb. Updated test snapshots for the 3 reasoning model tests to reflect the corrected token calculation. All 126 tests now passing (both node and edge).

@lgrammel lgrammel marked this pull request as ready for review January 5, 2026 21:08
@lgrammel lgrammel changed the title Fix negative text_output_tokens for xAI reasoning models fix(xai): negative text_output_tokens for xAI reasoning models Jan 5, 2026
@lgrammel lgrammel enabled auto-merge (squash) January 5, 2026 21:09
@lgrammel lgrammel merged commit 77012ef into main Jan 5, 2026
19 checks passed
@lgrammel lgrammel deleted the copilot/fix-negative-text-output-tokens branch January 5, 2026 21:35
@shaper shaper added the backport Admins only: add this label to a pull request in order to backport it to the prior version label Jan 9, 2026
ghost pushed a commit that referenced this pull request Jan 9, 2026
@ghost ghost removed the backport Admins only: add this label to a pull request in order to backport it to the prior version label Jan 9, 2026
@ghost
Copy link
Copy Markdown

ghost commented Jan 9, 2026

⚠️ Backport to release-v5.0 created but has conflicts: #11664

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)
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.

Negative text_output_tokens when using grok-4-fast-reasoning

4 participants