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(auth): enforce budget for models not in cost map (#24949)

michelligabrieleadedae2cfac1
diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py
index c00a351bdd8..68bde8434a6 100644
--- a/litellm/proxy/auth/auth_checks.py
+++ b/litellm/proxy/auth/auth_checks.py
@@ -165,9 +165,24 @@ def _is_model_cost_zero(
)
return False
- # This model has zero cost explicitly configured
+ # Costs are 0 — verify this is from explicit configuration,
+ # not from defaulted sparse auto-registration entries.
+ # See: https://github.com/BerriAI/litellm/issues/24770
+ safe_name = str(model_name).replace("\n", "").replace("\r", "")
+ if not _is_cost_explicitly_configured(model_name, llm_router):
+ verbose_proxy_logger.debug(
+ "Model %s has zero cost but no explicit cost "
+ "configuration in model_cost entry — treating as unknown "
+ "cost (enforce budget)",
+ safe_name,
+ )
+ return False
+
verbose_proxy_logger.debug(
- f"Model {model_name} has zero cost explicitly configured (input: {input_cost}, output: {output_cost})"
+ "Model %s has zero cost explicitly configured (input: %s, output: %s)",
+ safe_name,
+ input_cost,
+ output_cost,
)
except Exception as e:
@@ -181,6 +196,33 @@ def _is_model_cost_zero(
return True
+def _is_cost_explicitly_configured(
+ model: str, llm_router: "Router"
+) -> bool:
+ """
+ Check if any deployment in the model group has cost fields explicitly
+ set in its litellm.model_cost entry.
+
+ When Router._create_deployment() registers a model not in the global
+ cost map, it creates a sparse entry like {"id": "<hash>"} with no cost
+ fields. _get_model_info_helper() then defaults missing costs to 0.
+ This function detects that scenario by checking the raw model_cost entry.
+ """
+ for deployment in llm_router.model_list:
+ if deployment.get("model_name") != model:
+ continue
+ model_id = deployment.get("model_info", {}).get("id")
+ if model_id is None:
+ continue
+ raw_entry = litellm.model_cost.get(model_id, {})
+ if (
+ "input_cost_per_token" in raw_entry
+ or "output_cost_per_token" in raw_entry
+ ):
+ return True
+ return False
+
+
async def _run_project_checks(
project_object: Optional[LiteLLM_ProjectTableCachedObj],
_model: Optional[Union[str, List[str]]],
diff --git a/litellm/router.py b/litellm/router.py
index 6cc6bad9def..586d5961b94 100644
--- a/litellm/router.py
+++ b/litellm/router.py
@@ -7808,8 +7808,8 @@ class Router:
max_tokens=None,
max_input_tokens=None,
max_output_tokens=None,
- input_cost_per_token=0,
- output_cost_per_token=0,
+ input_cost_per_token=None,
+ output_cost_per_token=None,
litellm_provider=llm_provider,
mode=mode,
supported_openai_params=supported_openai_params,
diff --git a/litellm/types/utils.py b/litellm/types/utils.py
index 82557513a8a..3f6e6e5aa5a 100644
--- a/litellm/types/utils.py
+++ b/litellm/types/utils.py
@@ -169,7 +169,7 @@ class ModelInfoBase(ProviderSpecificModelInfo, total=False):
max_tokens: Required[Optional[int]]
max_input_tokens: Required[Optional[int]]
max_output_tokens: Required[Optional[int]]
- input_cost_per_token: Required[float]
+ input_cost_per_token: Required[Optional[float]]
input_cost_per_token_flex: Optional[float] # OpenAI flex service tier pricing
input_cost_per_token_priority: Optional[
float
@@ -206,7 +206,7 @@ class ModelInfoBase(ProviderSpecificModelInfo, total=False):
input_cost_per_second: Optional[float] # for OpenAI Speech models
input_cost_per_token_batches: Optional[float]
output_cost_per_token_batches: Optional[float]
- output_cost_per_token: Required[float]
+ output_cost_per_token: Required[Optional[float]]
output_cost_per_token_flex: Optional[float] # OpenAI flex service tier pricing
output_cost_per_token_priority: Optional[
float
diff --git a/tests/test_litellm/proxy/auth/test_unmapped_model_budget_enforcement.py b/tests/test_litellm/proxy/auth/test_unmapped_model_budget_enforcement.py
new file mode 100644
index 00000000000..e9f4111f83d
--- /dev/null
+++ b/tests/test_litellm/proxy/auth/test_unmapped_model_budget_enforcement.py
@@ -0,0 +1,108 @@
+"""
+Test that models not in the cost map do NOT bypass budget enforcement.
+
+Regression test for the bug where unmapped models got fallback costs of 0,
+causing _is_model_cost_zero() to return True and skip all budget checks.
+
+See: https://github.com/BerriAI/litellm/issues/24770
+"""
+
+import copy
+
+import litellm
+from litellm.proxy.auth.auth_checks import _is_model_cost_zero
+from litellm.router import Router
+
+
+class TestUnmappedModelBudgetEnforcement:
+ """Unmapped models must NOT bypass budget checks."""
+
+ def setup_method(self):
+ """Snapshot litellm.model_cost before each test."""
+ self._saved_model_cost = copy.deepcopy(litellm.model_cost)
+
+ def teardown_method(self):
+ """Restore litellm.model_cost after each test."""
+ litellm.model_cost = self._saved_model_cost
+
+ def test_unmapped_model_enforces_budget(self):
+ """A model not in litellm.model_cost should have budget enforced."""
+ router = Router(
+ model_list=[
+ {
+ "model_name": "custom-model",
+ "litellm_params": {
+ "model": "openai/totally-nonexistent-model-xyz",
+ "api_key": "sk-fake",
+ },
+ },
+ ]
+ )
+ result = _is_model_cost_zero(model="custom-model", llm_router=router)
+ assert result is False, (
+ "Unmapped model should enforce budget (return False), "
+ "not bypass it (return True)"
+ )
+
+ def test_explicitly_free_model_bypasses_budget(self):
+ """A model with explicit cost=0 in model_info should bypass budget."""
+ router = Router(
+ model_list=[
+ {
+ "model_name": "free-model",
+ "litellm_params": {
+ "model": "ollama/llama2",
+ "api_base": "http://localhost:11434",
+ "input_cost_per_token": 0.0,
+ "output_cost_per_token": 0.0,
+ },
+ "model_info": {
+ "id": "free-model-id",
+ "input_cost_per_token": 0.0,
+ "output_cost_per_token": 0.0,
+ },
+ },
+ ]
+ )
+ result = _is_model_cost_zero(model="free-model", llm_router=router)
+ assert result is True, (
+ "Explicitly free model should bypass budget (return True)"
+ )
+
+ def test_known_paid_model_enforces_budget(self):
+ """A model in the cost map with non-zero costs should enforce budget."""
+ router = Router(
+ model_list=[
+ {
+ "model_name": "paid-model",
+ "litellm_params": {
+ "model": "openai/gpt-4o-mini",
+ "api_key": "sk-fake",
+ },
+ },
+ ]
+ )
+ result = _is_model_cost_zero(model="paid-model", llm_router=router)
+ assert result is False, (
+ "Known paid model should enforce budget (return False)"
+ )
+
+ def test_unmapped_model_with_litellm_params_pricing(self):
+ """A model with cost=0 in litellm_params (not model_info) should bypass budget."""
+ router = Router(
+ model_list=[
+ {
+ "model_name": "free-via-params",
+ "litellm_params": {
+ "model": "openai/nonexistent-but-free-model",
+ "api_key": "sk-fake",
+ "input_cost_per_token": 0.0,
+ "output_cost_per_token": 0.0,
+ },
+ },
+ ]
+ )
+ result = _is_model_cost_zero(model="free-via-params", llm_router=router)
+ assert result is True, (
+ "Model with explicit cost=0 in litellm_params should bypass budget"
+ )