litellm/tests/unit/proxy/common_utils
joshua-berri a12a6cd154
feat(accounting)!: make the SpendLogs ledger the default accounting path (#49872)
* fix(accounting): keep one undecodable delivery from blocking the ledger

A request whose metadata carried a 401-digit tokens_saved stored that number in
its delivery payload. The query engine could not decode it when the worker
claimed the batch, so every later delivery on the proxy stayed pending

The worker now claims the payload as text and decodes each row in Python. A row
that still cannot be decoded is parked alone while the rest of the batch
applies. The payload builder also clamps integers to the JSON-safe range, so
caller-supplied counts can no longer reach the engine at that size

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): take spend locks in one order so the ledger cannot deadlock

A ledger delivery updates many spend tables in one transaction, so it can
deadlock with any writer that locks the same rows in another order. It took the
team advisory lock after the user and team rows, while team roster writers take
it first

The delivery now takes every team advisory lock before any row, then writes
tables in one canonical order with ids sorted. It also runs under the rollup
lock_timeout, applies 500 deliveries per batch by default, and spreads retries
with jitter

The writers that could still collide with it now follow the same order. The
budget reset cascade, model allowlist rename, global spend reset, bulk user
update, org delete and the delete-by-list paths lock their rows in key order
first, and access group endpoints update teams and keys in one sorted pass

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(accounting): prove a ledger delivery waits instead of deadlocking

Real Postgres tests hold the first lock of a team roster change, an access group
update and a budget reset cascade, start a delivery, and let the other writer
finish. The delivery must wait and then charge once. Before the lock order fix
each case ends in a Postgres deadlock

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(accounting)!: make the SpendLogs ledger the default accounting path

use_spend_logs_ledger now defaults to true, so every request is charged through
LiteLLM_SpendLogDelivery. Setting it to false rolls back to the in-memory and
Redis spend queues, which stay in place. Tests that cover those legacy writers
pin the setting off, and the rest check the queued charge the ledger stores

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(proxy): let the mocked client open a transaction for key and team deletes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(proxy): take team locks first when a model rename rewrites allowlists

A rename locked team rows before user rows while a roster change holds the team
advisory lock and a user row, so the two could deadlock. The rename now takes the
advisory lock for every team that lists the model, sorted, before any row lock.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): drain a backlog in one worker run, one batch per transaction

One run applied a single fresh batch, so at 500 per batch charges fell behind any
load above that rate. The worker now keeps claiming fresh batches while they come
back full, for up to five seconds, and each batch still commits on its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): queue only the charge when a request's spend log is suppressed

With logging off for a request, the ledger still queued the full row, so a failed
flush or a shutdown could leave its prompt and response in Redis. The queue now
holds only the fields the charge needs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* style(proxy): format the rename team listing query

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): remove expired delivery receipts as fast as the worker applies them

The worker now applies several batches per run but still removed one cleanup batch
of applied receipts, so the table could outgrow its retention window. Cleanup now
repeats while each pass removes a full batch, for up to five seconds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(proxy): retry a model rename when a team starts listing the model mid-lock

The team advisory locks come from a snapshot, so a team that gained the old name
before its row lock was locked without its advisory lock. That attempt now commits
without writing and the next one locks the fresh set of teams.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Joshua Valluru <326636767+joshua-berri@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 09:14:10 -08:00
..
html_forms
__init__.py
test_admin_ui_utils.py
test_auth_cache_invalidation_pubsub.py fix(proxy): charge budget windows when the cached key or team expired before the request ended (#47608) 2026-11-02 14:41:59 -08:00
test_cache_aware_routing.py
test_cache_codec.py
test_callback_config_validation.py
test_callback_utils.py
test_check_batch_cost.py feat(accounting)!: make the SpendLogs ledger the default accounting path (#49872) 2026-11-05 09:14:10 -08:00
test_check_responses_cost.py
test_codex_model_catalog.py feat(proxy)!: honor model_info.max_input_tokens in the Codex catalog for stock models (#49701) 2026-11-02 13:27:58 -08:00
test_config_sync_pubsub.py
test_credential_hydration.py
test_custom_openapi_spec.py
test_debug_utils.py
test_discoverable_model_filter.py
test_encrypt_decrypt_utils.py
test_error_body_call_id.py
test_expired_ui_session_key_cleanup_manager.py
test_fips.py feat(proxy): add general_settings.fips_mode startup gate (#48600) 2026-11-03 14:56:14 -08:00
test_get_routes.py
test_http_parsing_utils.py feat(skills): upload local skill ZIPs from the dashboard (#50008) 2026-11-04 00:56:40 -08:00
test_json_merge_patch.py
test_key_rotation_e2e.py
test_key_rotation_integration.py
test_key_rotation_lock.py
test_key_rotation_manager.py
test_load_config_utils.py
test_model_deprecation.py feat(alerts): show recommended model provenance and provider notices (#50162) 2026-11-05 04:34:17 +00:00
test_model_listing_utils.py
test_openai_endpoint_utils.py
test_openai_error_payload.py
test_openapi_route.py
test_openapi_schema_compat.py
test_periodic_reload_schedule.py
test_proxy_encrypt_decrypt.py
test_rbac_utils.py
test_realtime_cache.py feat(realtime)!: support latest OpenAI audio models (#35600) 2026-11-03 07:27:52 +00:00
test_registry_read_through.py
test_request_tag_mappings.py chore(proxy): use neutral example names for baggage tag mappings (#50011) 2026-11-04 14:49:55 +00:00
test_reset_budget_job.py
test_resource_ownership.py
test_scheduled_job_stagger.py
test_semantic_text_index.py
test_slow_gc_logger.py
test_sse_keepalive.py
test_static_asset_utils.py
test_swagger_utils.py
test_timezone_utils.py
test_token_count_log.py
test_token_count_settings.py fix(count_tokens): count grok-4-1, grok-4.3 and DeepSeek V4.1 tool requests the way their hosts bill them (#49074) 2026-11-02 12:37:35 -08:00
test_upsert_budget_membership.py
test_user_api_key_cache.py fix(proxy): charge budget windows when the cached key or team expired before the request ended (#47608) 2026-11-02 14:41:59 -08:00
test_validation_error_body.py