From 8dd25b9e661a7cc4ef76e72f693d33e1256889c4 Mon Sep 17 00:00:00 2001 From: LancyZhao <207653224+LancyZhao@users.noreply.github.com> Date: Mon, 24 Aug 2026 17:16:04 +0800 Subject: [PATCH] fix(router): break cost-based-routing input+output ties on cache-read price --- litellm/router_strategy/lowest_cost.py | 9 +++- .../local_testing/test_lowest_cost_routing.py | 49 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/litellm/router_strategy/lowest_cost.py b/litellm/router_strategy/lowest_cost.py index b927df0c438..cdcccf91921 100644 --- a/litellm/router_strategy/lowest_cost.py +++ b/litellm/router_strategy/lowest_cost.py @@ -262,6 +262,11 @@ class LowestCostLoggingHandler(CustomLogger): if item_output_cost is None: item_output_cost = item_litellm_model_cost_map.get("output_cost_per_token", 5.0) + # Secondary ranking signal used to break ties on total input+output price. + # Fall back to the input cost when a model has no cache-read price, so models + # missing that field are not ranked as if their cache reads were free. + item_cache_read_cost = item_litellm_model_cost_map.get("cache_read_input_token_cost", item_input_cost) + # if litellm["model"] is not in model_cost map -> use item_cost = $10 item_cost = item_input_cost + item_output_cost @@ -294,12 +299,12 @@ class LowestCostLoggingHandler(CustomLogger): ): # if user passed in tpm / rpm in the model_list continue else: - potential_deployments.append((_deployment, item_cost)) + potential_deployments.append((_deployment, item_cost, item_cache_read_cost)) if len(potential_deployments) == 0: return None - potential_deployments = sorted(potential_deployments, key=lambda x: x[1]) + potential_deployments = sorted(potential_deployments, key=lambda x: (x[1], x[2])) selected_deployment: Final = potential_deployments[0][0] return selected_deployment diff --git a/tests/local_testing/test_lowest_cost_routing.py b/tests/local_testing/test_lowest_cost_routing.py index 5bf3a3ee98b..4ad33f8db42 100644 --- a/tests/local_testing/test_lowest_cost_routing.py +++ b/tests/local_testing/test_lowest_cost_routing.py @@ -199,3 +199,52 @@ async def test_get_available_endpoints_tpm_rpm_check_async(ans_rpm): assert (d_ans and d_ans["model_info"]["id"]) == ans print("selected deployment:", d_ans) + + +@pytest.mark.parametrize("cheaper_cache_first", [True, False]) +@pytest.mark.asyncio +async def test_lowest_cost_routing_breaks_input_output_tie_on_cache_read_cost( + cheaper_cache_first, +): + """ + Regression test for https://github.com/BerriAI/litellm/issues/38064 + + When two deployments tie on input + output price, cost-based routing must + fall back to the cache-read price instead of returning whichever deployment + happens to be listed first in the model_list. + """ + import litellm + + cheaper_cache = "tencent/deepseek-v4-flash" # lower cache_read_input_token_cost + pricier_cache = "fireworks_ai/deepseek-v4-flash" + + # The assertion is only meaningful while the shipped price map still ties these + # two models on input+output and separates them on cache-read price. + cheap = litellm.model_cost[cheaper_cache] + pricey = litellm.model_cost[pricier_cache] + assert ( + cheap["input_cost_per_token"] + cheap["output_cost_per_token"] + == pricey["input_cost_per_token"] + pricey["output_cost_per_token"] + ), "precondition: the two deployments must tie on input+output price" + assert ( + cheap["cache_read_input_token_cost"] < pricey["cache_read_input_token_cost"] + ), "precondition: cheaper_cache must have the lower cache-read price" + + ordered = [cheaper_cache, pricier_cache] if cheaper_cache_first else [pricier_cache, cheaper_cache] + model_list = [ + { + "model_name": "deepseek-v4-flash", + "litellm_params": {"model": model}, + "model_info": {"id": model}, + } + for model in ordered + ] + + lowest_cost_logger = LowestCostLoggingHandler(router_cache=DualCache()) + + selected = await lowest_cost_logger.async_get_available_deployments( + model_group="deepseek-v4-flash", healthy_deployments=model_list + ) + + # Cheaper cache-read deployment must win regardless of model_list ordering. + assert selected["model_info"]["id"] == cheaper_cache