From f6685b7858455ceddd16c6cc2c5e31521f6ef3cf Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Thu, 10 Sep 2026 08:12:16 -0700 Subject: [PATCH] fix(cost_calculator): keep base_model pricing off the regional row A deployment with base_model set was priced from the region's own row once completion_cost forwarded the response region into cost_per_token, which now strips the provider prefix and finds bedrock//. Explicit pricing (base_model or custom pricing) suppresses the region for cost_per_token the same way _select_model_name_for_cost_calc already does --- litellm/cost_calculator.py | 3 ++- tests/test_litellm/test_cost_calculator.py | 23 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/litellm/cost_calculator.py b/litellm/cost_calculator.py index 1678c05c470..48880ef5611 100644 --- a/litellm/cost_calculator.py +++ b/litellm/cost_calculator.py @@ -1292,6 +1292,7 @@ def completion_cost( service_tier = _normalize_service_tier(service_tier) + explicit_pricing: Final = custom_pricing is True or base_model is not None selected_model: Final = _select_model_name_for_cost_calc( model=model, completion_response=completion_response, @@ -1655,7 +1656,7 @@ def completion_cost( completion_tokens=completion_tokens or 0, custom_llm_provider=custom_llm_provider, response_time_ms=total_time, - region_name=region_name, + region_name=None if explicit_pricing else region_name, custom_cost_per_second=custom_cost_per_second, custom_cost_per_token=custom_cost_per_token, prompt_characters=prompt_characters, diff --git a/tests/test_litellm/test_cost_calculator.py b/tests/test_litellm/test_cost_calculator.py index d8c9b7cbc6e..4809b58be70 100644 --- a/tests/test_litellm/test_cost_calculator.py +++ b/tests/test_litellm/test_cost_calculator.py @@ -4363,6 +4363,29 @@ def test_select_model_name_keeps_base_model_free_of_region(_local_model_cost_map assert selected == "bedrock/moonshotai.kimi-k2.5" +def test_completion_cost_base_model_ignores_regional_row(_local_model_cost_map): + """A deployment with base_model set is priced from that model's own row even when the response + carries a region whose regional row charges different rates.""" + + response = litellm.ModelResponse( + id="x", + choices=[{"index": 0, "message": {"role": "assistant", "content": "hi"}, "finish_reason": "stop"}], + model="my-bedrock-deployment", + usage={"prompt_tokens": 1000, "completion_tokens": 0, "total_tokens": 1000}, + ) + response._hidden_params = {"custom_llm_provider": "bedrock", "region_name": "eu-central-1"} + flat = litellm.model_cost["anthropic.claude-instant-v1"] + regional = litellm.model_cost["bedrock/eu-central-1/anthropic.claude-instant-v1"] + assert flat["input_cost_per_token"] != regional["input_cost_per_token"] + + assert litellm.completion_cost( + completion_response=response, + model="my-bedrock-deployment", + custom_llm_provider="bedrock", + base_model="anthropic.claude-instant-v1", + ) == pytest.approx(1000 * flat["input_cost_per_token"]) + + 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)."""