From 1a890ead5edac2a7a549502501c8fef75c3dbd70 Mon Sep 17 00:00:00 2001 From: "devin-ai-integration[bot]" <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Mon, 26 Oct 2026 16:26:05 +0000 Subject: [PATCH] fix(cost): stamp a zero bill over a reported usage.cost on streams A 100 percent discount or zero operator rates bill 0, and the final streamed usage.cost now says so instead of leaving the upstream's number on the chunk. --- .../streaming_chunk_builder_utils.py | 2 +- tests/unit/test_cost_calculator.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/litellm/litellm_core_utils/streaming_chunk_builder_utils.py b/litellm/litellm_core_utils/streaming_chunk_builder_utils.py index 024ddf0bd91..443c39b647e 100644 --- a/litellm/litellm_core_utils/streaming_chunk_builder_utils.py +++ b/litellm/litellm_core_utils/streaming_chunk_builder_utils.py @@ -1414,7 +1414,7 @@ def _stamp_streaming_usage_cost(usage: Usage, response: ModelResponse, logging_o if logging_obj is None: return computed_cost: Final = logging_obj.response_cost_calculator(result=response) - if not isinstance(computed_cost, (int, float)) or computed_cost <= 0: + if not isinstance(computed_cost, (int, float)) or computed_cost < 0: return stamp_billed_usage_cost(response, usage, computed_cost) _set_hidden_response_cost(response, float(computed_cost)) diff --git a/tests/unit/test_cost_calculator.py b/tests/unit/test_cost_calculator.py index 81b00f3225d..a52efc6b41e 100644 --- a/tests/unit/test_cost_calculator.py +++ b/tests/unit/test_cost_calculator.py @@ -10666,6 +10666,25 @@ def test_a_usage_cost_litellm_stamped_on_a_stream_is_not_billed_again_as_a_provi ) +def test_a_zero_bill_is_stamped_over_the_provider_reported_usage_cost_on_a_stream(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(litellm, "cost_discount_config", {"openai": 1.0}) + logging_obj: Final = _reported_cost_logging(True, {}) + + response: Final[object] = litellm.stream_chunk_builder( + [ + _reported_cost_stream_chunk("hi", None, None), + _reported_cost_stream_chunk(None, "stop", None), + _reported_cost_stream_chunk(None, None, Usage(**_REPORTED_USAGE)), + ], + messages=[{"role": "user", "content": "hi"}], + logging_obj=logging_obj, + ) + + assert isinstance(response, ModelResponse) + assert response.usage is not None and response.usage.cost == 0.0 + assert logging_obj.response_cost_calculator(result=response) == 0.0 + + def test_provider_reported_usage_cost_splits_input_and_output_by_the_token_math_ratio( monkeypatch: pytest.MonkeyPatch, ) -> None: