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): honor explicit zero tier rates and skip synthesized backend output rates

mateo-berri18752c860c3b
diff --git a/litellm/litellm_core_utils/llm_cost_calc/tiered_pricing.py b/litellm/litellm_core_utils/llm_cost_calc/tiered_pricing.py
index d4ce6abfcc4..9bcc2b1743c 100644
--- a/litellm/litellm_core_utils/llm_cost_calc/tiered_pricing.py
+++ b/litellm/litellm_core_utils/llm_cost_calc/tiered_pricing.py
@@ -60,6 +60,12 @@ def tier_rate(
cost_key: str,
fallback_cost_key: str | None = None,
) -> float:
- """Read a per-token rate from a tier, coercing YAML string costs to float."""
- raw: Final = tier.get(cost_key) or tier.get(fallback_cost_key, 0)
- return _coerce_cost_per_token(raw)
+ """Read a per-token rate from a tier, coercing YAML string costs to float.
+
+ A rate that is explicitly present wins over the fallback, an explicit zero
+ included, so a tier can declare a token type free.
+ """
+ primary: Final = tier.get(cost_key)
+ if primary is not None:
+ return _coerce_cost_per_token(primary)
+ return _coerce_cost_per_token(tier.get(fallback_cost_key, 0))
diff --git a/litellm/llms/dashscope/cost_calculator.py b/litellm/llms/dashscope/cost_calculator.py
index 3b3328d02a7..771ce140f66 100644
--- a/litellm/llms/dashscope/cost_calculator.py
+++ b/litellm/llms/dashscope/cost_calculator.py
@@ -97,11 +97,15 @@ def _calculate_completion_cost(
if tier_declares_output
else float(model_info.get("output_cost_per_token") or 0.0)
)
- tier_reasoning_cost: Final = tier_rate(tier, "output_cost_per_reasoning_token") if tier is not None else 0.0
- model_reasoning_cost: Final = (
- 0.0 if tier_declares_output else float(model_info.get("output_cost_per_reasoning_token") or 0.0)
+ tier_declares_reasoning: Final = tier is not None and "output_cost_per_reasoning_token" in tier
+ model_reasoning_rate: Final = None if tier_declares_output else model_info.get("output_cost_per_reasoning_token")
+ reasoning_cost: Final = (
+ tier_rate(tier, "output_cost_per_reasoning_token", "output_cost_per_token")
+ if tier_declares_reasoning
+ else float(model_reasoning_rate)
+ if model_reasoning_rate is not None
+ else output_cost
)
- reasoning_cost: Final = tier_reasoning_cost or model_reasoning_cost or output_cost
return (breakdown.completion_tokens * output_cost) + (breakdown.reasoning_tokens * reasoning_cost)
diff --git a/litellm/router.py b/litellm/router.py
index cc64d4992a8..4a450cf3c7c 100644
--- a/litellm/router.py
+++ b/litellm/router.py
@@ -7649,7 +7649,9 @@ class Router:
A user-specified ``output_cost_per_token`` always wins. No-op without a
tier table, when every tier declares its own output rate, or when the
- backend model has no canonical entry.
+ backend model has no canonical entry or no flat output rate:
+ ``get_model_info`` synthesizes a zero for tiered-only backends, and
+ storing that zero would mark the deployment as explicitly priced free.
"""
tiers: Final = model_info.get("tiered_pricing")
if not isinstance(tiers, list) or not tiers:
@@ -7663,7 +7665,7 @@ class Router:
except Exception: # noqa: BLE001 # get_model_info raises plain Exception for an unmapped backend model
return
backend_rate: Final = backend_info.get("output_cost_per_token")
- if backend_rate is not None:
+ if backend_rate:
model_info["output_cost_per_token"] = backend_rate
def _create_deployment(
diff --git a/tests/test_litellm/llms/dashscope/test_dashscope_cost_calculator.py b/tests/test_litellm/llms/dashscope/test_dashscope_cost_calculator.py
index 4188cb6a4f3..6f5aaabae06 100644
--- a/tests/test_litellm/llms/dashscope/test_dashscope_cost_calculator.py
+++ b/tests/test_litellm/llms/dashscope/test_dashscope_cost_calculator.py
@@ -403,6 +403,59 @@ class TestDashscopeCostCalculator:
assert math.isclose(completion_cost, 200 * 1.6e-06, rel_tol=1e-10)
+ def test_dashscope_model_zero_reasoning_rate_bills_reasoning_free(self):
+ """
+ Regression: a model declaring an explicit zero reasoning rate had it treated as
+ missing, billing reasoning tokens at the plain output rate instead of free.
+ """
+ litellm.model_cost["dashscope/qwen-zero-reasoning-test"] = {
+ "litellm_provider": "dashscope",
+ "mode": "chat",
+ "input_cost_per_token": 4e-07,
+ "output_cost_per_token": 1.6e-06,
+ "output_cost_per_reasoning_token": 0,
+ }
+
+ usage = Usage(
+ prompt_tokens=500,
+ completion_tokens=200,
+ completion_tokens_details=CompletionTokensDetailsWrapper(reasoning_tokens=150),
+ )
+ _, completion_cost = dashscope_cost_per_token(
+ model="qwen-zero-reasoning-test", usage=usage
+ )
+
+ assert math.isclose(completion_cost, 50 * 1.6e-06, rel_tol=1e-10)
+
+ def test_dashscope_tier_zero_reasoning_rate_bills_reasoning_free(self):
+ """
+ Regression: a tier declaring an explicit zero reasoning rate had it treated as
+ missing, billing reasoning tokens at the tier's output rate instead of free.
+ """
+ litellm.model_cost["dashscope/qwen-tier-zero-reasoning-test"] = {
+ "litellm_provider": "dashscope",
+ "mode": "chat",
+ "tiered_pricing": [
+ {
+ "range": [0, 1000],
+ "input_cost_per_token": 4e-07,
+ "output_cost_per_token": 1.6e-06,
+ "output_cost_per_reasoning_token": 0,
+ }
+ ],
+ }
+
+ usage = Usage(
+ prompt_tokens=500,
+ completion_tokens=200,
+ completion_tokens_details=CompletionTokensDetailsWrapper(reasoning_tokens=150),
+ )
+ _, completion_cost = dashscope_cost_per_token(
+ model="qwen-tier-zero-reasoning-test", usage=usage
+ )
+
+ assert math.isclose(completion_cost, 50 * 1.6e-06, rel_tol=1e-10)
+
def test_dashscope_tiered_pricing_zero_input_falls_back_to_flat_rates(self):
"""
No tier can be selected without input tokens, so an empty-prompt request must
diff --git a/tests/test_litellm/test_router_model_cost_isolation.py b/tests/test_litellm/test_router_model_cost_isolation.py
index 3fb4511e52c..dfe46d54ab8 100644
--- a/tests/test_litellm/test_router_model_cost_isolation.py
+++ b/tests/test_litellm/test_router_model_cost_isolation.py
@@ -1529,3 +1529,58 @@ def test_strategy_router_alias_pricing_never_enters_model_cost(monkeypatch):
finally:
litellm.model_cost = saved_model_cost
_invalidate_model_cost_lowercase_map()
+
+
+def test_inherit_builtin_tiered_output_rate_fills_the_backend_flat_rate():
+ """
+ A deployment entry whose custom tiers publish only input rates would bill
+ completions at 0, so the backend model's flat output rate is copied in at
+ registration.
+ """
+ model_info = {"tiered_pricing": [{"range": [0, 3000], "input_cost_per_token": 3.25e-07}]}
+
+ Router._inherit_builtin_tiered_output_rate(
+ model_info=model_info,
+ backend_model="claude-haiku-4-5",
+ custom_llm_provider="anthropic",
+ )
+
+ backend_rate = litellm.get_model_info(model="claude-haiku-4-5", custom_llm_provider="anthropic")[
+ "output_cost_per_token"
+ ]
+ assert backend_rate > 0
+ assert model_info["output_cost_per_token"] == backend_rate
+
+
+def test_inherit_builtin_tiered_output_rate_never_stores_a_synthesized_zero():
+ """
+ Regression: get_model_info reports output_cost_per_token 0 for a backend that
+ only publishes tiered rates (e.g. dashscope/qwen-flash), and storing that zero
+ would mark the deployment as explicitly priced free.
+ """
+ backend_info = litellm.get_model_info(model="qwen-flash", custom_llm_provider="dashscope")
+ assert backend_info["output_cost_per_token"] == 0
+
+ model_info = {"tiered_pricing": [{"range": [0, 3000], "input_cost_per_token": 3.25e-07}]}
+ Router._inherit_builtin_tiered_output_rate(
+ model_info=model_info,
+ backend_model="qwen-flash",
+ custom_llm_provider="dashscope",
+ )
+
+ assert "output_cost_per_token" not in model_info
+
+
+def test_inherit_builtin_tiered_output_rate_leaves_a_user_rate_alone():
+ model_info = {
+ "tiered_pricing": [{"range": [0, 3000], "input_cost_per_token": 3.25e-07}],
+ "output_cost_per_token": 9e-07,
+ }
+
+ Router._inherit_builtin_tiered_output_rate(
+ model_info=model_info,
+ backend_model="claude-haiku-4-5",
+ custom_llm_provider="anthropic",
+ )
+
+ assert model_info["output_cost_per_token"] == 9e-07