SIGN IN SIGN UP

fix: prevent negative text output token counts when reasoning exceeds completion usage (#19316)

## Background

Published OpenAI, Groq, Alibaba, Moonshot AI, and DeepSeek packages
could normalize inconsistent provider usage into negative text token
counts, which could contaminate aggregated usage. Perplexity had a
related but distinct accounting issue: it reports completion and
reasoning tokens as separate categories.

## Root Cause

Five provider-owned usage converters directly calculated completion
tokens minus reasoning tokens without a non-negative guard. The
Perplexity converter used the same subtraction even though Perplexity's
`completion_tokens` already represent generated text tokens and
`reasoning_tokens` are reported separately, causing negative text counts
and undercounted total output.

## Summary

- Clamp text output tokens at zero for OpenAI, Groq, Alibaba, Moonshot
AI, and DeepSeek when reported reasoning exceeds completion usage.
- Map Perplexity usage as `text = completion_tokens`, `reasoning =
reasoning_tokens`, and `total = completion_tokens + reasoning_tokens`.
- Update provider-specific regression tests, Perplexity generation and
streaming expectations, and the patch changeset for all six published
packages.

## Testing

- Perplexity Node tests: 45 passed.
- Perplexity Edge tests: 45 passed.
- Full workspace type-check passed.
- Full formatting and lint checks passed with zero warnings or errors.

## Related Issues

Fixes #19311

Closes #19312

---------

Co-authored-by: ai-sdk-factory <308175966+ai-sdk-factory@users.noreply.github.com>
Co-authored-by: 4ktLuffy <146297264+4ktLuffy@users.noreply.github.com>
Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com>
A
ai-sdk-factory[bot] committed
221425865dbae0b48ff5eb9572830d55aadc618f
Parent: 2cded86
Committed by GitHub <noreply@github.com> on 8/24/2026, 6:42:35 PM