Skip to content

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]

fix(fallback-generalizations): let exact cost-map entries beat capability rules across lookup-candidate ladders

mateo-berri81e639398ad4
diff --git a/basedpyright-code-budget.json b/basedpyright-code-budget.json
index c29adbd3044..edfb3536ad3 100644
--- a/basedpyright-code-budget.json
+++ b/basedpyright-code-budget.json
@@ -57,7 +57,7 @@
"limit": 5900
},
"reportMissingTypeArgument": {
- "limit": 15908
+ "limit": 15903
},
"reportMissingTypeStubs": {
"limit": 41
@@ -108,10 +108,10 @@
"limit": 40539
},
"reportUnknownParameterType": {
- "limit": 20408
+ "limit": 20403
},
"reportUnknownVariableType": {
- "limit": 32143
+ "limit": 32141
},
"reportUnnecessaryCast": {
"limit": 177
@@ -123,7 +123,7 @@
"limit": 7
},
"reportUnnecessaryIsInstance": {
- "limit": 1210
+ "limit": 1209
},
"reportUntypedBaseClass": {
"limit": 165
diff --git a/litellm/utils.py b/litellm/utils.py
index 907834dc5a4..18b89ee0d13 100644
--- a/litellm/utils.py
+++ b/litellm/utils.py
@@ -5069,8 +5069,13 @@ def _get_model_info_from_generalization(
Tries the same name candidates as the exact lookups, in the same order, and
returns ``(matched_name, model_info)`` for the first candidate matched by at
least one capability rule, with ``litellm_provider`` backfilled from the
- provider the caller requested. O(number of rules); only call after the exact
- lookups have missed.
+ provider the caller requested. Rules lose to exact entries: if ANY candidate is
+ an exact ``litellm.model_cost`` key (necessarily provider-mismatched, or the
+ exact lookups would have returned it), the model is known rather than unmapped,
+ and resolving it from rules would hand an unpriced rule-derived entry to
+ callers whose fallback ladder (e.g. the cost calculator's model-name variants)
+ still had a priced exact name to try. O(number of rules); only call after the
+ exact lookups have missed.
"""
candidates = (
potential_model_names["combined_model_name"],
@@ -5079,6 +5084,8 @@ def _get_model_info_from_generalization(
potential_model_names["combined_stripped_model_name"],
potential_model_names["stripped_model_name"],
)
+ if any(_get_model_cost_key(candidate) is not None for candidate in candidates):
+ return None
for candidate in candidates:
generalized_info = match_capability_generalizations(candidate)
if generalized_info is None:
diff --git a/tests/test_litellm/litellm_core_utils/test_fallback_generalizations.py b/tests/test_litellm/litellm_core_utils/test_fallback_generalizations.py
index 652f99ee402..69020fceba1 100644
--- a/tests/test_litellm/litellm_core_utils/test_fallback_generalizations.py
+++ b/tests/test_litellm/litellm_core_utils/test_fallback_generalizations.py
@@ -449,6 +449,31 @@ def test_shipped_exact_entry_beats_rules(shipped_cost_map):
assert info.get("supports_mid_conversation_system") is None
+def test_shipped_rules_lose_to_exact_entries_across_cost_ladder_variants(shipped_cost_map):
+ """A route-mangled variant of an exactly-mapped model must never resolve from
+ rules. The cost calculator tries model-name variants in order; a rule-derived
+ unpriced entry served for an early variant (here bedrock/claude-haiku-4-5-20251001,
+ whose bare form is exactly mapped under anthropic) would zero out the bill even
+ though the exact priced bedrock entry is one variant later. An exactly-mapped id
+ under a mismatched provider raises instead of resolving from rules."""
+ from litellm import completion_cost
+ from litellm.types.utils import ModelResponse, Usage
+
+ assert "claude-haiku-4-5-20251001" in litellm.model_cost
+ with pytest.raises(Exception):
+ litellm.get_model_info("claude-haiku-4-5-20251001", custom_llm_provider="bedrock")
+
+ entry = litellm.model_cost["us.anthropic.claude-haiku-4-5-20251001-v1:0"]
+ response = ModelResponse(model="claude-haiku-4-5-20251001", usage=Usage(prompt_tokens=100, completion_tokens=50))
+ cost = completion_cost(
+ completion_response=response,
+ model="bedrock/invoke/us.anthropic.claude-haiku-4-5-20251001-v1:0",
+ custom_llm_provider="bedrock",
+ )
+ assert cost == 100 * entry["input_cost_per_token"] + 50 * entry["output_cost_per_token"]
+ assert cost > 0
+
+
def test_shipped_adaptive_rule_gates_on_version_not_pricing(shipped_cost_map):
"""The version-gated adaptive-thinking capability rule marks an unmapped Claude
adaptive only from >= 4.6, including provider-prefixed ids the anchored routing
diff --git a/tests/test_litellm/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py b/tests/test_litellm/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py
index 7d104ff1f2b..52f136a1fd9 100644
--- a/tests/test_litellm/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py
+++ b/tests/test_litellm/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py
@@ -2130,9 +2130,9 @@ def test_bedrock_invoke_transform_hoists_all_system_for_unmapped_model(local_mod
def test_bedrock_invoke_transform_keeps_system_in_place_for_unmapped_future_claude(local_model_cost_map):
"""An unmapped Bedrock Claude at 4.8 or higher resolves through the
- ``bedrock-anthropic-claude-mid-conversation-system`` fallback rule, so a
- future model that has not landed in the cost map yet keeps the
- cache-preserving in-place behavior instead of falling back to hoist-all."""
+ ``claude-mid-conversation-system`` capability rule, so a future model that
+ has not landed in the cost map yet keeps the cache-preserving in-place
+ behavior instead of falling back to hoist-all."""
from litellm.types.router import GenericLiteLLMParams
cfg = AmazonAnthropicClaudeMessagesConfig()
@@ -2159,9 +2159,9 @@ def test_bedrock_claude_4_8_plus_cost_map_entries_carry_mid_conversation_system_
"""Exact cost-map hits resolve before fallback-generalization rules, so a
mapped Bedrock Claude 4.8+ entry without ``supports_mid_conversation_system``
silently loses the cache-preserving in-place handling that the
- ``bedrock-anthropic-claude-mid-conversation-system`` rule grants unmapped
- ids. Every mapped entry the rule's own pattern matches must carry the flag
- explicitly."""
+ ``claude-mid-conversation-system`` capability rule grants unmapped ids.
+ Every mapped bedrock entry the rule's own pattern matches must carry the
+ flag explicitly."""
import re
import litellm
@@ -2171,7 +2171,7 @@ def test_bedrock_claude_4_8_plus_cost_map_entries_carry_mid_conversation_system_
cost_map = json.load(f)
rules = cost_map["fallback_generalizations"]["rules"]
pattern = re.compile(
- next(r["pattern"] for r in rules if r["name"] == "bedrock-anthropic-claude-mid-conversation-system"),
+ next(r["pattern"] for r in rules if r["name"] == "claude-mid-conversation-system"),
re.IGNORECASE,
)
missing = [
diff --git a/type-discipline-budget.json b/type-discipline-budget.json
index 6bc301baa0e..87b4c96e323 100644
--- a/type-discipline-budget.json
+++ b/type-discipline-budget.json
@@ -1,9 +1,9 @@
{
"LIT001": {
- "limit": 23424
+ "limit": 23409
},
"LIT002": {
- "limit": 27514
+ "limit": 27511
},
"LIT003": {
"limit": 292