mirror of
https://github.com/usestrix/strix.git
synced 2026-11-18 09:42:48 +00:00
fix(pricing): only prefer the native route when every route prices the model the same
Review feedback on #1279: returning the native listing before the price check turned bare names whose routes disagree on price from None into a guess (91 of 1993 bare names on litellm 1.101). Move the preference inside the branch that already accepts the match (one route, or all routes at one price), so it only breaks ties the resolver was willing to resolve and changes nothing for differently priced routes. The guard test now uses fixtures with litellm_provider, as every real catalog entry has, and the test that asserted the wide behavior asserts None instead. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TavwVMH7PvrD68JTDfd6Hb
This commit is contained in:
parent
2b14448b64
commit
61baca5336
2 changed files with 22 additions and 24 deletions
|
|
@ -7,9 +7,7 @@ from typing import Any, cast
|
|||
|
||||
|
||||
@lru_cache(maxsize=512)
|
||||
def resolve_litellm_model( # noqa: PLR0911 - each resolution path returns its own name
|
||||
model: str,
|
||||
) -> str | None:
|
||||
def resolve_litellm_model(model: str) -> str | None:
|
||||
"""Return a provider-qualified model name that LiteLLM can price."""
|
||||
try:
|
||||
import litellm
|
||||
|
|
@ -41,22 +39,6 @@ def resolve_litellm_model( # noqa: PLR0911 - each resolution path returns its o
|
|||
matches = sorted(key for key in model_cost if key.endswith(f"/{name}"))
|
||||
if not matches:
|
||||
continue
|
||||
# Prefer the first-party provider's own listing over an aggregator's
|
||||
# re-listing of the same model. Aggregators (OpenRouter, DeepInfra,
|
||||
# Perplexity, ...) re-key a model under a namespaced path such as
|
||||
# "openrouter/x-ai/grok-4.5"; the native listing is the one whose key
|
||||
# is exactly "<litellm_provider>/<name>" ("xai/grok-4.5"). When that
|
||||
# is unique it is the canonical name, so tie-break on it before
|
||||
# falling back to price consensus (which would otherwise pick the
|
||||
# alphabetically first key, an aggregator).
|
||||
native = [
|
||||
key
|
||||
for key in matches
|
||||
if isinstance(model_cost.get(key), dict)
|
||||
and key == f"{model_cost[key].get('litellm_provider')}/{name}"
|
||||
]
|
||||
if len(native) == 1:
|
||||
return native[0]
|
||||
prices = {
|
||||
(
|
||||
model_cost[key].get("input_cost_per_token"),
|
||||
|
|
@ -66,7 +48,18 @@ def resolve_litellm_model( # noqa: PLR0911 - each resolution path returns its o
|
|||
if isinstance(model_cost.get(key), dict)
|
||||
}
|
||||
if len(matches) == 1 or len(prices) == 1:
|
||||
return matches[0]
|
||||
# Every route prices the model the same, so picking one is free; pick
|
||||
# the first-party listing over an aggregator's re-listing. Aggregators
|
||||
# (OpenRouter, DeepInfra, ...) re-key a model under a namespaced path
|
||||
# such as "openrouter/x-ai/grok-4.5", which sorts ahead of the native
|
||||
# "xai/grok-4.5", so the alphabetically first key would be misleading.
|
||||
native = [
|
||||
key
|
||||
for key in matches
|
||||
if isinstance(model_cost.get(key), dict)
|
||||
and key == f"{model_cost[key].get('litellm_provider')}/{name}"
|
||||
]
|
||||
return native[0] if len(native) == 1 else matches[0]
|
||||
return None # noqa: TRY300
|
||||
except Exception: # noqa: BLE001
|
||||
return None
|
||||
|
|
|
|||
|
|
@ -105,12 +105,16 @@ def test_resolver_uses_provider_when_bare_entry_has_one() -> None:
|
|||
|
||||
def test_resolver_does_not_guess_between_differently_priced_providers() -> None:
|
||||
original = litellm.model_cost
|
||||
# Real catalog entries always carry ``litellm_provider``; without it the native
|
||||
# route could never match and this guard would not exercise the preference.
|
||||
litellm.model_cost = {
|
||||
"provider-a/example": {
|
||||
"litellm_provider": "provider-a",
|
||||
"input_cost_per_token": 1.0,
|
||||
"output_cost_per_token": 2.0,
|
||||
},
|
||||
"provider-b/example": {
|
||||
"litellm_provider": "provider-b",
|
||||
"input_cost_per_token": 3.0,
|
||||
"output_cost_per_token": 4.0,
|
||||
},
|
||||
|
|
@ -148,9 +152,10 @@ def test_resolver_prefers_native_provider_over_aggregator() -> None:
|
|||
resolve_litellm_model.cache_clear()
|
||||
|
||||
|
||||
def test_resolver_prefers_unique_native_provider_over_differently_priced_aggregators() -> None:
|
||||
# A unique first-party listing wins even when aggregators price the model
|
||||
# differently, where price consensus alone would give up and return None.
|
||||
def test_resolver_does_not_guess_when_a_native_route_exists_but_prices_differ() -> None:
|
||||
# The native preference only breaks ties between routes that price the model the
|
||||
# same. When an aggregator charges differently, the cost depends on the route and
|
||||
# the resolver still declines to guess, as it did before the native preference.
|
||||
original = litellm.model_cost
|
||||
litellm.model_cost = {
|
||||
"deepinfra/x/example-model": {
|
||||
|
|
@ -166,7 +171,7 @@ def test_resolver_prefers_unique_native_provider_over_differently_priced_aggrega
|
|||
}
|
||||
try:
|
||||
resolve_litellm_model.cache_clear()
|
||||
assert resolve_litellm_model("example-model") == "example/example-model"
|
||||
assert resolve_litellm_model("example-model") is None
|
||||
finally:
|
||||
litellm.model_cost = original
|
||||
resolve_litellm_model.cache_clear()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue