From 28267bf72bc0767b09d0587a37d89a0917820562 Mon Sep 17 00:00:00 2001 From: YiMing Zhang Date: Mon, 24 Aug 2026 17:42:41 +0800 Subject: [PATCH] fix(cost): use deployment model for router aliases Signed-off-by: YiMing Zhang --- litellm/litellm_core_utils/litellm_logging.py | 5 +- .../test_litellm_logging.py | 50 +++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py index 3ad4c187b6d..e07b8c5c582 100644 --- a/litellm/litellm_core_utils/litellm_logging.py +++ b/litellm/litellm_core_utils/litellm_logging.py @@ -1609,9 +1609,10 @@ class Logging(LiteLLMLoggingBaseClass): cache_hit = self.model_call_details.get("cache_hit", False) try: + model_for_cost: Final = litellm_model_name or self.get_deployment_model_for_cost() response_cost_calculator_kwargs: Final = { "response_object": result, - "model": litellm_model_name or self.model, + "model": model_for_cost, "cache_hit": cache_hit, "custom_llm_provider": self.model_call_details.get("custom_llm_provider", None), "base_model": _get_base_model_from_metadata(model_call_details=self.model_call_details), @@ -1632,7 +1633,7 @@ class Logging(LiteLLMLoggingBaseClass): custom_llm_provider=self.model_call_details.get("custom_llm_provider", None), litellm_params=(self.litellm_params if hasattr(self, "litellm_params") else None), optional_params=self.optional_params, - model=litellm_model_name or self.model, + model=model_for_cost, ), } except Exception as e: # error creating kwargs for cost calculation diff --git a/tests/test_litellm/litellm_core_utils/test_litellm_logging.py b/tests/test_litellm/litellm_core_utils/test_litellm_logging.py index 1c706be51fa..662a2b05186 100644 --- a/tests/test_litellm/litellm_core_utils/test_litellm_logging.py +++ b/tests/test_litellm/litellm_core_utils/test_litellm_logging.py @@ -281,6 +281,56 @@ def test_response_cost_calculator_uses_router_model_id_from_litellm_metadata(): litellm.model_cost.pop(custom_model_id, None) +def test_response_cost_calculator_uses_deployment_model_for_router_alias(): + """A streamed router alias must not hide the provider-qualified deployment model.""" + from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj + + alias_model = "vertex/claude-alias-cost-test" + deployment_model = "vertex_ai/claude-alias-cost-test" + input_cost = 2.0 + output_cost = 3.0 + + litellm.register_model( + model_cost={ + deployment_model: { + "input_cost_per_token": input_cost, + "output_cost_per_token": output_cost, + "max_tokens": 128000, + "max_input_tokens": 128000, + "max_output_tokens": 16384, + "litellm_provider": "vertex_ai", + } + } + ) + + try: + logging_obj = LiteLLMLoggingObj( + model=alias_model, + messages=[{"role": "user", "content": "Hi"}], + stream=True, + call_type="completion", + start_time=time.time(), + litellm_call_id="test-router-alias-cost", + function_id="test-fn", + ) + logging_obj.optional_params = {} + logging_obj.model_call_details["model"] = deployment_model + logging_obj.model_call_details["custom_llm_provider"] = "vertex_ai" + + response_obj = ModelResponse( + id="chatcmpl-router-alias-cost", + model=alias_model, + choices=[], + usage={"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15}, + ) + + cost = logging_obj._response_cost_calculator(result=response_obj) + + assert cost == pytest.approx((10 * input_cost) + (5 * output_cost)) + finally: + litellm.model_cost.pop(deployment_model, None) + + class TestGetRouterModelId: """Tests for the get_router_model_id helper method."""