litellm/tests/unit/proxy/spend_tracking
linhqm 768ea1b462
fix(proxy): size the budget hold for Gemini native generateContent bodies (#53412)
* fix(proxy): size the budget hold for Gemini native generateContent bodies

A Gemini contents body got no input-token count, so the pre-call budget hold fell back to the model's whole max_input_tokens window and an overlapping call on a budget-limited key got 422. The hold now counts text, function calls and declarations, responseSchema and inline audio by duration, reads the Gemini config output cap and candidateCount, and holds cachedContent at the cache-read rate. Images, PDF, video, fileData and server tools keep the whole-window hold.

* fix(proxy): hold cached Gemini context at the deployment cache-read rate

A model group behind a router has no cache_read_input_token_cost of its own, so the cachedContent hold fell back to the input rate and still exceeded small budgets. Read each deployment's cache-read rate and hold at the highest one

* fix(proxy): count every field of a Gemini part and hold inline audio at its audio price

A part that carries text beside a function call, a function response or inline
audio now counts each field. A body with inline audio holds its input at the
higher of the text and audio prices, counted at the audio rate of the Gemini
model routing serves the name from. A cachedContent name only changes the hold
on Gemini contents bodies, so chat and Responses holds stay as they were.

* fix(proxy): hold Gemini thought signatures, long-prompt rates and extra_body overrides

Count a returned thoughtSignature at its decoded size, since Gemini bills it as the earlier turn's thinking tokens

Price a Gemini contents prompt at the _above_<n>_tokens rates its size reaches, and hold a cache that may hold audio at the audio cache-read rate

Size the hold and the cancel floor from the body extra_body sends, and count a named cachedContent's window at the cache-read rate in the cancel floor too

* fix(proxy): read above-threshold Gemini prices from each deployment

A router model group's prices leave out the _above_<n>_tokens keys, so a cachedContent hold on a routed gemini-2.5-pro used the base cache-read rate. Read those keys from each deployment's prices too

* fix(proxy): hold uncounted Gemini prompts at base prices and read every config name

A Gemini body the proxy cannot count (media, file URIs, server tools) keeps
main's base prices for its whole-window hold. The hold reads the output cap,
candidate count and cachedContent from every config name the body sends and
takes the largest, and an extra_body config replaces one sent under any name.
The audio cache-read price is read from each deployment like the text one.

* fix(proxy): hold the larger Gemini body when extra_body may be dropped

A generateContent request with extra_body now holds the larger of the body
with extra_body merged in and the body without it. A Gemini deployment
merges it, while the completion adapter other providers use drops it, so a
smaller extra_body can no longer shrink the hold of a request the provider
gets in full.

* fix(proxy): keep the uncapped hold when a Gemini config name sets no output cap

* fix(proxy): keep a larger Gemini output cap when another config name sets none

* fix(proxy): count a Gemini response schema under any config name in the budget hold

* fix(proxy): count a Gemini body routed to a non-Gemini deployment as the chat request it becomes

Size the config fields too, so a large response schema is counted off the event loop.

* fix(proxy): let a Gemini body's deployments, not its counted name, pick the token count

A mixed group holds the larger of the native and chat counts, media the chat counter cannot size keeps the whole window on the adapter route, and an inline PDF adds the chat route's per-file tokens

* fix(proxy): keep an unsized Gemini count when the group's name points at another provider

A contents body whose context-aware count is unknown (a server tool such as urlContext) was recounted by the group's name alone. For a group named gpt-4o or claude-sonnet-5-5 that Gemini serves, that recount went through the completion adapter, dropped the tool, and held a few tokens instead of the whole window. The precomputed counts now keep an unknown count as None, and the estimators only recount a model that has no precomputed entry.

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-22 23:03:18 +00:00
..
native_budgets_golden fix(proxy): size the budget hold for Gemini native generateContent bodies (#53412) 2026-11-22 23:03:18 +00:00
__init__.py
native_budgets_replica.py feat(budgets): replace the python budget hold and settle engine with a rust engine admitted at auth (#51535) 2026-11-15 16:03:51 -08:00
test_background_interaction_settlement.py
test_baseline_accounting.py
test_budget_reservation.py fix(proxy): size the budget hold for Gemini native generateContent bodies (#53412) 2026-11-22 23:03:18 +00:00
test_budget_reservation_redis_failure.py feat(budgets): replace the python budget hold and settle engine with a rust engine admitted at auth (#51535) 2026-11-15 16:03:51 -08:00
test_budget_reservation_redis_failure_ledger.py feat(budgets): run the ledger's post-call spend counter step in rust (#52166) 2026-11-20 16:31:03 -08:00
test_budget_reservation_rust.py refactor(auth_cache): type auth cache keys and keep the cache always native (#51130) 2026-11-21 17:20:30 +09:00
test_budget_reservation_rust_ledger.py feat(budgets): run the ledger's post-call spend counter step in rust (#52166) 2026-11-20 16:31:03 -08:00
test_carried_budget_state.py fix(proxy): show committed spend in the remaining budget gauges (#49199) 2026-11-09 20:05:37 +00:00
test_cloudzero_endpoints.py
test_committed_spend.py fix(proxy): show committed spend in the remaining budget gauges (#49199) 2026-11-09 20:05:37 +00:00
test_compression_savings.py
test_cost_finalization.py feat(proxy): add a surcharge on customer requests (#52406) 2026-11-19 00:45:10 +08:00
test_customer_budget_alerts.py feat(proxy): add app groups with shared budgets and usage (#53232) 2026-11-21 12:50:58 -08:00
test_customer_cost_margin.py feat(proxy): let an app set its own surcharge on customer requests (#52492) 2026-11-18 19:56:06 +00:00
test_customer_window_accounting.py refactor(auth_cache): type auth cache keys and keep the cache always native (#51130) 2026-11-21 17:20:30 +09:00
test_customer_window_accounting_rust.py feat(budgets): replace the python budget hold and settle engine with a rust engine admitted at auth (#51535) 2026-11-15 16:03:51 -08:00
test_input_tokens.py fix(proxy): size the budget hold for Gemini native generateContent bodies (#53412) 2026-11-22 23:03:18 +00:00
test_key_metadata_recovery.py
test_native_budget_checks.py refactor(auth_cache): type auth cache keys and keep the cache always native (#51130) 2026-11-21 17:20:30 +09:00
test_native_budgets.py refactor(auth_cache): type auth cache keys and keep the cache always native (#51130) 2026-11-21 17:20:30 +09:00
test_native_budgets_ledger.py feat(budgets): run the ledger's post-call spend counter step in rust (#52166) 2026-11-20 16:31:03 -08:00
test_native_budgets_proxy_path.py feat(customer portal): set budgets per customer and gateway app (#52477) 2026-11-18 17:31:03 +00:00
test_native_budgets_proxy_path_ledger.py feat(budgets): run the ledger's post-call spend counter step in rust (#52166) 2026-11-20 16:31:03 -08:00
test_native_spend_log_row_type.py feat(spend-logs): rust spend log row builder (#52161) 2026-11-20 16:31:03 -08:00
test_performance_alerts.py feat(alerts): detect daily changes in recorded usage (#50666) 2026-11-11 18:23:02 -08:00
test_ptu_feature_flag.py
test_ptu_flat_cost_rollup.py refactor(router): move router callers onto the entry points and invert the proxy imports (#48847) 2026-10-29 17:26:33 +00:00
test_rust_spend_counters.py refactor(auth_cache): type auth cache keys and keep the cache always native (#51130) 2026-11-21 17:20:30 +09:00
test_savings.py fix(spend): keep a NaN or infinite cost from wedging daily spend writes (#48829) 2026-10-30 08:27:35 -07:00
test_search_api_logging.py
test_session_page.py fix(spend): read only the newest rows when paging sessions (LIT-8513) (#48886) 2026-11-03 10:51:09 -08:00
test_session_spend_total.py feat(auto_router): report a session's whole spend and name why savings stay unestimated (#51066) 2026-11-10 17:07:11 -08:00
test_spend_capture_rate.py
test_spend_counter_batch.py feat(budgets): replace the python budget hold and settle engine with a rust engine admitted at auth (#51535) 2026-11-15 16:03:51 -08:00
test_spend_counter_batch_ledger.py test(spend): assert ledger callback counts apart from wire command order in the remaining spend counter tests (#53471) 2026-11-21 23:37:10 +00:00
test_spend_event.py fix(spend): write a background response's settling read in-process when the spend collector is on (#50957) 2026-11-10 18:19:17 -08:00
test_spend_event_producer.py
test_spend_log_error_logger.py
test_spend_logs_ledger_setting.py feat(spend-logs): rust spend log callback (#52182) 2026-11-20 16:31:04 -08:00
test_spend_management_endpoints.py refactor(auth_cache): type auth cache keys and keep the cache always native (#51130) 2026-11-21 17:20:30 +09:00
test_spend_query_optimization.py fix(proxy): cancel the Admin UI's raw spend-log reads in Postgres after 30s (#49965) 2026-11-03 16:07:00 -08:00
test_spend_tracking_utils.py feat(proxy): add app groups with shared budgets and usage (#53232) 2026-11-21 12:50:58 -08:00
test_subscription_billing_periods.py feat(usage): show subscription-covered providers and account fees separately (#44772) 2026-10-30 03:58:36 -07:00