litellm/tests/unit/proxy/hooks
devin-ai-integration[bot] f86f570aca
fix(proxy): keep a retried /v1/messages stream's spend log by logging the superseded attempt as a failure (#45136)
* fix(proxy): keep a retried /v1/messages stream's spend log by logging the superseded attempt as a failure

A /v1/messages stream the router retries before any content shares one logging object across both attempts. The superseded attempt's end-of-stream logging ran as a success (a usage-only or placeholder result) and claimed the request's one-shot success log, so the recovered stream's spend row was either missing or written under the superseded attempt's message id. The passthrough logging path now logs an Anthropic stream whose frames carry an `event: error` as a failure with whatever usage the provider reported, the way a dropped stream already does, and the router closes the superseded stream before it opens the retry so that logging is queued first.

* fix(proxy): log a provider error frame through an attempt-scoped logging copy

The error-frame failure stashed the superseded attempt's usage on the logging
object the retry shares, so the retry's success log carried the failed
attempt's token counts. The failure now logs through a copy of the logging
object with its own model_call_details, and the helper moves into the
Anthropic passthrough logging handler so the proxy streaming handler only
routes by endpoint type.

* fix(proxy): scope the error-frame failure log to streams closed at the frame

A stream that ran to its end at the provider's error frame kept logging
through the success route before this PR, and the attempt-scoped failure
log is now taken only for a stream the router closed at that frame, with
the copy made at the close so the retry's deployment never reaches it

* chore: drop duplicate litellm import and mark the stream_ended rebind

* fix(proxy): log only router-superseded attempts as error-frame failures

A client that hangs up at a forwarded error frame closed the stream the same way the router closes a superseded attempt, so a non-retried stream lost its spend. The router now closes a superseded attempt through aclose_superseded_attempt, and only that close routes the attempt to the failure log

* test: type the _settle helper's recorded events

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
(cherry picked from commit 8bb8ac7f18)
2026-10-31 18:02:04 -07:00
..
litellm_skills
__init__.py
test_async_post_call_streaming_iterator_hook.py
test_autorouter_baseline_cache.py
test_banned_keyword_list.py
test_batch_enqueued_tokens.py
test_batch_file_validation.py
test_batch_rate_limiter.py
test_dynamic_rate_limiter.py
test_dynamic_rate_limiter_v3.py
test_image_generation_guardrails.py
test_key_management_event_hooks.py
test_max_budget_per_session_limiter.py
test_max_iterations_limiter.py
test_model_max_budget_limiter.py
test_parallel_request_limiter.py fix(proxy): only release the legacy limiter's slot from an admitted Responses WebSocket (#46822) 2026-10-15 20:34:48 -07:00
test_parallel_request_limiter_v3.py
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_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-31 18:02:04 -07:00
test_prompt_injection_detection.py
test_proxy_hooks_init.py
test_proxy_rate_limit_provider_field.py
test_proxy_track_cost_callback.py fix(proxy): attribute retry attempt spend rows to their deployment and keep them out of daily request counts (#47658) 2026-10-27 19:57:39 -07:00
test_rate_limiter_toctou.py
test_search_free_tier.py fix(proxy): scope the search free-tier counter to the request's provider (#47646) 2026-10-27 19:57:38 -07:00
test_send_invite_email.py
test_sensitive_data_routing.py
test_tpm_concurrent.py
test_unit_test_max_model_budget_limiter.py test(budgets): age the monthly window past any calendar month so the reset test passes on the 31st (#49372) 2026-10-31 13:00:28 -07:00
test_user_management_event_hooks.py