fix(pricing): prefer the first-party route on equally priced model ties

resolve_litellm_model() returned matches[0] (alphabetical) when several
provider routes shared identical pricing, which resolves bare names like
grok-4.5 to the openrouter aggregator alias instead of the first-party
xai route. On a price tie, prefer the route whose model segment matches
the bare name exactly, keeping alphabetical order as the fallback.

Fixes #1288
This commit is contained in:
helenanova 2026-09-16 02:05:52 -07:00
parent 2dadbb748a
commit e71af8db03

View file

@ -48,7 +48,10 @@ def resolve_litellm_model(model: str) -> str | None:
if isinstance(model_cost.get(key), dict)
}
if len(matches) == 1 or len(prices) == 1:
return matches[0]
# On a price tie, prefer the route whose model segment is exactly the
# bare name (the first-party route) over aggregator aliases like
# openrouter/x-ai/<model>; fall back to alphabetical order.
return min(matches, key=lambda key: (key.split("/", 1)[1] != name, key))
return None # noqa: TRY300
except Exception: # noqa: BLE001
return None