Skip to content

Conversation

@Chesars
Copy link
Contributor

@Chesars Chesars commented Dec 17, 2025

Relevant issues

Fixes #14285

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Type

🐛 Bug Fix

Changes

When sending images as input to Gemini, LiteLLM was not counting IMAGE tokens for cost calculation. The API returns promptTokensDetails with separate modalities:

{
  "promptTokenCount": 262,
  "promptTokensDetails": [
    {"modality": "TEXT", "tokenCount": 4},
    {"modality": "IMAGE", "tokenCount": 258}
  ]
}

LiteLLM only counted TEXT tokens (4), ignoring IMAGE tokens (258), resulting in ~98% undercharged costs for image inputs.

Example

response = litellm.completion(
    model="gemini/gemini-2.0-flash",
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "What is this?"},
            {"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}}
        ]
    }]
)
# Before fix: text_tokens=4, cost based on 4 tokens
# After fix: text_tokens=262, cost based on 262 tokens

Solution

According to Google's Gemini API pricing, image input tokens are priced the same as text tokens for all Gemini models:

Input price: $0.30 (text / image / video)

The fix adds IMAGE tokens to text_tokens in _calculate_usage():

elif detail["modality"] in ("TEXT", "IMAGE"):
    # Image tokens are priced the same as text tokens for Gemini
    text_tokens = (text_tokens or 0) + detail.get("tokenCount", 0)

Tests Added

  • test_vertex_ai_usage_metadata_with_image_input_tokens - IMAGE tokens in promptTokensDetails
  • test_vertex_ai_usage_metadata_with_image_input_tokens_reverse_order - Order independence

When sending images as input to Gemini, the API returns promptTokensDetails
with both TEXT and IMAGE modalities. Previously, only TEXT tokens were counted
for cost calculation, resulting in significantly undercharged costs.

Image tokens are priced the same as text tokens for all Gemini models
(https://ai.google.dev/gemini-api/docs/pricing), so this fix adds IMAGE
tokens to text_tokens for correct cost calculation.

Fixes BerriAI#14285
@vercel
Copy link

vercel bot commented Dec 17, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
litellm Ready Ready Preview, Comment Dec 17, 2025 11:26pm

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Missing Gemini IMAGE tokens in cost calculation

1 participant