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): price proxy-aliased models from the model map in stream_chunk_builder

mateo-berria17bc1f22cf6
diff --git a/litellm/main.py b/litellm/main.py
index c2c450967a5..c5af2db75a4 100644
--- a/litellm/main.py
+++ b/litellm/main.py
@@ -8602,6 +8602,18 @@ def _stream_builder_response_cost(response: ModelResponse, logging_obj: Optional
try:
return litellm.completion_cost(completion_response=response, custom_llm_provider=provider_hint)
except Exception:
+ return _stream_builder_model_map_cost(response)
+
+
+def _stream_builder_model_map_cost(response: ModelResponse) -> float | None:
+ model_name: Final = getattr(response, "model", None)
+ usage: Final = getattr(response, "usage", None)
+ if not isinstance(model_name, str) or not model_name or not isinstance(usage, Usage):
+ return None
+ try:
+ prompt_cost, completion_tokens_cost = litellm.cost_per_token(model=model_name, usage_object=usage)
+ return prompt_cost + completion_tokens_cost
+ except Exception: # noqa: BLE001 # cost_per_token raises bare Exception for unpriceable models
return None
diff --git a/tests/test_litellm/test_main.py b/tests/test_litellm/test_main.py
index 9408898ad89..8cf878d05d9 100644
--- a/tests/test_litellm/test_main.py
+++ b/tests/test_litellm/test_main.py
@@ -3113,6 +3113,24 @@ def test_stream_chunk_builder_unknown_model_leaves_response_cost_unset():
assert response.choices[0].message.content == "Hello world."
+def test_stream_chunk_builder_prices_proxy_alias_via_model_map():
+ chunks: Final = [
+ _stream_builder_text_chunk("claude-opus-5", "Hello "),
+ _stream_builder_text_chunk("claude-opus-5", "world.", finish_reason="stop"),
+ ]
+ for chunk in chunks:
+ chunk._hidden_params = {"custom_llm_provider": "openai"}
+
+ response: Final = litellm.stream_chunk_builder(chunks=chunks, messages=[{"role": "user", "content": "hi"}])
+
+ assert response is not None
+ assert response._hidden_params["custom_llm_provider"] == "openai"
+ prompt_cost, completion_cost = litellm.cost_per_token(model="claude-opus-5", usage_object=response.usage)
+ expected_cost: Final = prompt_cost + completion_cost
+ assert expected_cost > 0
+ assert response._hidden_params["response_cost"] == pytest.approx(expected_cost)
+
+
def _stream_builder_logging_obj() -> LiteLLMLogging:
logging_obj: Final = LiteLLMLogging(
model="gpt-4o",