fix(openai-compatible): clamp negative outputTokens.text from inconsistent provider usage - #18614
Merged
Merged
Conversation
…stent provider usage
aayush-kapoor
approved these changes
Aug 9, 2026
github-actions Bot
added a commit
that referenced
this pull request
Aug 9, 2026
…stent provider usage (#18614) ## Problem Providers can return internally inconsistent usage where `completion_tokens_details.reasoning_tokens` exceeds `completion_tokens`. Observed with Baseten serving `moonshotai/Kimi-K3`: the model spent its whole output budget on reasoning and hit the length stop before emitting any text, and the response reported: ```json "usage": { "prompt_tokens": 951, "completion_tokens": 6000, "total_tokens": 6952, "prompt_tokens_details": { "cached_tokens": 60 }, "completion_tokens_details": { "reasoning_tokens": 6001 } } ``` (`total_tokens` = 951 + 6001, so `completion_tokens` is the field undercounting the actual generation here.) `convertOpenAICompatibleChatUsage` computes `text = completion_tokens - reasoning_tokens`, which yields `outputTokens.text: -1` for this response. A negative token count is nonsensical and breaks downstream consumers that validate usage values (e.g. schemas requiring non-negative counts). ## Fix Clamp the text share at 0 — the text portion of completion tokens can never be negative. `total` and `reasoning` remain exactly as reported by the provider, so consumers that aggregate `text + reasoning` stay aligned with the actual generation (6001 reasoning tokens here). ## Testing - Added `convert-openai-compatible-chat-usage.test.ts` with a regression case replaying the incident payload, plus null/normal cases. - Updated 5 snapshots that had baked in negative `text` values (`-318`, `-229`, `-338`, `-201`) from recorded xAI fixtures — the same class of provider-side usage inconsistency, previously recorded as expected output. - Reproduced end-to-end via a mock fetch replaying the incident payload through the Baseten provider on both the `generateText` and `streamText` paths: `textTokens: -1` pre-fix, `0` post-fix. - Full `@ai-sdk/openai-compatible` suite (node + edge, 238 tests) and `tsc --build` pass.
Contributor
|
✅ Backport PR created: #18620 |
Contributor
|
🚀 Published in:
|
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.
Problem
Providers can return internally inconsistent usage where
completion_tokens_details.reasoning_tokensexceedscompletion_tokens. Observed with Baseten servingmoonshotai/Kimi-K3: the model spent its whole output budget on reasoning and hit the length stop before emitting any text, and the response reported:(
total_tokens= 951 + 6001, socompletion_tokensis the field undercounting the actual generation here.)convertOpenAICompatibleChatUsagecomputestext = completion_tokens - reasoning_tokens, which yieldsoutputTokens.text: -1for this response. A negative token count is nonsensical and breaks downstream consumers that validate usage values (e.g. schemas requiring non-negative counts).Fix
Clamp the text share at 0 — the text portion of completion tokens can never be negative.
totalandreasoningremain exactly as reported by the provider, so consumers that aggregatetext + reasoningstay aligned with the actual generation (6001 reasoning tokens here).Testing
convert-openai-compatible-chat-usage.test.tswith a regression case replaying the incident payload, plus null/normal cases.textvalues (-318,-229,-338,-201) from recorded xAI fixtures — the same class of provider-side usage inconsistency, previously recorded as expected output.generateTextandstreamTextpaths:textTokens: -1pre-fix,0post-fix.@ai-sdk/openai-compatiblesuite (node + edge, 238 tests) andtsc --buildpass.