From 40d3d1cca2ec4a945168afd718d1ce76f0ab799c Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Thu, 17 Sep 2026 17:16:44 +0000 Subject: [PATCH] fix(cost_calculator): keep deployment provider when response hidden provider differs Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- litellm/cost_calculator.py | 33 ++++++++++++++-------- tests/test_litellm/test_cost_calculator.py | 33 ++++++++++++++++++++++ 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/litellm/cost_calculator.py b/litellm/cost_calculator.py index 088f9e8867c..bbb23490d71 100644 --- a/litellm/cost_calculator.py +++ b/litellm/cost_calculator.py @@ -773,6 +773,23 @@ def _get_hidden_str_for_cost_calc(hidden_params: object, key: str) -> str | None return value if isinstance(value, str) and value else None +def _router_model_id_has_pricing(router_model_id: str | None) -> bool: + if router_model_id is None: + return False + entry: Final = litellm.model_cost.get(router_model_id) + if entry is None: + return False + return any( + entry.get(cost_key) is not None + for cost_key in ( + "input_cost_per_token", + "input_cost_per_second", + "input_cost_per_query", + "tiered_pricing", + ) + ) + + def _select_model_name_for_cost_calc( model: str | None, completion_response: object | None, @@ -809,17 +826,8 @@ def _select_model_name_for_cost_calc( ) if custom_pricing is True: - if router_model_id is not None and router_model_id in litellm.model_cost: - entry: Final = litellm.model_cost[router_model_id] - if ( - entry.get("input_cost_per_token") is not None - or entry.get("input_cost_per_second") is not None - or entry.get("input_cost_per_query") is not None - or entry.get("tiered_pricing") is not None - ): - return_model = router_model_id - else: - return_model = model + if _router_model_id_has_pricing(router_model_id): + return_model = router_model_id else: return_model = model @@ -1446,7 +1454,8 @@ def completion_cost( hidden_params = getattr(completion_response, "_hidden_params", None) if hidden_params is not None: - custom_llm_provider = hidden_params.get("custom_llm_provider", custom_llm_provider or None) + if not (custom_pricing is True and _router_model_id_has_pricing(router_model_id)): + custom_llm_provider = hidden_params.get("custom_llm_provider", custom_llm_provider or None) region_name = hidden_params.get("region_name", region_name) # For Gemini/Vertex AI responses, trafficType is stored in diff --git a/tests/test_litellm/test_cost_calculator.py b/tests/test_litellm/test_cost_calculator.py index a5ed7175649..1575c6aa1e0 100644 --- a/tests/test_litellm/test_cost_calculator.py +++ b/tests/test_litellm/test_cost_calculator.py @@ -834,6 +834,39 @@ def test_custom_pricing_cost_calc_uses_router_model_id_from_litellm_metadata(): assert custom_model_id not in (selected_model_no_custom or "") +def test_completion_cost_keeps_custom_pricing_provider_when_hidden_provider_differs( + monkeypatch: pytest.MonkeyPatch, +): + deployment_id = "azure-ai-custom-pricing-provider-test" + input_rate = 0.0004 + output_rate = 0.0011 + monkeypatch.setitem( + litellm.model_cost, + deployment_id, + { + "input_cost_per_token": input_rate, + "output_cost_per_token": output_rate, + "litellm_provider": "azure_ai", + }, + ) + + response = ModelResponse( + model="azure_ai/neutral-test-model", + usage=Usage(prompt_tokens=2, completion_tokens=3, total_tokens=5), + ) + response._hidden_params = {"custom_llm_provider": "openai"} + + cost = completion_cost( + completion_response=response, + model="azure_ai/neutral-test-model", + custom_llm_provider="azure_ai", + custom_pricing=True, + router_model_id=deployment_id, + ) + + assert cost == 2 * input_rate + 3 * output_rate + + def test_per_request_custom_pricing_with_router(): """When custom pricing is passed as per-request kwargs (not in model_list), _select_model_name_for_cost_calc should fall back to the model name