Skip to content

feat(@ai-sdk/google): preserve per-modality token details in usage data#14016

Merged
felixarntz merged 4 commits intomainfrom
google-modality-token-details
Apr 3, 2026
Merged

feat(@ai-sdk/google): preserve per-modality token details in usage data#14016
felixarntz merged 4 commits intomainfrom
google-modality-token-details

Conversation

@R-Taneja
Copy link
Copy Markdown
Contributor

@R-Taneja R-Taneja commented Apr 1, 2026

Summary

  • Add promptTokensDetails and candidatesTokensDetails to the Gemini response usageSchema so per-modality token counts (TEXT, IMAGE, AUDIO, VIDEO) are no longer stripped by Zod parsing
  • These fields now flow through to usage.raw, enabling downstream consumers to distinguish token usage by modality

Why

Gemini charges different rates for different input modalities (e.g. audio input is $0.50/1M tokens vs $0.25/1M for text/image/video). The ai-gateway needs per-modality token counts to bill correctly. Previously, promptTokensDetails was present in the Gemini API response but stripped during Zod schema validation, making it impossible to differentiate modalities downstream.

Validation

  • Ran the generate-text/google/image.ts example and confirmed promptTokensDetails now appears in usage.raw with both TEXT and IMAGE modality entries
  • All 328 existing tests pass; 10 snapshots updated to include the new fields

Add `promptTokensDetails` and `candidatesTokensDetails` to the Gemini
response usage schema so per-modality token counts (text, image, audio,
video) flow through to `usage.raw` instead of being stripped by Zod.

This enables downstream consumers like ai-gateway to bill different
input modalities at their correct rates (e.g. audio at $0.50/1M vs
text/image/video at $0.25/1M).
@tigent tigent bot added ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label feature New feature or request provider/google Issues related to the @ai-sdk/google provider labels Apr 1, 2026
@R-Taneja R-Taneja requested a review from shaper April 1, 2026 19:10
@R-Taneja R-Taneja marked this pull request as ready for review April 1, 2026 19:10
Copy link
Copy Markdown
Collaborator

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@R-Taneja Awesome, LGTM. Added 2 examples for verification.

@felixarntz felixarntz added the backport Admins only: add this label to a pull request in order to backport it to the prior version label Apr 3, 2026
@felixarntz felixarntz enabled auto-merge (squash) April 3, 2026 16:06
@felixarntz felixarntz merged commit a05109d into main Apr 3, 2026
20 checks passed
@felixarntz felixarntz deleted the google-modality-token-details branch April 3, 2026 16:07
vercel-ai-sdk bot pushed a commit that referenced this pull request Apr 3, 2026
…ta (#14016)

## Summary
- Add `promptTokensDetails` and `candidatesTokensDetails` to the Gemini
response `usageSchema` so per-modality token counts (TEXT, IMAGE, AUDIO,
VIDEO) are no longer stripped by Zod parsing
- These fields now flow through to `usage.raw`, enabling downstream
consumers to distinguish token usage by modality

## Why
Gemini charges different rates for different input modalities (e.g.
audio input is $0.50/1M tokens vs $0.25/1M for text/image/video). The
ai-gateway needs per-modality token counts to bill correctly.
Previously, `promptTokensDetails` was present in the Gemini API response
but stripped during Zod schema validation, making it impossible to
differentiate modalities downstream.

## Validation
- Ran the `generate-text/google/image.ts` example and confirmed
`promptTokensDetails` now appears in `usage.raw` with both `TEXT` and
`IMAGE` modality entries
- All 328 existing tests pass; 10 snapshots updated to include the new
fields

---------

Co-authored-by: Felix Arntz <felix.arntz@vercel.com>
@vercel-ai-sdk vercel-ai-sdk bot removed the backport Admins only: add this label to a pull request in order to backport it to the prior version label Apr 3, 2026
@vercel-ai-sdk
Copy link
Copy Markdown
Contributor

vercel-ai-sdk bot commented Apr 3, 2026

✅ Backport PR created: #14110

vercel-ai-sdk bot added a commit that referenced this pull request Apr 3, 2026
…n usage data (#14110)

This is an automated backport of #14016 to the release-v6.0 branch. FYI
@R-Taneja

Co-authored-by: Rohan Taneja <47066511+R-Taneja@users.noreply.github.com>
Co-authored-by: Felix Arntz <felix.arntz@vercel.com>
@vercel-ai-sdk
Copy link
Copy Markdown
Contributor

vercel-ai-sdk bot commented Apr 3, 2026

🚀 Published in:

Package Version
@ai-sdk/google 4.0.0-beta.26
@ai-sdk/google-vertex 5.0.0-beta.34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label feature New feature or request provider/google Issues related to the @ai-sdk/google provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants