fix(gemini): include DOCUMENT modality tokens in cost calculation - #24410
Merged
Chesars merged 1 commit intoMar 23, 2026
Merged
Conversation
Gemini API returns a DOCUMENT modality in promptTokensDetails for PDF inputs, but the token parser only handled TEXT, IMAGE, AUDIO, and VIDEO. DOCUMENT tokens were silently dropped, causing cost to be undercounted by up to 99% for PDF-heavy requests. Map DOCUMENT tokens to text_tokens since Gemini bills documents at the text token rate. Applied to all four modality parser loops: promptTokensDetails, cacheTokensDetails, responseTokensDetails, and candidatesTokensDetails. Fixes BerriAI#24375
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
Greptile SummaryThis PR fixes a cost undercounting bug for Gemini models when PDFs are included in prompts. The Gemini API returns a Key changes:
Confidence Score: 5/5
|
| Filename | Overview |
|---|---|
| litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py | Adds DOCUMENT modality handling to all four token-detail parsing loops (responseTokensDetails, candidatesTokensDetails, promptTokensDetails, cacheTokensDetails), mapping DOCUMENT tokens to text_tokens to match Gemini's billing semantics. Fix is minimal, consistent, and correctly placed. |
| tests/test_litellm/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py | Adds two new unit tests: one verifying DOCUMENT tokens accumulate into text_tokens from promptTokensDetails, and one verifying cached DOCUMENT tokens are correctly subtracted. Both tests are purely mock-based with no network calls. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Gemini API Response\nusageMetadata] --> B{responseTokensDetails\npresent?}
B -- Yes --> C[Loop over details]
C --> D{modality?}
D -- TEXT --> E[text_tokens += count]
D -- AUDIO --> F[audio_tokens += count]
D -- DOCUMENT\n✅ NEW --> E
A --> G{candidatesTokensDetails\npresent?}
G -- Yes --> H[Loop over details]
H --> I{modality?}
I -- TEXT --> J[text_tokens += count]
I -- AUDIO --> K[audio_tokens += count]
I -- IMAGE --> L[image_tokens += count]
I -- VIDEO --> M[video_tokens += count]
I -- DOCUMENT\n✅ NEW --> J
A --> N{promptTokensDetails\npresent?}
N -- Yes --> O[Loop over details]
O --> P{modality?}
P -- TEXT --> Q[prompt_text_tokens += count]
P -- AUDIO --> R[prompt_audio_tokens += count]
P -- IMAGE --> S[prompt_image_tokens += count]
P -- VIDEO --> T[prompt_video_tokens += count]
P -- DOCUMENT\n✅ NEW --> Q
A --> U{cacheTokensDetails\npresent?}
U -- Yes --> V[Loop over details]
V --> W{modality?}
W -- TEXT --> X[cached_text_tokens += count]
W -- AUDIO --> Y[cached_audio_tokens += count]
W -- IMAGE --> Z[cached_image_tokens += count]
W -- VIDEO --> AA[cached_video_tokens += count]
W -- DOCUMENT\n✅ NEW --> X
Q --> AB[Subtract cached\nfrom prompt tokens]
X --> AB
AB --> AC[Build PromptTokensDetailsWrapper]
J --> AD[Build CompletionTokensDetailsWrapper]
AC --> AE[Usage object\nwith correct costs]
AD --> AE
Reviews (1): Last reviewed commit: "fix(gemini): include DOCUMENT modality t..." | Re-trigger Greptile
Contributor
Chesars
merged commit Mar 23, 2026
4399b76
into
BerriAI:litellm_staging_03_23_2026
38 of 39 checks passed
fzowl
pushed a commit
to fzowl/litellm
that referenced
this pull request
Jun 24, 2026
…ality-cost fix(gemini): include DOCUMENT modality tokens in cost calculation
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.
Relevant issues
Fixes #24375
Pre-Submission checklist
tests/test_litellm/directory, Adding at least 1 test is a hard requirement - see detailsmake test-unit@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer reviewType
🐛 Bug Fix
Changes
When a PDF is sent to a Gemini model, the API returns a
DOCUMENTmodality inpromptTokensDetails(e.g.,{modality: "DOCUMENT", tokenCount: 774}). The Gemini response parser only handledTEXT,IMAGE,AUDIO, andVIDEOmodalities —DOCUMENTwas silently dropped.This caused
prompt_tokens_details.text_tokensto report only the text portion (e.g., 9 of 783 tokens), and cost calculation used that incomplete breakdown, undercounting spend by up to 99% for PDF-heavy requests.Fix
Map
DOCUMENTmodality tokens totext_tokens(Gemini bills documents at the text token rate). Applied to all four modality parser loops invertex_and_google_ai_studio_gemini.py:promptTokensDetails→prompt_text_tokenscacheTokensDetails→cached_text_tokensresponseTokensDetails→response_tokens_details.text_tokenscandidatesTokensDetails→response_tokens_details.text_tokensTests added
test_vertex_ai_usage_metadata_with_document_tokens_in_prompt— verifies DOCUMENT tokens are accumulated intotext_tokenstest_vertex_ai_usage_metadata_with_document_tokens_cached— verifies cached DOCUMENT tokens are correctly subtracted