litellm/tests/unit/proxy/hooks
edvinsq d473d742d2
fix(proxy): keep a request's slot, TPM and budget holds while it runs MCP tools (#54469)
* fix(proxy): keep a request's parallel slot when an MCP tool call inside it fails

* fix(proxy): keep the request's budget hold when a tool call inside it fails on the Rust spend logger

The Rust spend logger replaces the Python budget logger whenever the spend
logs ledger is on, which is the default with a database. Its failure path
released the request's budget reservation for every failure, so a failed MCP
tool call still freed the hold while the request ran. The failure document now
says whether the failure was reported inside a tool call, and the logger keeps
the reservation for those while still writing the failure row

The MCP tool loop again passes empty request data for a failure before the
server lookup, as before this branch, so failure hooks see no new fields.
Test signatures gain their return and Final annotations

* fix(proxy): keep the request's rate limit headers when a tool call inside it runs

A tool call's limit check replaced the request's stored rate limit response, so an MCP tool request answered without its x-ratelimit-api_key-* headers. The tool call now leaves the request's stored response alone, so the headers and the logged statuses come from the request's own check, as on a request without tools

* fix(proxy): check a tool call inside a request against TPM and project IO limits, and keep the request's budget hold and slot through its tool calls

A tool call inside a request reserves TPM and project IO on a scratch limiter state, so an exhausted limit still refuses it as before, then gives that reservation straight back so the request's own reservation settles once at its end. The tool call's own spend log no longer carries the request's budget reservation, which it used to settle at the tool call's cost. The MCP loop stamps the request's slot as reused when its tool calls start, so a round logged before them, such as streaming /v1/messages round 1, leaves the slot held

* fix(mcp): keep the request's budget hold through the MCP tool listing inside it

The tool listing's own spend log carried the request's budget reservation and finalized it before the request ended, so the listing now logs with the reservation stripped, as the tool call already does

* fix(proxy): refund a tool call's TPM hold before the tool runs

The scratch reservation a nested MCP tool call takes for its TPM and IO
check now goes back to Redis at once instead of waiting on the request's
post-call batch, so other requests on the key no longer see the tool
call's tokens while the request runs

The slot test now uses fixed end times, and the budget tests read the
spend log metadata through the real logging path

* test(proxy): move the tool call TPM refund test into the integration suite

It starts a Redis server, which tests/unit does not allow. It now runs the proxy's own pre-call hook
against the integration lane's Redis, and fails on base with the tool call's tokens still held

* test(mcp): run the budget hold tests through the real MCP manager and tool registry

The tool call test now calls a registered in-process OpenAPI tool through the real server
manager and proxy logging, and the listing test uses the real manager with no servers, so
neither doubles our own code. Removing either budget reservation strip fails its test

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-26 08:11:44 +00: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
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(proxy): keep a request's slot, TPM and budget holds while it runs MCP tools (#54469) 2026-11-26 08:11:44 +00:00
test_async_post_call_streaming_iterator_hook.py
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(proxy): keep a request's slot, TPM and budget holds while it runs MCP tools (#54469) 2026-11-26 08:11:44 +00:00
test_image_generation_guardrails.py
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(gemini): return an error instead of an empty 200 when a Gemini image request gets no image (#29125) 2026-11-25 20:55:23 +00:00
test_max_iterations_limiter.py
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
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 fix(proxy): bill a refused /v1/messages stream on one failure row with the upstream's tokens (#54238) 2026-11-25 10:14:41 +00:00
test_parallel_request_limiter.py fix(passthrough): bill OpenAI Agents API turns from the session's turn usage (#51237) 2026-11-24 11:54:08 -08:00
test_parallel_request_limiter_v3.py fix(proxy): keep token rate-limit reservations for embeddings the provider reports as 0 tokens (#54088) 2026-11-24 08:11:49 -08:00
test_parallel_request_limiter_v3_native.py fix(gemini): return an error instead of an empty 200 when a Gemini image request gets no image (#29125) 2026-11-25 20:55:23 +00:00
test_parallel_request_limiter_v3_rust.py fix(proxy): keep a request's slot, TPM and budget holds while it runs MCP tools (#54469) 2026-11-26 08:11:44 +00:00
test_post_call_failure_hook_integration.py
test_post_call_response_headers_hook.py
test_post_call_streaming_hook_integration.py
test_post_call_success_hook_integration.py
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
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 fix(proxy): keep a request's slot, TPM and budget holds while it runs MCP tools (#54469) 2026-11-26 08:11:44 +00: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(proxy): keep a request's slot, TPM and budget holds while it runs MCP tools (#54469) 2026-11-26 08:11:44 +00: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
test_send_invite_email.py
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(proxy): reserve TPM for the system prompt on Messages and instructions on Responses (#54318) 2026-11-24 19:55:39 -08:00
test_unit_test_max_model_budget_limiter.py
test_user_management_event_hooks.py