Repository navigation
fix(vertex_ai): bill per-character text embeddings on Google's billableCharacterCount - #47460
Conversation
…leCharacterCount Vertex text embedding predict responses report metadata.billableCharacterCount, the unit Google bills text-embedding-005 and text-multilingual-embedding-002 on. Carry it into usage.prompt_tokens_details.character_count and, when the model carries input_cost_per_character, bill that count instead of the token count so the prompt is charged under one unit. Per-token embeddings (gemini-embedding-001) are unchanged.
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
|
|
@litellm-stampbot |
litellm-stampbot: risk highNot approved at Rules (
Cursor Bugbot: Medium Risk at Pushes do not re-run this check. Comment |
|
bugbot run |
There was a problem hiding this comment.
✅ 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 fe38060. Configure here.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…leCharacterCount (BerriAI#47460) Backport of BerriAI#47460 to stable/1.103.x Cherry-picked from d29200f (main)
…leCharacterCount (BerriAI#47460) Vertex text embedding predict responses report metadata.billableCharacterCount, the unit Google bills text-embedding-005 and text-multilingual-embedding-002 on. Carry it into usage.prompt_tokens_details.character_count and, when the model carries input_cost_per_character, bill that count instead of the token count so the prompt is charged under one unit. Per-token embeddings (gemini-embedding-001) are unchanged. Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com> Backport of BerriAI#47460 to stable/1.106.x Cherry-picked from d29200f (main)
…leCharacterCount (#47460) Vertex text embedding predict responses report metadata.billableCharacterCount, the unit Google bills text-embedding-005 and text-multilingual-embedding-002 on. Carry it into usage.prompt_tokens_details.character_count and, when the model carries input_cost_per_character, bill that count instead of the token count so the prompt is charged under one unit. Per-token embeddings (gemini-embedding-001) are unchanged. Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com> (cherry picked from commit d29200f)
…leCharacterCount (BerriAI#47460) Vertex text embedding predict responses report metadata.billableCharacterCount, the unit Google bills text-embedding-005 and text-multilingual-embedding-002 on. Carry it into usage.prompt_tokens_details.character_count and, when the model carries input_cost_per_character, bill that count instead of the token count so the prompt is charged under one unit. Per-token embeddings (gemini-embedding-001) are unchanged. Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com> Backport of BerriAI#47460 to stable/1.105.x. Cherry-picked from d29200f (main).
TLDR
Problem this solves:
text-embedding-005andtext-multilingual-embedding-002per character, LiteLLM charged per tokeninput_cost_per_character: 2.5e-08on both keys, the embedding path never read itmetadata.billableCharacterCountin the Vertex predict response was droppedHow it solves it:
billableCharacterCountasprompt_tokens_details.character_countinput_cost_per_characterbills that count instead of its text tokens, never bothgemini-embedding-001, any key withoutinput_cost_per_character) bill as beforeIntentional product change: every Vertex text embedding response now has
usage.prompt_tokens_details.character_count(per-token models included). Nothing is removedRelated: #47069 covers the same issue from an OSS contributor. Nothing was taken from that branch. It stashes the count in a hidden param read from a
ModelResponsebranch an embedding never takes, and it reads the count from per-predictionstatisticswhere Vertex does not put it (the live response below has it under top-levelmetadata). This PR reuses the existingcharacter_countusage field and the character branchgeneric_cost_per_tokenalready hasUser Flow
Before: a platform team charging Vertex embedding spend back to internal teams sees LiteLLM log more than Google invoices
"model": "vertex_ai/text-embedding-005"and a 43 character sentenceusage.prompt_tokens: 11and the headerx-litellm-response-cost: 1.1e-06After: the same request is logged at Google's per-character price
usage.prompt_tokens: 11,usage.prompt_tokens_details.character_count: 35andx-litellm-response-cost: 8.75e-07vertex_ai/gemini-embedding-001request still shows its per-token priceRelevant issues
Fixes section 2 of #46880
Linear ticket
Resolves LIT-9892
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or moreScreenshots / Proof of Fix
Live proxy on localhost:4000 with Postgres, real Vertex AI calls. Config:
The input is
The quick brown fox jumps over the lazy dog, 43 characters, which Vertex reports as"metadata": {"billableCharacterCount": 35}(whitespace is not billed) and 11 tokens, so the two prices differ: 11 x 1e-07 = 1.1e-06 per token, 35 x 2.5e-08 = 8.75e-07 per character. Both calls were made withBefore (497f31d)
text-embedding-005 is billed per token
curl ... vertex_ai/text-embedding-005gemini-embedding-001 is billed per token
curl ... vertex_ai/gemini-embedding-001SpendLogs rows
psql ... LIMIT 2After (fe38060)
text-embedding-005 is billed per character
curl ... vertex_ai/text-embedding-005gemini-embedding-001 is billed per token
curl ... vertex_ai/gemini-embedding-001SpendLogs rows
psql ... LIMIT 2Regression tests
tests/unit/llms/vertex_ai/vertex_embeddings/test_embedding_handler.py::test_vertex_embedding_usage_carries_the_billable_character_count_google_prices_onandtests/unit/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py::test_generic_cost_per_token_bills_a_character_counted_prompt_under_one_unit. On the merge base the character cases fail ((8, None) == (8, 10)and1.975e-06 != 8.75e-07, the token-plus-character double charge) and the no-metadata and token-priced cases pass, which is the "per-token embeddings unchanged" proof. All six pass on the fixType
🐛 Bug Fix
Caveats (if any)
Low
usageMetadata, noembeddingobject,native_vertex_batch_row_statsreturns None for them), so noinput_cost_per_character_batchesfield is added. Adding batch character pricing is a separate design decisionusage.prompt_tokens_details.character_countis now set on per-token Vertex embedding responses too (see the gemini-embedding-001 After output). It is ignored for cost thereFinal Attestation
Link to Devin session: https://app.devin.ai/sessions/3192b8ae836340568c04a74ffcd6816c
Open in Devin Desktop: https://app.devin.ai/desktop/session/3192b8ae836340568c04a74ffcd6816c?variant=devin
Requested by: @kerrylu-berri
Note
Medium Risk
Changes billing math for Vertex character-priced embedding models and alters reported usage metadata; per-token embedding pricing is intended to stay the same but spend/logging will shift for affected models.
Overview
Fixes Vertex per-character text embeddings (
text-embedding-005, etc.) being overcharged because LiteLLM billed tokens while Google billsmetadata.billableCharacterCount.Vertex embedding responses now surface that count on
usage.prompt_tokens_details.character_count. In_calculate_input_cost, models withinput_cost_per_characterbill only the character count (text-token base cost is skipped so tokens and characters are never charged together). Per-token embedding models keep token pricing;character_countmay still appear on usage but is ignored for cost.Unit tests cover the cost-calc branch and the Vertex usage transform.
Reviewed by Cursor Bugbot for commit fe38060. Bugbot is set up for automated code reviews on this repo. Configure here.