mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-25 22:20:43 +00:00
* fix(router): re-check and reserve per-model rate limits on fallback targets A provider error fallback handed the caller the fallback model group without its per-model TPM/RPM limits ever being checked. The router gets a fallback_rate_limit_check hook next to fallback_budget_check, the proxy wires it to the v3 limiter, which reserves the target model's per-model descriptors before each fallback hop and skips targets that are over their limit. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(router): dedupe a repeated fallback target before the reserving rate limit check Both walks now consult the attempted set before the rate limit check, so a chain like ["B", "B"] reserves B once. The deprecated settings parity test also learns the new setting. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(router): a bidirectional fallback chain never re-reserves the requested model group Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(router): match the surfaced failure in the fallback reservation tests Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(rate-limit): keep the admission stash untouched while building fallback descriptors Building the fallback target's descriptors could rewrite the request stash's PTU ceiling, which post-call settlement reads for the model the caller named. The fallback check now hides the stash while it builds, like the frame check. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
77 lines
3.5 KiB
Python
77 lines
3.5 KiB
Python
from typing import Final
|
|
|
|
import pytest
|
|
|
|
from litellm import Router
|
|
from litellm.caching.dual_cache import DualCache
|
|
from litellm.proxy._types import UserAPIKeyAuth
|
|
from litellm.proxy.auth.fallback_rate_limit import RouterFallbackRateLimitCheck
|
|
from litellm.proxy.hooks.parallel_request_limiter_v3 import PROXY_MaxParallelRequestsHandler_v3
|
|
from litellm.proxy.utils import InternalUsageCache
|
|
from litellm.router_api import RoutingContext
|
|
|
|
PRIMARY: Final = {"model_name": "primary", "litellm_params": {"model": "openai/gpt-4o", "api_key": "k"}}
|
|
FALLBACK: Final = {"model_name": "fallback", "litellm_params": {"model": "openai/gpt-4o", "api_key": "k"}}
|
|
|
|
|
|
def _router() -> Router:
|
|
return Router(model_list=[PRIMARY, FALLBACK], fallbacks=[{"primary": ["fallback"]}])
|
|
|
|
|
|
def _limiter() -> PROXY_MaxParallelRequestsHandler_v3:
|
|
return PROXY_MaxParallelRequestsHandler_v3(internal_usage_cache=InternalUsageCache(DualCache()))
|
|
|
|
|
|
def _request(token: UserAPIKeyAuth) -> dict[str, object]:
|
|
return {
|
|
"model": "primary",
|
|
"messages": [{"role": "user", "content": "hi"}],
|
|
"metadata": {"user_api_key_auth": token},
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_fallback_target_is_admitted_until_the_keys_per_model_rpm_limit_is_spent() -> None:
|
|
limiter: Final = _limiter()
|
|
check: Final = RouterFallbackRateLimitCheck(limiter=lambda: limiter)
|
|
token: Final = UserAPIKeyAuth(api_key="hashed", token="hashed", metadata={"model_rpm_limit": {"fallback": 2}})
|
|
ctx: Final = RoutingContext(_router())
|
|
verdicts: Final = tuple([await check(model="fallback", request_kwargs=_request(token), ctx=ctx) for _ in range(3)])
|
|
assert verdicts == (True, True, False)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_fallback_target_without_a_per_model_limit_is_always_admitted() -> None:
|
|
limiter: Final = _limiter()
|
|
check: Final = RouterFallbackRateLimitCheck(limiter=lambda: limiter)
|
|
token: Final = UserAPIKeyAuth(api_key="hashed", token="hashed", metadata={"model_rpm_limit": {"other": 1}})
|
|
ctx: Final = RoutingContext(_router())
|
|
verdicts: Final = tuple([await check(model="fallback", request_kwargs=_request(token), ctx=ctx) for _ in range(3)])
|
|
assert verdicts == (True, True, True)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_request_without_a_key_is_unrestricted() -> None:
|
|
check: Final = RouterFallbackRateLimitCheck(limiter=_limiter)
|
|
assert await check(model="fallback", request_kwargs={"model": "primary"}, ctx=RoutingContext(_router())) is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_proxy_without_the_v3_limiter_is_unrestricted() -> None:
|
|
check: Final = RouterFallbackRateLimitCheck(limiter=lambda: None)
|
|
token: Final = UserAPIKeyAuth(api_key="hashed", token="hashed", metadata={"model_rpm_limit": {"fallback": 0}})
|
|
assert await check(model="fallback", request_kwargs=_request(token), ctx=RoutingContext(_router())) is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_a_failing_limiter_fails_closed() -> None:
|
|
class BrokenLimiter(PROXY_MaxParallelRequestsHandler_v3):
|
|
async def async_fallback_target_allowed(
|
|
self, user_api_key_dict: object, model: str, request_data: object
|
|
) -> bool:
|
|
raise RuntimeError("redis down")
|
|
|
|
limiter: Final = BrokenLimiter(internal_usage_cache=InternalUsageCache(DualCache()))
|
|
check: Final = RouterFallbackRateLimitCheck(limiter=lambda: limiter)
|
|
token: Final = UserAPIKeyAuth(api_key="hashed", token="hashed", metadata={"model_rpm_limit": {"fallback": 5}})
|
|
assert await check(model="fallback", request_kwargs=_request(token), ctx=RoutingContext(_router())) is False
|