Skip to content

Conversation

@dancer
Copy link
Collaborator

@dancer dancer commented Feb 11, 2026

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: "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

before (negative tokens)
--- grok-3-mini ---
raw: completion_tokens=2, reasoning_tokens=317, total_tokens=331
sdk: outputTokens=2, textTokens=-315, reasoningTokens=317, totalTokens=14
after (correct)
--- grok-3-mini ---
raw: completion_tokens=1, reasoning_tokens=386, total_tokens=399
sdk: outputTokens=387, textTokens=1, reasoningTokens=386, totalTokens=399

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 (run pnpm changeset in root)
  • i have reviewed this pull request (self-review)

@dancer
Copy link
Collaborator Author

dancer commented Feb 11, 2026

@Jaaneek can I get your 👀 on this as well please

cc: @shaper will wait on gateway team before merge just to quadriple check

@dancer
Copy link
Collaborator Author

dancer commented Feb 11, 2026

also @williamlmao i know you reported the original issue so tagging you in would love some community feedback once this is merged! pls!

@Jaaneek
Copy link
Contributor

Jaaneek commented Feb 11, 2026

Will take a look in few hours, thanks for working on this

@Jaaneek
Copy link
Contributor

Jaaneek commented Feb 11, 2026

Just confirmed this with the team, will add it to docs on our side as well. Thank you! Good to merge

@dancer dancer merged commit e1d5111 into main Feb 11, 2026
28 checks passed
@dancer dancer deleted the josh/xai-usage-fix branch February 11, 2026 17:21
vercel-ai-sdk bot pushed a commit that referenced this pull request Feb 11, 2026
@vercel-ai-sdk vercel-ai-sdk bot removed the backport label Feb 11, 2026
@vercel-ai-sdk
Copy link
Contributor

vercel-ai-sdk bot commented Feb 11, 2026

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

gr2m pushed a commit that referenced this pull request Feb 11, 2026
…oning models (#12441)

This is an automated backport of #12425 to the release-v5.0 branch.

Co-authored-by: josh <josh@afterima.ge>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants