mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-06 07:48:06 +00:00
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.
This commit is contained in:
parent
2c0e47ba7e
commit
1a890ead5e
2 changed files with 20 additions and 1 deletions
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue