From 1754a0a33c98015f3a93098f275f641295c87526 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 28 Aug 2026 13:13:37 -0700 Subject: [PATCH] fix(streaming): keep partial-failure spend on the logging cost pipeline --- litellm/litellm_core_utils/streaming_handler.py | 1 + .../litellm_core_utils/test_streaming_handler.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/litellm/litellm_core_utils/streaming_handler.py b/litellm/litellm_core_utils/streaming_handler.py index 39fa8776578..1c204f68c48 100644 --- a/litellm/litellm_core_utils/streaming_handler.py +++ b/litellm/litellm_core_utils/streaming_handler.py @@ -2338,6 +2338,7 @@ class CustomStreamWrapper: partial_response: Final = litellm.stream_chunk_builder( chunks=self.chunks, messages=self.messages if isinstance(self.messages, list) else None, + logging_obj=self.logging_obj, ) if partial_response is None: return diff --git a/tests/test_litellm/litellm_core_utils/test_streaming_handler.py b/tests/test_litellm/litellm_core_utils/test_streaming_handler.py index 5329edce47e..a9cf92ced76 100644 --- a/tests/test_litellm/litellm_core_utils/test_streaming_handler.py +++ b/tests/test_litellm/litellm_core_utils/test_streaming_handler.py @@ -3469,6 +3469,21 @@ def test_record_partial_usage_for_failure_backfills_missing_cache_fields(): assert stashed.prompt_tokens_details.cached_tokens == 0 +def test_record_partial_usage_for_failure_prices_corrected_model_not_chunk_model(): + wrapper, logging_obj = _wrapper_with_partial_chunks( + chunk_model="claude-opus-5", + usage=Usage(prompt_tokens=40, completion_tokens=5, total_tokens=45), + model="gpt-4o-mini", + custom_llm_provider="openai", + ) + + wrapper._record_partial_usage_for_failure() + + rates = litellm.model_cost["gpt-4o-mini"] + expected = 40 * rates["input_cost_per_token"] + 5 * rates["output_cost_per_token"] + assert logging_obj.model_call_details["response_cost"] == pytest.approx(expected) + + def test_record_partial_usage_for_failure_carries_up_openai_style_cached_tokens(): recovered = Usage( prompt_tokens=1000,