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
fix(adaptive_router): fall back to model_info for cost-weighted scoring
moe-berria00b60933c66
diff --git a/litellm/router.py b/litellm/router.pyindex 6d6efad9f42..5debdeed39a 100644--- a/litellm/router.py+++ b/litellm/router.py@@ -9075,10 +9075,15 @@ class Router:if prefs_raw is not None:model_to_prefs[name] = AdaptiveRouterPreferences(**prefs_raw)- # `input_cost_per_token` is a LiteLLM_Params field per types/router.py.+ # `input_cost_per_token` is a LiteLLM_Params field per types/router.py, but custom+ # pricing is conventionally declared under model_info everywhere else in LiteLLM+ # (cost_calculator.py, add_deployment's litellm.model_cost registration), so fall+ # back to it here too rather than silently reading a zero cost for such deployments.lp = d.get("litellm_params") if isinstance(d, dict) else d.litellm_paramslp_dict: dict[str, Any] = lp if isinstance(lp, dict) else (lp.model_dump() if lp else {})cost = lp_dict.get("input_cost_per_token")+ if cost is None:+ cost = mi_dict.get("input_cost_per_token")if cost is not None:model_to_cost[name] = float(cost)diff --git a/litellm/router_strategy/complexity_router/complexity_router.py b/litellm/router_strategy/complexity_router/complexity_router.pyindex 98a1eb7ac9e..e0353efaefd 100644--- a/litellm/router_strategy/complexity_router/complexity_router.py+++ b/litellm/router_strategy/complexity_router/complexity_router.py@@ -2174,9 +2174,15 @@ class ComplexityRouter(CustomLogger):else:model_to_prefs[name] = AdaptiveRouterPreferences(quality_tier=2, strengths=[])+ # `input_cost_per_token` is a LiteLLM_Params field per types/router.py, but custom+ # pricing is conventionally declared under model_info everywhere else in LiteLLM+ # (cost_calculator.py, add_deployment's litellm.model_cost registration), so fall+ # back to it here too rather than silently costing such a deployment at 0.0.lp = deployment.get("litellm_params") if isinstance(deployment, dict) else deployment.litellm_paramslp_dict: dict[str, Any] = lp if isinstance(lp, dict) else (lp.model_dump() if lp else {})cost = lp_dict.get("input_cost_per_token")+ if cost is None:+ cost = mi_dict.get("input_cost_per_token")model_to_cost[name] = float(cost) if cost is not None else 0.0self.adaptive_router = AdaptiveRouter(diff --git a/tests/test_litellm/router_strategy/adaptive_router/test_router_dispatch.py b/tests/test_litellm/router_strategy/adaptive_router/test_router_dispatch.pyindex a4e803f59ad..68961483ef0 100644--- a/tests/test_litellm/router_strategy/adaptive_router/test_router_dispatch.py+++ b/tests/test_litellm/router_strategy/adaptive_router/test_router_dispatch.py@@ -133,6 +133,64 @@ def test_init_adaptive_router_reads_cost_from_litellm_params():}+def test_init_adaptive_router_falls_back_to_model_info_cost():+ """Custom pricing declared under model_info (the conventional location everywhere else in+ LiteLLM: cost_calculator.py, add_deployment's litellm.model_cost registration) must still+ feed cost-weighted routing, not silently zero it out."""+ r = Router(+ model_list=[+ {+ "model_name": "smart-cheap-router",+ "litellm_params": {+ "model": "auto_router/adaptive_router",+ "adaptive_router_config": {+ "available_models": ["fast", "smart"],+ },+ },+ },+ {+ "model_name": "fast",+ "litellm_params": {"model": "openai/gpt-4o-mini"},+ "model_info": {"input_cost_per_token": 0.00000015},+ },+ {+ "model_name": "smart",+ "litellm_params": {"model": "openai/gpt-4o"},+ "model_info": {"input_cost_per_token": 0.0000050},+ },+ ]+ )+ assert _adaptive(r, "smart-cheap-router").model_to_cost == {+ "fast": 0.00000015,+ "smart": 0.0000050,+ }+++def test_init_adaptive_router_prefers_litellm_params_cost_over_model_info():+ r = Router(+ model_list=[+ {+ "model_name": "smart-cheap-router",+ "litellm_params": {+ "model": "auto_router/adaptive_router",+ "adaptive_router_config": {+ "available_models": ["fast"],+ },+ },+ },+ {+ "model_name": "fast",+ "litellm_params": {+ "model": "openai/gpt-4o-mini",+ "input_cost_per_token": 0.00000015,+ },+ "model_info": {"input_cost_per_token": 0.0000050},+ },+ ]+ )+ assert _adaptive(r, "smart-cheap-router").model_to_cost == {"fast": 0.00000015}++# ---- Fix 4: pre-routing dispatch ---------------------------------------diff --git a/tests/test_litellm/router_strategy/test_complexity_router.py b/tests/test_litellm/router_strategy/test_complexity_router.pyindex 918ec7bc100..e6186ae50db 100644--- a/tests/test_litellm/router_strategy/test_complexity_router.py+++ b/tests/test_litellm/router_strategy/test_complexity_router.py@@ -1512,6 +1512,70 @@ class TestRouterComplexityDeploymentMethods:assert adaptive.model_to_prefs["cheap"].quality_tier == 1assert adaptive.model_to_prefs["premium"].quality_tier == 3+ def test_hybrid_adaptive_router_falls_back_to_model_info_cost(self):+ """Custom pricing declared under model_info (the conventional location everywhere else+ in LiteLLM) must still feed the hybrid adaptive router's cost-weighted scoring, not+ silently cost the deployment at 0.0."""+ router = Router(+ model_list=[+ {+ "model_name": "hybrid",+ "litellm_params": {+ "model": "auto_router/complexity_router",+ "complexity_router_default_model": "cheap",+ "complexity_router_config": {+ "adaptive": True,+ "tiers": {"SIMPLE": ["cheap"], "MEDIUM": ["cheap", "premium"]},+ },+ },+ },+ {+ "model_name": "cheap",+ "litellm_params": {"model": "openai/gpt-4o-mini"},+ "model_info": {"input_cost_per_token": 0.00000015},+ },+ {+ "model_name": "premium",+ "litellm_params": {"model": "openai/gpt-4o"},+ "model_info": {"input_cost_per_token": 0.000005},+ },+ ]+ )++ adaptive = router.adaptive_routers["hybrid"][0].strategy+ assert adaptive.model_to_cost == {+ "cheap": pytest.approx(0.00000015),+ "premium": pytest.approx(0.000005),+ }++ def test_hybrid_adaptive_router_prefers_litellm_params_cost_over_model_info(self):+ router = Router(+ model_list=[+ {+ "model_name": "hybrid",+ "litellm_params": {+ "model": "auto_router/complexity_router",+ "complexity_router_default_model": "cheap",+ "complexity_router_config": {+ "adaptive": True,+ "tiers": {"SIMPLE": ["cheap"]},+ },+ },+ },+ {+ "model_name": "cheap",+ "litellm_params": {+ "model": "openai/gpt-4o-mini",+ "input_cost_per_token": 0.00000015,+ },+ "model_info": {"input_cost_per_token": 0.000005},+ },+ ]+ )++ adaptive = router.adaptive_routers["hybrid"][0].strategy+ assert adaptive.model_to_cost == {"cheap": pytest.approx(0.00000015)}+class TestComplexityRouterTagBasedRouting:"""Regression tests for https://github.com/BerriAI/litellm/issues/33655.