Skip to content

fix(mistral): map cached token counts from Mistral usage response - #14889

Merged
lgrammel merged 2 commits into
vercel:mainfrom
Christian-Sidak:fix/mistral-cached-tokens-signed-v2
May 4, 2026
Merged

lgrammel merged 2 commits into
vercel:mainfrom
Christian-Sidak:fix/mistral-cached-tokens-signed-v2

Conversation

@Christian-Sidak

@Christian-Sidak Christian-Sidak commented May 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Mistral's API returns cached token counts via num_cached_tokens, prompt_tokens_details.cached_tokens, and prompt_token_details.cached_tokens
  • These fields were not included in mistralUsageSchema (stripped by Zod) and not mapped in convertMistralUsage
  • All prompt tokens were incorrectly reported as noCacheTokens
  • Adds the fields to the schema and computes cacheRead/noCache correctly

Test plan

  • Unit test with cached token fields in usage response
  • Verify noCache = promptTokens - cacheReadTokens
  • Verify cacheRead is populated when cached tokens present
  • Verify backward compatibility when cached fields are absent

Related Issues

Closes #14382
Supersedes #14419

Mistral API returns cached token counts via num_cached_tokens,
prompt_tokens_details.cached_tokens, and prompt_token_details.cached_tokens.
These fields were stripped by the Zod schema and never mapped in
convertMistralUsage, causing all prompt tokens to be reported as
noCacheTokens. This adds the fields to the schema and computes
cacheRead/noCache correctly.

Closes vercel#14382
@lgrammel lgrammel added the backport Admins only: add this label to a pull request in order to backport it to the prior version label May 4, 2026
@lgrammel
lgrammel merged commit 8abd80f into vercel:main May 4, 2026
21 of 22 checks passed
github-actions Bot added a commit that referenced this pull request May 4, 2026
…4889)

## Summary
- Mistral's API returns cached token counts via `num_cached_tokens`,
`prompt_tokens_details.cached_tokens`, and
`prompt_token_details.cached_tokens`
- These fields were not included in `mistralUsageSchema` (stripped by
Zod) and not mapped in `convertMistralUsage`
- All prompt tokens were incorrectly reported as `noCacheTokens`
- Adds the fields to the schema and computes `cacheRead`/`noCache`
correctly

## Test plan
- [x] Unit test with cached token fields in usage response
- [x] Verify noCache = promptTokens - cacheReadTokens
- [x] Verify cacheRead is populated when cached tokens present
- [x] Verify backward compatibility when cached fields are absent

## Related Issues
Closes #14382
Supersedes #14419

Co-authored-by: Lars Grammel <[email protected]>
@github-actions

github-actions Bot commented May 4, 2026

Copy link
Copy Markdown
Contributor

✅ Backport PR created: #14947

@github-actions github-actions Bot removed the backport Admins only: add this label to a pull request in order to backport it to the prior version label May 4, 2026
gr2m pushed a commit that referenced this pull request May 4, 2026
…sponse (#14947)

This is an automated backport of #14889 to the release-v6.0 branch. FYI
@Christian-Sidak

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Lars Grammel <[email protected]>
gr2m pushed a commit that referenced this pull request Jun 5, 2026
…4889)

## Summary
- Mistral's API returns cached token counts via `num_cached_tokens`,
`prompt_tokens_details.cached_tokens`, and
`prompt_token_details.cached_tokens`
- These fields were not included in `mistralUsageSchema` (stripped by
Zod) and not mapped in `convertMistralUsage`
- All prompt tokens were incorrectly reported as `noCacheTokens`
- Adds the fields to the schema and computes `cacheRead`/`noCache`
correctly

## Test plan
- [x] Unit test with cached token fields in usage response
- [x] Verify noCache = promptTokens - cacheReadTokens
- [x] Verify cacheRead is populated when cached tokens present
- [x] Verify backward compatibility when cached fields are absent

## Related Issues
Closes #14382
Supersedes #14419

Co-authored-by: Lars Grammel <[email protected]>
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.

[Bug] Mistral provider: all tokens reported as noCacheTokens

2 participants