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(cost_calculator): keep base_model pricing off the regional row

mateo-berrif6685b785845
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)."""