litellm/tests/unit/proxy/spend_tracking
yucheng-berri a50594c30f
fix(proxy): write a cold spend counter's cached spend once (#52806)
* fix(proxy): seed a cold end-user spend counter once from the end-user row

A cold spend:end_user counter was seeded by adding the cached end-user
spend, so requests that overlapped on the cold counter added it once each
and a customer under budget was refused. The write path now seeds it with
SET NX under the per-counter lock, from LiteLLM_EndUserTable and never
below the cached spend, the way key, team, user, org and project counters
seed. Reads of a cold counter are unchanged.

Fixes #47062

* fix(proxy): write a cold counter's cached spend with SET NX, drop the row read

The cold seed goes back to the cached spend main already used, written with
SET NX in place of INCRBYFLOAT, so requests and proxies that overlap on a
cold counter write it once. This drops the LiteLLM_EndUserTable read from
the first commit: the row could already hold the request's own charge, and
the post-call increment would then add it a second time.

* fix(proxy): raise a cold counter to the cached spend, never past it

The cold seed now uses the store's set_max, the write the stale-counter
repair already uses. A charge that creates the counter just before the seed
no longer makes the seed skip the cached spend, and two proxies with
different cached spends leave the larger one. The new tests read the cached
spend through a real UserApiKeyCache.

* fix(proxy): seed a cold counter with an increment when Redis refuses the script

* test: keep main's import block in the spend counter tests

* fix(budgets): raise a cold counter to the cached spend in the rust post-call step

The post-call step that main now runs in Rust added the auth cache's spend to a
cold counter with INCRBYFLOAT, so requests that overlap on a cold counter with
budget reservation off still multiplied it. Raise it with set_max as the Python
seed does, and drop this process's copy when a seed fails.

* fix(budgets): drop a cold counter everywhere when its seed may have applied

A seed whose reply is lost may have written the cached spend to Redis.
A failed increment already drops the counter from Redis and memory, and
the set_max seed now does the same in the Python and Rust paths.

* test(budgets): key the cold seed tests by AuthObject after the main merge

* fix(budgets): end the cold seed with the charge's own increment

On Redis Cluster a slot mid-migration answers the set_max script through
an ASK redirect but refuses the pipelined INCRBYFLOAT that charges the
counter. The seed then succeeded, the charge pipeline failed, and every
counter in that step was dropped. The seed now ends with INCRBYFLOAT 0,
so a counter the charge cannot reach fails in the seed and is dropped on
its own, as it was when the seed was that increment.

* test(budgets): mark the new charge tests with rstest

---------

Co-authored-by: kenneth <kenneth@berri.ai>
2026-11-24 13:00:34 -08: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 ci: move tests/proxy_unit_tests to tests/unit/proxy and run the proxy-db shards from litellm-tests (#42903) 2026-09-24 22:59:11 +00:00
native_budgets_replica.py fix(proxy): write a cold spend counter's cached spend once (#52806) 2026-11-24 13:00:34 -08:00
test_background_interaction_settlement.py fix(interactions): keep background interaction bills through crashes, late restarts, and deployment keys (#45743) 2026-10-16 12:00:08 +00:00
test_baseline_accounting.py fix(router): preserve native baseline identity and accounting (#44960) 2026-10-07 11:00:31 -07:00
test_budget_reservation.py feat(cost): price Claude token calls, budget holds and auth price checks in Rust (#52484) 2026-11-23 22:00:28 +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(proxy): move middleware, spend_tracking, pass_through, common_utils and root proxy tests into tests/unit/proxy (#44015) 2026-10-01 18:23:31 +00:00
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(proxy): move middleware, spend_tracking, pass_through, common_utils and root proxy tests into tests/unit/proxy (#44015) 2026-10-01 18:23:31 +00:00
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(cost): price Claude token calls, budget holds and auth price checks in Rust (#52484) 2026-11-23 22:00:28 +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 fix: cap cold MCP schema workers at usable CPUs and deflake wall-clock unit tests (#45791) 2026-10-15 00:47:59 -07:00
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 feat(proxy): split Azure PTU deployments across teams with per-team PTU ceilings and PTU-hour reporting (#43043) 2026-10-15 18:44:30 +00:00
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 refactor(proxy): expose public names for private proxy helpers (#45170) 2026-10-07 18:59:27 -07:00
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(proxy): move middleware, spend_tracking, pass_through, common_utils and root proxy tests into tests/unit/proxy (#44015) 2026-10-01 18:23:31 +00:00
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 fix(proxy): write a cold spend counter's cached spend once (#52806) 2026-11-24 13:00:34 -08: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(unit): keep unix socket paths in unit tests under the macOS AF_UNIX limit (#45040) 2026-10-28 20:23:22 -07:00
test_spend_log_error_logger.py test(proxy): move middleware, spend_tracking, pass_through, common_utils and root proxy tests into tests/unit/proxy (#44015) 2026-10-01 18:23:31 +00:00
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 feat(responses): store each bridged turn's path to the root so a fork replays only its ancestors (#49519) 2026-11-23 12:43:11 -08: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 fix(anthropic): log the safeguards classifier and compaction summary calls in the parent request's session (#53405) 2026-11-24 01:31:04 -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