From d77d525ce770558a5ef8242f68fe5e87fba359c8 Mon Sep 17 00:00:00 2001 From: ryan-crabbe <128659760+ryan-crabbe@users.noreply.github.com> Date: Sat, 7 Feb 2026 09:18:57 -0800 Subject: [PATCH] perf: add LRU cache to normalize_request_route (#19812) Add @lru_cache(maxsize=256) to eliminate redundant regex work for repeated routes. Reduces time from 1.04s to ~0s for 6,006 calls. --- litellm/proxy/auth/auth_utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/auth/auth_utils.py b/litellm/proxy/auth/auth_utils.py index 2bd84a1d98..fbfc88228c 100644 --- a/litellm/proxy/auth/auth_utils.py +++ b/litellm/proxy/auth/auth_utils.py @@ -1,6 +1,7 @@ import os import re import sys +from functools import lru_cache from typing import Any, List, Optional, Tuple from fastapi import HTTPException, Request, status @@ -311,10 +312,11 @@ def get_request_route(request: Request) -> str: return request.url.path +@lru_cache(maxsize=256) def normalize_request_route(route: str) -> str: """ Normalize request routes by replacing dynamic path parameters with placeholders. - + This prevents high cardinality in Prometheus metrics by collapsing routes like: - /v1/responses/1234567890 -> /v1/responses/{response_id} - /v1/threads/thread_123 -> /v1/threads/{thread_id}