litellm/litellm/proxy/route_priority.py
devin-ai-integration[bot] 9316b4194a
perf(proxy): register liveness and core inference routes first (#40687)
Starlette scans the route table in registration order, so a request pays one
regex match per route registered ahead of its own. The proxy registers several
hundred routes and left the liveness probe near position 280 and the lazy
loaded /v1/messages at the very end. Move /health/liveliness, /health/liveness,
/v1/chat/completions, /chat/completions and /v1/messages to the front of the
route table after startup registration and again after a lazy router loads.

Co-authored-by: yassin <yassin@berri.ai>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-09-11 17:10:38 +00:00

24 lines
722 B
Python

"""Starlette matches routes in registration order, so the routes that take the most traffic go first."""
from collections.abc import Sequence
from typing import Final
from starlette.routing import BaseRoute, Route
HOT_ROUTE_PATHS: Final[frozenset[str]] = frozenset(
(
"/health/liveliness",
"/health/liveness",
"/v1/chat/completions",
"/chat/completions",
"/v1/messages",
)
)
def _is_hot(route: BaseRoute) -> bool:
return isinstance(route, Route) and route.path in HOT_ROUTE_PATHS
def hot_routes_first(routes: Sequence[BaseRoute]) -> list[BaseRoute]: # mutable-ok: assigned to Router.routes, a list
return sorted(routes, key=lambda route: not _is_hot(route))