litellm/tests/unit/proxy/hooks
owen-hj 440572c451
fix(realtime): enforce TPM limits during realtime sessions (#53689)
* fix(realtime): count realtime turns toward TPM limits and refuse turns over the limit

Each response.done charges its tokens to the session's key, team, user, end user, tag and per-key model TPM windows before the client sees it. Each response.create is checked first, read only. A turn over a limit, or one whose limits cannot be checked, gets an error event, is not forwarded, and the session stays open. A response the provider starts on its own over the limit is cancelled. The close still charges no tokens, so session TPM equals the sum of its turns

* fix(realtime): apply each turn's TPM charge before the next turn is checked

With Redis, the turn charge rode the socket's post-call batch, which flushes up to a second later, so a response.create sent right after response.done was admitted over the limit

* fix(realtime): never cancel a turn on a call the proxy only listens to

* fix(realtime): count a response the provider reports done twice only once

OpenAI sends a second response.done with the same id and usage when the caller starts speaking right after a reply finishes, so charging every event counted that reply twice toward the TPM limit

* fix(realtime): stop over-limit turns on every provider and count transcription tokens

Check the limits on every response.created, so a rejected create earns no free turn. A provider that cannot cancel a response now has its session ended before the output reaches the client

Count token-billed transcription turns, retry a failed turn charge before the next check, check deployment capacity per turn, and find per-model limits by the router's model group so aliases are covered. Bedrock guardrail replies are checked and its final usage is charged at close

* fix(realtime): stop output after a session ends over its token limit

Frames the provider already sent no longer reach the client once the proxy ends a session over its TPM limit, and their usage still counts. Bedrock text turns over the limit now end the session the same way audio turns do

* fix(realtime): cancel a refused response by its id

A response outside the default conversation is only cancelled when the cancel names it. Move the Redis-backed turn test to the integration suite and give the session test helpers precise frame types

* fix(realtime): count realtime sessions toward deployment capacity

* fix(realtime): stop idle sessions holding TPM and charge each turn to capacity

An open realtime session no longer reserves part of the limit, and each finished turn now counts toward deployment capacity as it arrives. A refused turn the provider cannot cancel still reports its tokens before the session closes

* fix(realtime): send one refusal per turn and stop reporting limit-ended sessions as failures

* fix(realtime): close at the refusal, gate Gemini text turns, and charge agent windows per turn

* fix(realtime): check and charge project TPM per turn and keep capacity on the turn's model group

A realtime turn is now checked against the project's ITPM and OTPM windows
before it starts and charged with its actual usage, cached input excluded.
A refusal no longer outlives a later check that passes. Deployment capacity
charges a session's close to the model group its turns used, and usage a
provider reports only at close is charged before the session-end log

* fix(realtime): write per-turn project charges to Redis at once and let started turns run when limits cannot be read

Project ITPM and OTPM charges for a realtime turn skip the request's Redis batch, so a back-to-back
response.create on another worker sees them. A turn OpenAI or Azure started on its own is no longer
cancelled when the limiter cannot be read, matching Gemini and Bedrock. The fake Redis test now checks
that its accept thread stops instead of timing a test

* fix(realtime): log a turn before counting it, refuse started turns when fail-closed limits cannot be read, and hold back a refused turn's output

* fix(realtime): keep a refused turn's finished output and conversation items from the client

* fix(bedrock): wait for a refused turn's usage when the client side ends during the over-limit close

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-24 05:15:04 -08:00
..
litellm_skills ci(unit): cut unit check time with cheaper per-test resets and fewer non-unit tests (#51864) 2026-11-14 15:41:30 -08:00
model_capacity_golden feat(model_capacity): replace the python dynamic rate limiter v3 with a rust callback (#51540) 2026-11-17 22:20:54 +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_capacity_handler.py fix(realtime): enforce TPM limits during realtime sessions (#53689) 2026-11-24 05:15:04 -08:00
parallel_request_limiter_v3_golden.json fix(realtime): enforce TPM limits during realtime sessions (#53689) 2026-11-24 05:15:04 -08:00
test_async_post_call_streaming_iterator_hook.py fix(guardrails): run the end-of-stream post_call scan when the client disconnects mid-stream (#43839) 2026-10-05 01:44:43 -07:00
test_autorouter_baseline_cache.py fix(proxy): resolve admin oidc api_key references on batch cancel, fine-tuning listing, OCR, /v1/messages and cache prediction (#53678) 2026-11-23 10:11:10 -08:00
test_banned_keyword_list.py chore: move workspace packages under litellm (#51000) 2026-11-12 04:19:57 +00:00
test_batch_enqueued_tokens.py feat(callbacks-parallel-request-limiter): port parallel request limiter v3 to a rust callback (#51537) 2026-11-16 22:56:38 -08:00
test_batch_enqueued_tokens_rust.py feat(callbacks-parallel-request-limiter): port parallel request limiter v3 to a rust callback (#51537) 2026-11-16 22:56:38 -08:00
test_batch_file_validation.py fix(proxy): count Vertex and Bedrock batch input tokens (#53257) 2026-11-22 18:02:42 -08:00
test_batch_rate_limiter.py fix(proxy): count Vertex and Bedrock batch input tokens (#53257) 2026-11-22 18:02:42 -08:00
test_dynamic_rate_limiter.py refactor(rust_bridge): route every Rust-to-Python call through host modules (#50822) 2026-11-11 01:57:54 +00:00
test_dynamic_rate_limiter_v3_on_native_limiter_rust.py feat(model_capacity): replace the python dynamic rate limiter v3 with a rust callback (#51540) 2026-11-17 22:20:54 +00:00
test_dynamic_rate_limiter_v3_rust.py fix(realtime): enforce TPM limits during realtime sessions (#53689) 2026-11-24 05:15:04 -08:00
test_image_generation_guardrails.py test(proxy): move auth, hooks, policy_engine and client tests into tests/unit/proxy (#43998) 2026-10-01 10:11:45 -07:00
test_key_management_event_hooks.py feat(control_plane): dataplane registry, credential strategies and client (#49655) 2026-11-16 17:36:23 +00:00
test_max_budget_per_session_limiter.py fix(redis): share one native circuit breaker between RedisCache and the Rust stores (#50473) 2026-11-07 17:50:06 +09:00
test_max_iterations_limiter.py refactor(typing): clear about 1,100 basedpyright Any errors across 303 files (#49986) 2026-11-06 13:31:15 -08:00
test_message_thread_ownership.py refactor(auth_cache): type auth cache keys and keep the cache always native (#51130) 2026-11-21 17:20:30 +09:00
test_model_budget_shared_windows.py feat(proxy)!: model family and access group entries in model_max_budget (#47722) 2026-10-28 15:40:57 -07:00
test_model_capacity_native.golden.json feat(model_capacity): replace the python dynamic rate limiter v3 with a rust callback (#51540) 2026-11-17 22:20:54 +00:00
test_model_capacity_native.py feat(model_capacity): replace the python dynamic rate limiter v3 with a rust callback (#51540) 2026-11-17 22:20:54 +00:00
test_model_max_budget_limiter.py feat(realtime)!: serve GPT-Live over WebRTC with sideband attach, and bill WebRTC calls from a proxy sideband (#53282) 2026-11-21 22:49:26 -08:00
test_parallel_request_limiter.py fix(proxy): bill an OpenAI pass-through background response to the key that created it (#53586) 2026-11-23 20:14:58 +00:00
test_parallel_request_limiter_v3.py feat(anthropic): price Claude subscription OAuth allowance usage at $0, overage at API rates (#52901) 2026-11-19 15:08:39 -08:00
test_parallel_request_limiter_v3_native.py feat(model_capacity): replace the python dynamic rate limiter v3 with a rust callback (#51540) 2026-11-17 22:20:54 +00:00
test_parallel_request_limiter_v3_rust.py fix(realtime): enforce TPM limits during realtime sessions (#53689) 2026-11-24 05:15:04 -08:00
test_post_call_failure_hook_integration.py test(proxy): move auth, hooks, policy_engine and client tests into tests/unit/proxy (#43998) 2026-10-01 10:11:45 -07:00
test_post_call_response_headers_hook.py test(proxy): move auth, hooks, policy_engine and client tests into tests/unit/proxy (#43998) 2026-10-01 10:11:45 -07:00
test_post_call_streaming_hook_integration.py test(proxy): move auth, hooks, policy_engine and client tests into tests/unit/proxy (#43998) 2026-10-01 10:11:45 -07:00
test_post_call_success_hook_integration.py test(proxy): move auth, hooks, policy_engine and client tests into tests/unit/proxy (#43998) 2026-10-01 10:11:45 -07:00
test_priority_class_share.py feat(dynamic_rate_limiter): customer organizations and keys borrow idle model capacity and split class shares (#51705) 2026-11-15 12:05:58 -08:00
test_prompt_cache_observer.py fix(proxy): keep a retried /v1/messages stream's spend log by logging the superseded attempt as a failure (#45136) 2026-10-16 21:19:58 +00:00
test_prompt_injection_detection.py refactor(rust_bridge): route every Rust-to-Python call through host modules (#50822) 2026-11-11 01:57:54 +00:00
test_proxy_hooks_init.py chore: move workspace packages under litellm (#51000) 2026-11-12 04:19:57 +00:00
test_proxy_rate_limit_provider_field.py feat(model_capacity): replace the python dynamic rate limiter v3 with a rust callback (#51540) 2026-11-17 22:20:54 +00:00
test_proxy_rate_limit_provider_field_capacity_rust.py feat(model_capacity): replace the python dynamic rate limiter v3 with a rust callback (#51540) 2026-11-17 22:20:54 +00:00
test_proxy_rate_limit_provider_field_rust.py feat(callbacks-parallel-request-limiter): port parallel request limiter v3 to a rust callback (#51537) 2026-11-16 22:56:38 -08:00
test_proxy_track_cost_callback.py refactor(auth_cache): type auth cache keys and keep the cache always native (#51130) 2026-11-21 17:20:30 +09:00
test_proxy_track_cost_callback_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_proxy_track_cost_callback_rust.py fix(spend-logs): let the proxy finish slow failure spend counters (#54003) 2026-11-24 14:50:24 +09:00
test_rate_limiter_toctou_capacity_rust.py feat(model_capacity): replace the python dynamic rate limiter v3 with a rust callback (#51540) 2026-11-17 22:20:54 +00:00
test_rate_limiter_toctou_rust.py feat(model_capacity): replace the python dynamic rate limiter v3 with a rust callback (#51540) 2026-11-17 22:20:54 +00:00
test_search_free_tier.py fix(proxy): scope the search free-tier counter to the request's provider (#47646) 2026-10-20 22:01:53 +00:00
test_send_invite_email.py test(proxy): move auth, hooks, policy_engine and client tests into tests/unit/proxy (#43998) 2026-10-01 10:11:45 -07:00
test_sensitive_data_routing.py fix(redis): share one native circuit breaker between RedisCache and the Rust stores (#50473) 2026-11-07 17:50:06 +09:00
test_tpm_concurrent.py fix(proxy): leave cache-read tokens out of the priority limiter's token count (#52209) 2026-11-17 11:32:25 -08:00
test_tpm_concurrent_rust.py fix(google_genai): honor snake_case generation_config on generateContent (#51139) 2026-11-18 03:47:08 +00:00
test_unit_test_max_model_budget_limiter.py fix(router): inspect image content without translating routed requests (#49371) 2026-10-31 10:44:24 -07:00
test_user_management_event_hooks.py test(proxy): move auth, hooks, policy_engine and client tests into tests/unit/proxy (#43998) 2026-10-01 10:11:45 -07:00