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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(cost): stop non-string service_tier from silently dropping cost tracking (#30690)
Mateo Wang43dadc5138d4
diff --git a/litellm/cost_calculator.py b/litellm/cost_calculator.pyindex 712a3b360cc..bb5b778d02e 100644--- a/litellm/cost_calculator.py+++ b/litellm/cost_calculator.py@@ -1227,10 +1227,14 @@ def completion_cost(if service_tier is None and optional_params is not None:service_tier = optional_params.get("service_tier")- # "auto" is a routing preference, not a billable tier: the provider picks- # the tier and reports the one actually served on the response/usage, so- # defer to that instead of pricing the request-level "auto" as standard- if service_tier is not None and service_tier.lower() == ServiceTier.AUTO.value:+ # A request-level service_tier only prices the request when it is a+ # concrete billable tier string. "auto" is a routing preference and any+ # non-string value is not a billable tier, so defer to the tier the+ # provider reports on the response/usage instead of crashing or mispricing+ if (+ not isinstance(service_tier, str)+ or service_tier.lower() == ServiceTier.AUTO.value+ ):service_tier = None# Extract service_tier from completion_response if not provideddiff --git a/tests/test_litellm/test_cost_calculator.py b/tests/test_litellm/test_cost_calculator.pyindex dfda21785f9..16f990af2b2 100644--- a/tests/test_litellm/test_cost_calculator.py+++ b/tests/test_litellm/test_cost_calculator.py@@ -2233,6 +2233,58 @@ def test_completion_cost_anthropic_auto_tier_uses_served_priority_rate():assert cost == pytest.approx(expected_priority)+def test_completion_cost_non_string_service_tier_defers_to_served_tier():+ """+ Regression: a non-string request-level ``service_tier`` (reachable via+ ``allowed_openai_params``/``drop_params``) must not crash cost tracking.++ Before the fix, ``completion_cost`` called ``service_tier.lower()`` on the+ request-level value, so a dict raised ``AttributeError``. ``_response_cost_calculator``+ swallowed it and reported ``response_cost=None``, silently dropping the cost.+ The non-string preference must be ignored so pricing defers to the tier the+ provider actually served on the response usage.+ """+ from litellm import completion_cost+ from litellm.llms.anthropic.chat.transformation import AnthropicConfig++ os.environ["LITELLM_LOCAL_MODEL_COST_MAP"] = "True"+ litellm.model_cost = litellm.get_model_cost_map(url="")++ model = "claude-test-non-string-tier-cost-model"+ litellm.register_model(+ model_cost={+ model: {+ "input_cost_per_token": 3e-6,+ "output_cost_per_token": 15e-6,+ "input_cost_per_token_priority": 6e-6,+ "output_cost_per_token_priority": 30e-6,+ "litellm_provider": "anthropic",+ "max_tokens": 8192,+ }+ }+ )++ usage = AnthropicConfig().calculate_usage(+ usage_object={+ "input_tokens": 1000,+ "output_tokens": 500,+ "service_tier": "priority",+ },+ reasoning_content=None,+ )+ response = ModelResponse(usage=usage, model=model)++ cost = completion_cost(+ completion_response=response,+ model=model,+ custom_llm_provider="anthropic",+ optional_params={"service_tier": {"name": "auto"}},+ )++ expected_priority = 1000 * 6e-6 + 500 * 30e-6+ assert cost == pytest.approx(expected_priority)++def test_anthropic_cost_per_token_prices_cache_at_served_tier_with_multiplier():"""Regression for the cache/tier interaction in the Anthropic geo/speed path.