From db2b0ab08521317c77e9f0de277f88b7d9ff09c3 Mon Sep 17 00:00:00 2001 From: Alexsander Hamir Date: Tue, 13 Jan 2026 14:16:46 -0800 Subject: [PATCH] Fix stale cache issue in _get_model_cost_key - Add verification that matched key exists in model_cost before returning - Prevents KeyError when model_cost is modified directly (e.g., via .pop()) - Handles edge case where lowercase map cache becomes stale if model_cost is modified outside of our invalidation mechanism (register_model, proxy_server) --- litellm/utils.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/litellm/utils.py b/litellm/utils.py index 1e52de1ba1f..213af89e8c8 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -5027,7 +5027,14 @@ def _get_model_cost_key(potential_key: str) -> Optional[str]: _model_cost_lowercase_map = {k.lower(): k for k in litellm.model_cost} potential_key_lower = potential_key.lower() - return _model_cost_lowercase_map.get(potential_key_lower) + matched_key = _model_cost_lowercase_map.get(potential_key_lower) + + # Verify the matched key still exists in model_cost (defense against stale cache) + # This handles cases where model_cost is modified directly (e.g., model_cost.pop()) + if matched_key is not None and matched_key in litellm.model_cost: + return matched_key + + return None