Skip to content

fix(cost): apply Vertex regional uplift to batch jobs and bill per-character embeddings per character - #47069

Closed
Mrun25 wants to merge 6 commits into
BerriAI:mainfrom
Mrun25:fix/vertex-embedding-batch-cost-46880
Closed

Mrun25 wants to merge 6 commits into
BerriAI:mainfrom
Mrun25:fix/vertex-embedding-batch-cost-46880

Conversation

@Mrun25

@Mrun25 Mrun25 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • Vertex batch jobs on regional endpoints are billed without the 10% regional premium
  • text-embedding-005 and text-multilingual-embedding-002 are billed per token; Google bills them per character

How it solves it:

  • Threads vertex_location through batch cost accounting and applies the same regional-endpoint uplift the online path uses
  • Routes per-character-priced Vertex embeddings to character billing, using billableCharacterCount from the predict response

User Flow

Before: a team running Gemini batch jobs on a regional Vertex endpoint logs spend lower than their Google invoice

  1. They create a batch with vertex_location: us-central1 with 1M input and 1M output tokens on Gemini 3.5 Flash
  2. The batch completes and they open https://litellm-domain/ui/?page=logs
  3. The job is logged at $5.25, while Google's published non-global batch price is $5.775

After: the same batch logs the regional price

  1. They create the same batch with vertex_location: us-central1
  2. The batch completes and they open https://litellm-domain/ui/?page=logs
  3. The job is logged at $5.775, matching Google's non-global pricing

Relevant issues

Fixes #46880

This PR covers the two code fixes from the issue. The price-data JSON updates mentioned there are left to the maintainers, who own the pricing map.

Pre-Submission checklist

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

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. 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 more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Screenshots / Proof of Fix

Setup: LITELLM_LOCAL_MODEL_COST_MAP=True, batch_cost_calculator / cost_per_token called the same way the batch and embedding paths call them.

Before (d5ce7db)

Regional Vertex batch billed without the uplift

  1. Call batch_cost_calculator for a Gemini 3.5 Flash batch with 1M input and 1M output tokens that ran on us-central1
  2. Output: prompt=$0.7500 completion=$4.5000 total=$5.2500 — the global rate, no way to pass the job's location

Per-character embedding billed per token

  1. Call cost_per_token for a text-embedding-005 embedding of 1,000,000 characters (250,000 tokens)
  2. Output: prompt=$0.025000 from token pricing; the character count the predict API returned is dropped

After (f1b6610)

Regional Vertex batch billed without the uplift

  1. Same call, now with vertex_location="us-central1" threaded from the batch job
  2. Output: global stays total=$5.2500; with us-central1 it is total=$5.7750 — the published non-global rate

Per-character embedding billed per token

  1. Same call; the response transformation now carries the summed billableCharacterCount through
  2. Output: prompt=$0.025000 from character pricing ($2.5e-08 x 1,000,000), and gemini-embedding-001 is unchanged at prompt=$0.037500 per token

Type

🐛 Bug Fix

Caveats (if any)

Medium

  • Per-line batch spend breakdowns do not know the job's location, so line items show pre-uplift rates; the batch total is correct

Low

  • An embedding row without billableCharacterCount in the predict response keeps per-token billing, same as before

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

…aracter embeddings per character

- thread vertex_location through batch cost accounting so non-global
  batch jobs get the same regional-endpoint uplift as the online path
- route per-character-priced Vertex embeddings (text-embedding-005,
  text-multilingual-embedding-002) to character billing, using the
  billableCharacterCount reported by the predict response
- per-token embeddings (gemini-embedding-*) keep token pricing

Fixes BerriAI#46880
@codspeed

codspeed Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

⚠️ 1 benchmark spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 32 untouched benchmarks


Comparing Mrun25:fix/vertex-embedding-batch-cost-46880 (699f8ad) with main (a4e0570)

Open in CodSpeed

@codecov

codecov Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.75000% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
litellm/cost_calculator.py 75.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

The repo-wide type gate counts Unknown-type diagnostics: reading the
stashed billable count through getattr on the partially-typed
completion_response added one reportUnknownArgumentType, and the new
billable-count read in the embedding transformation added
reportUnknownMemberType/reportUnknownVariableType entries. Narrow the
response with isinstance (the convention the next block already uses),
read the count through the typed get_hidden_params helper, and cast the
Vertex statistics dict instead of annotating it - explicit Any is not
allowed here.
The strict-imports gate bans typing.cast imports without a reason;
annotate the import with the payload's justification.
@Mrun25
Mrun25 force-pushed the fix/vertex-embedding-batch-cost-46880 branch from 4bf2eb7 to 23413eb Compare October 17, 2026 06:23
@mateo-berri

Copy link
Copy Markdown
Contributor

Closing this since #47460 and #47466 already fix these problems on main. Thanks for contributing, and please reopen if anything is missing

@mateo-berri mateo-berri closed this Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants