From 337f09cb1ceba7ab5da9cde1e807135eb9eb161b Mon Sep 17 00:00:00 2001 From: Diwak4r Date: Fri, 4 Sep 2026 20:58:14 +0545 Subject: [PATCH] test(cost-calc): cover region_name with a custom-priced router model id _select_model_name_for_cost_calc must price a custom-priced router deployment on the id its price was registered under, even when the response carries a region_name. c11a1f0bc1 ("scope region_name to response-derived model names") makes that hold via the explicit_pricing gate, but nothing pins the custom_pricing + router_model_id + region combination: test_custom_pricing_cost_calc_uses_router_model_id_from_ litellm_metadata exercises that path with completion_response=None, so no region is ever in play. The response here echoes a model and carries a region, so priced_from_response is True and explicit_pricing alone suppresses the region -- the guard this test exists to hold. Test-only; no production change. --- tests/test_litellm/test_cost_calculator.py | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/test_litellm/test_cost_calculator.py b/tests/test_litellm/test_cost_calculator.py index f610821e06a..43d59c66728 100644 --- a/tests/test_litellm/test_cost_calculator.py +++ b/tests/test_litellm/test_cost_calculator.py @@ -4327,6 +4327,47 @@ def test_select_model_name_keeps_base_model_free_of_region(_local_model_cost_map assert selected == "bedrock/moonshotai.kimi-k2.5" +def test_select_model_name_keeps_custom_priced_router_id_free_of_region(_local_model_cost_map): + """A custom-priced router deployment prices on the id its custom price was registered under, so a + region carried on the response must not widen the key to a regional one that was never registered.""" + + from litellm.cost_calculator import _select_model_name_for_cost_calc + + custom_model_id = "claude-sonnet-4-custom-region-test" + litellm.register_model( + model_cost={ + custom_model_id: { + "input_cost_per_token": 0.0003, + "output_cost_per_token": 0.0015, + "max_tokens": 8192, + "litellm_provider": "anthropic", + } + } + ) + response = litellm.ModelResponse( + id="x", + choices=[ + { + "index": 0, + "message": {"role": "assistant", "content": "hi"}, + "finish_reason": "stop", + } + ], + model="claude-sonnet-4-20250514", + ) + response._hidden_params = {"region_name": "us-east-1"} + + selected = _select_model_name_for_cost_calc( + model="anthropic/claude-sonnet-4-20250514", + completion_response=response, + custom_pricing=True, + custom_llm_provider="anthropic", + router_model_id=custom_model_id, + ) + + assert selected == f"anthropic/{custom_model_id}" + + def test_completion_cost_nonzero_for_slash_alias_model_name(_local_model_cost_map): """End-to-end cost through a "/"-containing alias must price above zero (#38069)."""