fix(cost_calculator): keep base_model pricing off the regional row
Some checks failed
LiteLLM Rust / rust-lint (push) Has been cancelled
LiteLLM Rust / rust-test (push) Has been cancelled
Terraform Provider / gofmt, vet, build, test (push) Has been cancelled
Terraform Provider / Provider endpoints vs proxy OpenAPI schema (push) Has been cancelled
Terraform Modules / fmt, validate, test (aws) (push) Has been cancelled
Terraform Modules / fmt, validate, test (gcp) (push) Has been cancelled

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/<region>/<base_model>. 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
This commit is contained in:
mateo-berri 2026-09-10 08:12:16 -07:00
parent 0308b05c7a
commit f6685b7858
2 changed files with 25 additions and 1 deletions

View file

@ -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,

View file

@ -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)."""