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(proxy): treat non-token pricing as priced and propagate the unpriced-model toggle across workers

mateo84c41dcdc3e5
diff --git a/litellm/constants.py b/litellm/constants.py
index 1014b472c61..b0ff992931f 100644
--- a/litellm/constants.py
+++ b/litellm/constants.py
@@ -1525,6 +1525,7 @@ LITELLM_SETTINGS_SAFE_DB_OVERRIDES = [
"public_model_groups_links",
"cost_discount_config",
"cost_margin_config",
+ "block_requests_for_models_without_pricing",
"budget_exceeded_throttle_percentage",
# Every field editable from the Admin UI (proxy_server._GENERAL_SETTINGS_UI_LITELLM_FIELDS)
# must be listed here so a DB write from one worker overrides the live litellm attribute on
diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py
index 11a45e78410..136b8d19c04 100644
--- a/litellm/proxy/auth/auth_checks.py
+++ b/litellm/proxy/auth/auth_checks.py
@@ -13,7 +13,18 @@ import asyncio
import math
import re
import time
-from typing import TYPE_CHECKING, Any, Dict, List, Literal, Optional, Type, Union, cast
+from typing import (
+ TYPE_CHECKING,
+ Any,
+ Dict,
+ List,
+ Literal,
+ Mapping,
+ Optional,
+ Type,
+ Union,
+ cast,
+)
from fastapi import HTTPException, Request, status
from pydantic import BaseModel
@@ -274,17 +285,52 @@ def _is_cost_explicitly_configured(model: str, llm_router: "Router") -> bool:
return False
+def _has_positive_cost(value: object) -> bool:
+ if isinstance(value, bool):
+ return False
+ if isinstance(value, (int, float)):
+ return value > 0
+ if isinstance(value, dict):
+ return any(_has_positive_cost(nested) for nested in value.values())
+ return False
+
+
+def _entry_has_priced_metric(entry: Mapping[str, object]) -> bool:
+ return any("cost_per" in key and _has_positive_cost(value) for key, value in entry.items())
+
+
+def _model_group_has_pricing(model: str, llm_router: "Router") -> bool:
+ """
+ Check every deployment behind a model group for a positive price on any billed
+ metric (tokens, characters, seconds, pages, images, queries, ...), so models that
+ are billed by a non-token metric are not treated as unpriced.
+ """
+ for deployment in llm_router.get_model_list(model_name=model) or []:
+ litellm_params = deployment.get("litellm_params") or {}
+ if _entry_has_priced_metric(litellm_params):
+ return True
+
+ model_id = (deployment.get("model_info") or {}).get("id")
+ if model_id is None:
+ continue
+
+ model_info = llm_router.get_deployment_model_info(
+ model_id=model_id, model_name=litellm_params.get("model") or ""
+ )
+ if model_info is not None and _entry_has_priced_metric(model_info):
+ return True
+
+ return False
+
+
def model_has_no_cost_mapping(model: Optional[str], llm_router: Optional[Router]) -> bool:
if not model or llm_router is None:
return False
- model_group_info = llm_router.get_model_group_info(model_group=model)
- if model_group_info is None:
+ if llm_router.get_model_group_info(model_group=model) is None:
return False
- input_cost = model_group_info.input_cost_per_token or 0
- output_cost = model_group_info.output_cost_per_token or 0
- if input_cost > 0 or output_cost > 0:
+ if _model_group_has_pricing(model=model, llm_router=llm_router):
return False
return not _is_cost_explicitly_configured(model, llm_router)
diff --git a/tests/test_litellm/proxy/auth/test_auth_checks.py b/tests/test_litellm/proxy/auth/test_auth_checks.py
index aec0ddc55f8..a078e041aa7 100644
--- a/tests/test_litellm/proxy/auth/test_auth_checks.py
+++ b/tests/test_litellm/proxy/auth/test_auth_checks.py
@@ -5165,6 +5165,7 @@ async def test_get_project_object_db_fetch_returns_cached_obj():
assert isinstance(result, LiteLLM_ProjectTableCachedObj)
assert result.project_id == "p-1"
+ assert result.project_alias == "proj"
UNPRICED_UNDERLYING_MODEL = "openai/unpriced-model-lit4984-xyz"
@@ -5212,6 +5213,50 @@ def test_model_has_no_cost_mapping_no_model_or_router_is_false():
assert model_has_no_cost_mapping(model="unpriced-group", llm_router=None) is False
+ "underlying_model",
+ [
+ "azure/speech/azure-tts",
+ "mistral/mistral-ocr-latest",
+ "vertex_ai/imagen-3.0-generate-001",
+ ],
+)
+def test_model_has_no_cost_mapping_non_token_priced_model_is_false(underlying_model):
+ from litellm.proxy.auth.auth_checks import model_has_no_cost_mapping
+ from litellm.router import Router
+
+ router = Router(
+ model_list=[
+ {
+ "model_name": "non-token-priced-group",
+ "litellm_params": {"model": underlying_model, "api_key": "sk-test"},
+ }
+ ]
+ )
+
+ assert model_has_no_cost_mapping(model="non-token-priced-group", llm_router=router) is False
+
+
+def test_model_has_no_cost_mapping_non_token_price_from_litellm_params_is_false():
+ from litellm.proxy.auth.auth_checks import model_has_no_cost_mapping
+ from litellm.router import Router
+
+ router = Router(
+ model_list=[
+ {
+ "model_name": "custom-tts",
+ "litellm_params": {
+ "model": UNPRICED_UNDERLYING_MODEL,
+ "api_key": "sk-test",
+ "input_cost_per_second": 0.0001,
+ },
+ }
+ ]
+ )
+
+ assert model_has_no_cost_mapping(model="custom-tts", llm_router=router) is False
+
+
async def _run_common_checks(
model: Optional[str], llm_router: Optional["Router"], route: str = "/chat/completions"
) -> bool:
diff --git a/tests/test_litellm/proxy/management_endpoints/test_cost_tracking_settings.py b/tests/test_litellm/proxy/management_endpoints/test_cost_tracking_settings.py
index 4fb90e9fb2d..1124a4a31d4 100644
--- a/tests/test_litellm/proxy/management_endpoints/test_cost_tracking_settings.py
+++ b/tests/test_litellm/proxy/management_endpoints/test_cost_tracking_settings.py
@@ -541,6 +541,20 @@ class TestBlockRequestsForModelsWithoutPricing:
saved_config = mock_proxy_config.save_config.call_args.kwargs["new_config"]
assert saved_config["litellm_settings"]["block_requests_for_models_without_pricing"] is True
+ def test_peer_workers_pick_up_persisted_flag_on_config_reload(self):
+ """A PATCH only mutates the flag on the worker that served it; peer workers must pick the
+ persisted value up when they reload litellm_settings from the DB."""
+ from litellm.proxy.proxy_server import ProxyConfig
+
+ with patch.object(litellm, "block_requests_for_models_without_pricing", False):
+ ProxyConfig()._update_config_fields(
+ current_config={},
+ param_name="litellm_settings",
+ db_param_value={"block_requests_for_models_without_pricing": True},
+ )
+
+ assert litellm.block_requests_for_models_without_pricing is True
+
@pytest.mark.asyncio
async def test_patch_requires_store_model_in_db(self):
with (