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
Warn when budget lookup fails; cache won't populate (#20545)
Alexsander Hamir09fb6d0087ab
diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.pyindex 359bb944546..92e98d6446e 100644--- a/litellm/proxy/auth/auth_checks.py+++ b/litellm/proxy/auth/auth_checks.py@@ -75,6 +75,28 @@ db_cache_expiry = DEFAULT_IN_MEMORY_TTL # refresh every 5sall_routes = LiteLLMRoutes.openai_routes.value + LiteLLMRoutes.management_routes.value+def _log_budget_lookup_failure(entity: str, error: Exception) -> None:+ """+ Log a warning when budget lookup fails; cache will not be populated.++ Skips logging for expected "user not found" cases (bare Exception from+ get_user_object when user_id_upsert=False). Adds a schema migration hint+ when the error appears schema-related.+ """+ # Skip logging for expected "user not found" - not caching is correct+ if str(error) == "" and type(error).__name__ == "Exception":+ return+ err_str = str(error).lower()+ hint = ""+ if any(+ x in err_str+ for x in ("column", "schema", "does not exist", "prisma", "migrate")+ ):+ hint = " Run `prisma db push` or `prisma migrate deploy` to fix schema mismatches."+ verbose_proxy_logger.error(+ f"Budget lookup failed for {entity}; cache will not be populated. "+ f"Each request will hit the database. Error: {error}.{hint}"+ )def _is_model_cost_zero(model: Optional[Union[str, List[str]]], llm_router: Optional[Router]@@ -1208,6 +1230,7 @@ async def get_user_object(return _responseexcept Exception as e: # if user not in db+ _log_budget_lookup_failure("user", e)raise ValueError(f"User doesn't exist in db. 'user_id'={user_id}. Create user via `/user/new` call. Got error - {e}")diff --git a/tests/test_litellm/proxy/auth/test_auth_checks.py b/tests/test_litellm/proxy/auth/test_auth_checks.pyindex ebcd9676129..4f8e80c023e 100644--- a/tests/test_litellm/proxy/auth/test_auth_checks.py+++ b/tests/test_litellm/proxy/auth/test_auth_checks.py@@ -30,6 +30,7 @@ from litellm.proxy.auth.auth_checks import (_can_object_call_vector_stores,_get_fuzzy_user_object,_get_team_db_check,+ _log_budget_lookup_failure,_virtual_key_max_budget_alert_check,_virtual_key_soft_budget_check,get_user_object,@@ -273,6 +274,27 @@ async def test_default_internal_user_params_with_get_user_object(monkeypatch):assert creation_args["user_role"] == "internal_user"+def test_log_budget_lookup_failure_dry_run():+ """Dry run: verify _log_budget_lookup_failure logs for schema/DB errors."""+ with patch("litellm.proxy.auth.auth_checks.verbose_proxy_logger") as mock_logger:+ err = Exception("column 'policies' does not exist in prisma schema")+ _log_budget_lookup_failure("user", err)+ mock_logger.error.assert_called_once()+ call_msg = mock_logger.error.call_args[0][0]+ assert "user" in call_msg+ assert "cache will not be populated" in call_msg+ assert "policies" in call_msg or "prisma" in call_msg+ assert "prisma db push" in call_msg+++def test_log_budget_lookup_failure_skips_user_not_found():+ """Verify _log_budget_lookup_failure does NOT log for expected user-not-found."""+ with patch("litellm.proxy.auth.auth_checks.verbose_proxy_logger") as mock_logger:+ err = Exception() # bare Exception from get_user_object when user not found+ _log_budget_lookup_failure("user", err)+ mock_logger.error.assert_not_called()++@pytest.mark.asyncio@patch("litellm.proxy.management_endpoints.team_endpoints.new_team", new_callable=AsyncMock)async def test_get_team_db_check_calls_new_team_on_upsert(mock_new_team, monkeypatch):