Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(streaming): keep partial-failure spend on the logging cost pipeline

mateo-berri1754a0a33c98
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,