Skip to content

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]

fix(logging): merge deployment pricing onto a copy of the cached model info

mateo-berri8a43a8c7e30f
diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py
index 96d9aa744fd..f59cd966261 100644
--- a/litellm/litellm_core_utils/litellm_logging.py
+++ b/litellm/litellm_core_utils/litellm_logging.py
@@ -626,7 +626,7 @@ class Logging(LiteLLMLoggingBaseClass):
):
return None
try:
- merged: Final = litellm.get_model_info(model=model_id)
+ merged: Final = litellm.get_model_info(model=model_id).copy()
except Exception: # noqa: BLE001 # get_model_info raises for ids it cannot resolve a provider for
return None
published: Final = self._published_model_info()
diff --git a/tests/test_litellm/litellm_core_utils/test_litellm_logging.py b/tests/test_litellm/litellm_core_utils/test_litellm_logging.py
index 007617c309d..946f19b7658 100644
--- a/tests/test_litellm/litellm_core_utils/test_litellm_logging.py
+++ b/tests/test_litellm/litellm_core_utils/test_litellm_logging.py
@@ -478,6 +478,38 @@ class TestGetRouterDeploymentModelInfo:
finally:
litellm.model_cost.pop(deployment_id, None)
+ def test_merging_does_not_mutate_the_cached_model_info(self) -> None:
+ """The published-rate merge must not write into get_model_info's lru-cached dict.
+
+ get_model_info returns the same cached object on every call, so writing
+ the published rates into it poisoned every later lookup of the
+ deployment id for the life of the process.
+ """
+ from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
+
+ model = "bedrock/global.anthropic.claude-sonnet-4-6"
+ deployment_id = "deploy-cache-not-poisoned-1"
+ litellm.model_cost[deployment_id] = {"id": deployment_id, "input_cost_per_token": 1e-06}
+ obj = LiteLLMLoggingObj(
+ model=model,
+ messages=[],
+ stream=False,
+ call_type="aretrieve_batch",
+ start_time=time.time(),
+ litellm_call_id="cache-not-poisoned",
+ function_id="f",
+ )
+ obj.litellm_params = {"litellm_metadata": {"model_info": {"id": deployment_id}}, "model": model}
+ obj.model_call_details["model"] = model
+ try:
+ cached_before = dict(litellm.get_model_info(model=deployment_id))
+ info = obj.get_router_deployment_model_info()
+ assert info is not None
+ assert info["output_cost_per_token"] == 1.5e-05
+ assert dict(litellm.get_model_info(model=deployment_id)) == cached_before
+ finally:
+ litellm.model_cost.pop(deployment_id, None)
+
def test_keeps_declared_rates_when_no_model_is_resolvable(self, logging_obj) -> None:
"""With no model to look a published entry up by, the declared rates stand alone."""
deployment_id = "deploy-no-model-at-all-1"