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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
perf(savings): read deployment pricing from the router's cached model info (#47400)
Nate Armstrong497f31d43abb
diff --git a/litellm/proxy/spend_tracking/savings.py b/litellm/proxy/spend_tracking/savings.pyindex bca91d710e1..2aab8deb034 100644--- a/litellm/proxy/spend_tracking/savings.py+++ b/litellm/proxy/spend_tracking/savings.py@@ -90,7 +90,7 @@ def _effective_model_info(router: "Router | None", deployment_id: str | None, moif router is None or deployment_id is None:return Nonetry:- return router.get_deployment_model_info(deployment_id, model)+ return router.cached_deployment_model_info(deployment_id, model)except Exception as e: # noqa: BLE001 # a dashboard metric must not fail the spend writeverbose_proxy_logger.debug("savings: no deployment pricing for %s (%s)", model, e)return Nonediff --git a/litellm/router_strategy/savings_baseline.py b/litellm/router_strategy/savings_baseline.pyindex a2e983a8369..b5b80ddae6d 100644--- a/litellm/router_strategy/savings_baseline.py+++ b/litellm/router_strategy/savings_baseline.py@@ -115,7 +115,7 @@ def _priced(router: "Router", candidate: Baseline) -> tuple[float, Baseline] | Nprovider, _, model_name = candidate.model.partition("/")try:- info: Final = router.get_deployment_model_info(candidate.deployment_id or "", candidate.model)+ info: Final = router.cached_deployment_model_info(candidate.deployment_id or "", candidate.model)if info is None:return Noneprompt_cost, completion_cost = generic_cost_per_token(diff --git a/tests/unit/proxy/spend_tracking/test_savings.py b/tests/unit/proxy/spend_tracking/test_savings.pyindex de413a86521..f9566fd1ba0 100644--- a/tests/unit/proxy/spend_tracking/test_savings.py+++ b/tests/unit/proxy/spend_tracking/test_savings.py@@ -14,6 +14,7 @@ from litellm.proxy.spend_tracking.savings import (prompt_caching_savings_for_request,)from litellm.router import Router+from litellm.types.router import Deployment, LiteLLM_Paramsfrom litellm.types.utils import Usagepytestmark = pytest.mark.usefixtures("local_model_cost_map")@@ -1070,6 +1071,31 @@ def test_a_recorded_baseline_deployment_prices_at_its_configured_rate():assert with_deployment_rate.autorouter > at_public_rate.autorouter+def test_savings_follow_a_deployments_updated_price() -> None:+ def deployment(input_cost_per_token: float) -> Deployment:+ return Deployment(+ model_name="negotiated",+ litellm_params=LiteLLM_Params(+ model="anthropic/claude-sonnet-5", input_cost_per_token=input_cost_per_token+ ),+ model_info={"id": "repriced"},+ )++ router: Final = Router(model_list=[deployment(1e-6).model_dump(exclude_none=True)])++ def compression_savings() -> float:+ return compute_savings_spend(+ "claude-sonnet-5", "anthropic", 1000, False, model_id="repriced", llm_router=lambda: router+ ).compression++ assert compression_savings() == pytest.approx(1000 * 1e-6)+ assert compression_savings() == pytest.approx(1000 * 1e-6)++ router.upsert_deployment(deployment(3e-6))++ assert compression_savings() == pytest.approx(1000 * 3e-6)++def _routed_decision() -> dict:return {"savings_baseline_model": "anthropic/claude-opus-5", "conversation_continuing": True}