Skip to content

fix(cost): avoid double billing Gemini embedding modalities - #41151

Closed
tin-berri wants to merge 1 commit into
mainfrom
litellm_vertex_embedding_audio_cost
Closed

tin-berri wants to merge 1 commit into
mainfrom
litellm_vertex_embedding_audio_cost

Conversation

@tin-berri

@tin-berri tin-berri commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • Gemini embedding audio costs include both tokens and seconds
  • Vertex unit tests fail after the pricing sync

How it solves it:

  • Charge each modality once, preferring explicit token prices
  • Preserve image/video tokens and align published online rates

User Flow

Before: 64 audio tokens representing two seconds cost $0.000736

  1. POST http://localhost:45202/spend/calculate with the embedding usage below
  2. Receive HTTP 200 with {"cost":0.000736}

After: the same usage costs $0.000416 at Google's token rate

  1. POST http://localhost:45202/spend/calculate with the embedding usage below
  2. Receive HTTP 200 with {"cost":0.000416}

Relevant issues

Affected release

Linear ticket

Changes

  • Select one token or physical-unit charge per modality
  • Retain image/video token counts without inventing a text token
  • Apply published online rates across six stable/preview aliases and synchronize the pricing schema
  • Reuse existing embedding tests and add one seven-case pricing matrix

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally: 390 passed
  • My PR passes all required CI/CD checks
  • My PR's scope is as isolated as possible; it only solves one specific problem
  • Greptile 5/5 on c698972; Bugbot and Veria passed

Screenshots / Proof of Fix

Real local proxy and Postgres, using /spend/calculate with example usage and each commit's bundled prices

Save as audio-request.json:

{"completion_response":{"model":"vertex_ai/gemini-embedding-2","usage":{"prompt_tokens":64,"completion_tokens":0,"total_tokens":64,"prompt_tokens_details":{"text_tokens":0,"audio_tokens":64,"audio_length_seconds":2}}}}

Before (d1fc231)

  1. Send the request

    curl -sS http://localhost:45202/spend/calculate \
      -H "Authorization: Bearer $LITELLM_MASTER_KEY" \
      -H 'Content-Type: application/json' --data @audio-request.json
  2. HTTP 200: {"cost":0.000736}

After (c698972)

  1. Send the same request

    curl -sS http://localhost:45202/spend/calculate \
      -H "Authorization: Bearer $LITELLM_MASTER_KEY" \
      -H 'Content-Type: application/json' --data @audio-request.json
  2. HTTP 200: {"cost":0.000416}

Type

Bug Fix

Caveats

Low

  • Paid Vertex inference unverified: Google credentials unavailable locally

Final Attestation

  • Tests cover mixed units, zero rates, caching, and fallback pricing

@tin-berri
tin-berri requested a review from a team September 15, 2026 00:24
@codspeed

codspeed Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_vertex_embedding_audio_cost (c698972) with main (b67137b)

Open in CodSpeed

@greptile-apps

greptile-apps Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR prevents multimodal embedding inputs from being charged using both token and physical-unit rates.

  • Selects one pricing basis per audio, image, and video modality, preferring explicitly configured token rates.
  • Preserves Gemini embedding image and video token details without synthesizing text usage.
  • Updates Gemini Embedding 2 pricing aliases and keeps the generated pricing schema synchronized.
  • Adds regression coverage for mixed pricing units, zero rates, cached audio, modality usage conversion, and provider/model aliases.

Confidence Score: 5/5

The PR appears safe to merge; the current commit keeps the generated schema synchronized and the single-charge modality behavior is covered across relevant fallback and zero-rate cases.

No actionable new failures remain in the changes since the previous review, and no mounted repository-rule violations were identified.

Important Files Changed

Filename Overview
litellm/litellm_core_utils/llm_cost_calc/utils.py Introduces mutually exclusive token-or-physical-unit modality charging while preserving explicit zero rates and generic token fallback.
litellm/llms/vertex_ai/gemini_embeddings/batch_embed_content_transformation.py Preserves image and video token metadata and removes the artificial text-token floor.
model_prices_and_context_window.json Adds published modality token rates across the intended Gemini Embedding 2 aliases.
model_prices_and_context_window.schema.json Adds the generated nonnegative-number schema property for video token pricing, consistent with the pricing source.
tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py Adds a pricing matrix covering token preference, unit fallback, explicit zero rates, generic fallback, and cached audio.
tests/test_litellm/llms/vertex_ai/gemini_embeddings/test_batch_embed_content_transformation.py Expands modality usage and exact-cost regressions across six Gemini and Vertex model/provider combinations.

Reviews (2): Last reviewed commit: "fix(cost): avoid double billing Gemini e..." | Re-trigger Greptile

@tin-berri
tin-berri force-pushed the litellm_vertex_embedding_audio_cost branch from 8fb0328 to c698972 Compare September 15, 2026 00:27
@tin-berri

Copy link
Copy Markdown
Contributor Author

@greptileai Please review the current commit c698972, including the generated pricing schema and the single-charge modality regressions

@tin-berri

Copy link
Copy Markdown
Contributor Author

bugbot run

Please review c698972 for pricing correctness, fallback behavior, cached audio accounting, and the generated schema update

@codecov

codecov Bot commented Sep 15, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit c698972. Configure here.

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.

1 participant