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
refactor: consolidate duplicate helpers and eliminate success-handler double lookup
Ephrim Stanleye562c1d06402
diff --git a/litellm/proxy/auth/auth_utils.py b/litellm/proxy/auth/auth_utils.pyindex ace39c05ffc..b4e0093b91a 100644--- a/litellm/proxy/auth/auth_utils.py+++ b/litellm/proxy/auth/auth_utils.py@@ -539,15 +539,14 @@ def bytes_to_mb(bytes_value: int):# helpers used by parallel request limiter to handle model rpm/tpm limits for a given api key-def _get_deployment_default_rpm_limit(model_name: str) -> Optional[int]:+def _get_deployment_default_limit(model_name: str, field: str) -> Optional[int]:"""- Return the default_api_key_rpm_limit for model_name.+ Return the minimum value of `field` across all deployments for model_name,+ or None if no deployment has the field set.- When multiple deployments share the same model name, returns the minimum- across all deployments that have the field set. This is the safest choice- for load-balanced setups: it ensures no deployment is over-consumed- regardless of which one actually serves a given request.- Returns None if no deployment has the field set.+ When multiple deployments share the same model name, taking the minimum is+ the safest choice for load-balanced setups: it ensures no deployment is+ over-consumed regardless of which one actually serves a given request."""from litellm.proxy.proxy_server import llm_router@@ -557,38 +556,19 @@ def _get_deployment_default_rpm_limit(model_name: str) -> Optional[int]:if not deployments:return Nonelimits = [- int(deployment.get("litellm_params", {}).get("default_api_key_rpm_limit"))+ int(deployment.get("litellm_params", {}).get(field))for deployment in deployments- if deployment.get("litellm_params", {}).get("default_api_key_rpm_limit")- is not None+ if deployment.get("litellm_params", {}).get(field) is not None]return min(limits) if limits else None-def _get_deployment_default_tpm_limit(model_name: str) -> Optional[int]:- """- Return the default_api_key_tpm_limit for model_name.+def _get_deployment_default_rpm_limit(model_name: str) -> Optional[int]:+ return _get_deployment_default_limit(model_name, "default_api_key_rpm_limit")- When multiple deployments share the same model name, returns the minimum- across all deployments that have the field set. This is the safest choice- for load-balanced setups: it ensures no deployment is over-consumed- regardless of which one actually serves a given request.- Returns None if no deployment has the field set.- """- from litellm.proxy.proxy_server import llm_router- if llm_router is None:- return None- deployments = llm_router.get_model_list(model_name=model_name)- if not deployments:- return None- limits = [- int(deployment.get("litellm_params", {}).get("default_api_key_tpm_limit"))- for deployment in deployments- if deployment.get("litellm_params", {}).get("default_api_key_tpm_limit")- is not None- ]- return min(limits) if limits else None+def _get_deployment_default_tpm_limit(model_name: str) -> Optional[int]:+ return _get_deployment_default_limit(model_name, "default_api_key_tpm_limit")def get_key_model_rpm_limit(diff --git a/litellm/proxy/hooks/parallel_request_limiter.py b/litellm/proxy/hooks/parallel_request_limiter.pyindex 49c6436c22f..55e89e02d67 100644--- a/litellm/proxy/hooks/parallel_request_limiter.py+++ b/litellm/proxy/hooks/parallel_request_limiter.py@@ -539,6 +539,16 @@ class _PROXY_MaxParallelRequestsHandler(CustomLogger):# Update usage - model group + API Key# ------------model_group = get_model_group_from_litellm_kwargs(kwargs)+ _success_tpm_limit = (+ get_key_model_tpm_limit(user_api_key_dict, model_name=model_group)+ if model_group is not None+ else None+ )+ _success_rpm_limit = (+ get_key_model_rpm_limit(user_api_key_dict, model_name=model_group)+ if model_group is not None+ else None+ )if (user_api_key is not Noneand model_group is not None@@ -546,14 +556,8 @@ class _PROXY_MaxParallelRequestsHandler(CustomLogger):"model_rpm_limit" in user_api_key_metadataor "model_tpm_limit" in user_api_key_metadataor user_api_key_model_max_budget is not None- or get_key_model_tpm_limit(- user_api_key_dict, model_name=model_group- )- is not None- or get_key_model_rpm_limit(- user_api_key_dict, model_name=model_group- )- is not None+ or _success_tpm_limit is not None+ or _success_rpm_limit is not None)):request_count_api_key = (diff --git a/tests/test_litellm/proxy/test_model_info_default_limits.py b/tests/test_litellm/proxy/test_model_info_default_limits.pyindex 8b85531785a..d9ebd554edc 100644--- a/tests/test_litellm/proxy/test_model_info_default_limits.py+++ b/tests/test_litellm/proxy/test_model_info_default_limits.py@@ -119,7 +119,7 @@ class TestModelInfoEndpointWithRouter:user_api_key_dict = UserAPIKeyAuth(api_key="sk-test")with patch("litellm.proxy.proxy_server.llm_router", mock_router), \- patch("litellm.proxy.proxy_server.llm_model_list", [{}]), \+ patch("litellm.proxy.proxy_server.llm_model_list", []), \patch("litellm.proxy.proxy_server.user_model", None):response = await model_info_v1(user_api_key_dict=user_api_key_dict,