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
Merge remote-tracking branch 'origin/litellm_internal_staging' into fix-cost-alias-double-prefix
mateo-berri6d1a7ff8a865
diff --cc litellm/cost_calculator.pyindex 1e13cb9307e,6536941a094..903b2c15a10--- a/litellm/cost_calculator.py+++ b/litellm/cost_calculator.py@@@ -794,12 -794,10 +794,8 @@@ def _select_model_name_for_cost_calcand custom_llm_provider is not Noneand not _model_contains_known_llm_provider(return_model)): # add provider prefix if not already present, to match model_cost-- if region_name is not None:-- return_model = f"{custom_llm_provider}/{region_name}/{return_model}"-- else:-- return_model = f"{custom_llm_provider}/{return_model}"-- return_model = _strip_unregistered_leading_segments(return_model, region_name)++ provider_prefix: Final = custom_llm_provider if region_name is None else f"{custom_llm_provider}/{region_name}"++ return_model = _strip_unregistered_leading_segments(f"{provider_prefix}/{return_model}", region_name)return return_modeldiff --cc tests/test_litellm/test_cost_calculator.pyindex 9aa627b2b23,82ab3b3bd08..178c059462b--- a/tests/test_litellm/test_cost_calculator.py+++ b/tests/test_litellm/test_cost_calculator.py@@@ -3783,134 -3909,49 +3909,182 @@@ def test_completion_cost_prices_anthropassert cost == pytest.approx(3 * 4e-6 + 4014 * 4e-7 + 5 * 2e-5, rel=1e-9)+def test_select_model_name_strips_unregistered_alias_prefix(_local_model_cost_map):+ """A router-facing model_name alias containing "/" whose leading segment is NOT a+ registered provider must not be double-prefixed into a non-existent cost key.++ Regression test for #38069: alias "vertex/claude-opus-5" (real deployment+ "vertex_ai/claude-opus-5") was re-prefixed into "vertex_ai/vertex/claude-opus-5",+ silently pricing every streamed request at $0.+ """++ from litellm.cost_calculator import _select_model_name_for_cost_calc++ response = litellm.ModelResponse(+ id="x",+ choices=[+ {+ "index": 0,+ "message": {"role": "assistant", "content": "hi"},+ "finish_reason": "stop",+ }+ ],+ model="vertex/claude-opus-5",+ )+ response._hidden_params = {}++ selected = _select_model_name_for_cost_calc(+ model=None,+ completion_response=response,+ custom_llm_provider="vertex_ai",+ )++ assert selected == "vertex_ai/claude-opus-5"+++def test_completion_cost_nonzero_for_slash_alias_model_name(_local_model_cost_map):+ """End-to-end cost through a "/"-containing alias must price above zero (#38069)."""++ response = litellm.ModelResponse(+ id="x",+ choices=[+ {+ "index": 0,+ "message": {"role": "assistant", "content": "hi"},+ "finish_reason": "stop",+ }+ ],+ model="vertex/claude-opus-5",+ )+ response._hidden_params = {"custom_llm_provider": "vertex_ai"}+ response.usage = litellm.Usage(prompt_tokens=100, completion_tokens=50)++ cost = litellm.completion_cost(+ completion_response=response,+ custom_llm_provider="vertex_ai",+ )++ assert cost == pytest.approx(100 * 5e-6 + 50 * 2.5e-5, rel=1e-9)+++def test_select_model_name_unresolvable_alias_unchanged(_local_model_cost_map):+ """An alias that resolves to no known cost key keeps the legacy double-prefixed name."""++ from litellm.cost_calculator import _select_model_name_for_cost_calc++ response = litellm.ModelResponse(+ id="x",+ choices=[+ {+ "index": 0,+ "message": {"role": "assistant", "content": "hi"},+ "finish_reason": "stop",+ }+ ],+ model="team/nonsense-model",+ )+ response._hidden_params = {}++ selected = _select_model_name_for_cost_calc(+ model=None,+ completion_response=response,+ custom_llm_provider="vertex_ai",+ )++ assert selected == "vertex_ai/team/nonsense-model"+++def test_completion_cost_keeps_custom_priced_slash_router_id(_local_model_cost_map):+ """A custom-priced router id containing "/" keeps its custom pricing instead of being+ rewritten to the built-in key its suffix happens to match."""++ from litellm.cost_calculator import _select_model_name_for_cost_calc++ litellm.register_model(+ model_cost={+ "vertex/claude-opus-5": {+ "input_cost_per_token": 7e-6,+ "output_cost_per_token": 8e-6,+ "litellm_provider": "vertex_ai",+ }+ }+ )++ selected = _select_model_name_for_cost_calc(+ model="vertex_ai/claude-opus-5",+ completion_response=None,+ custom_pricing=True,+ custom_llm_provider="vertex_ai",+ router_model_id="vertex/claude-opus-5",+ )+ assert selected == "vertex_ai/vertex/claude-opus-5"++ response = litellm.ModelResponse(+ id="x",+ choices=[+ {+ "index": 0,+ "message": {"role": "assistant", "content": "hi"},+ "finish_reason": "stop",+ }+ ],+ model="vertex/claude-opus-5",+ )+ response._hidden_params = {"custom_llm_provider": "vertex_ai"}+ response.usage = litellm.Usage(prompt_tokens=100, completion_tokens=50)++ cost = litellm.completion_cost(+ completion_response=response,+ custom_llm_provider="vertex_ai",+ custom_pricing=True,+ router_model_id="vertex/claude-opus-5",+ )+ assert cost == pytest.approx(100 * 7e-6 + 50 * 8e-6, rel=1e-9)+++++ @pytest.mark.parametrize(+ ("model", "expected_1hr_rate"),+ [("claude-3-haiku-20240307", 5e-07), ("claude-3-opus-20240229", 3e-05)],+ )+ def test_claude_3_one_hour_cache_writes_bill_at_double_input(+ _local_model_cost_map, model: str, expected_1hr_rate: float+ ):+ """Regression: both models carried the Sonnet 1h cache-write rate (6e-06) instead of+ 2x their own input price, overbilling haiku 12x and underbilling opus 5x."""++ usage = Usage(+ prompt_tokens=1000,+ completion_tokens=0,+ total_tokens=1000,+ prompt_tokens_details=PromptTokensDetailsWrapper(+ cached_tokens=0,+ cache_creation_tokens=1000,+ cache_creation_token_details=CacheCreationTokenDetails(+ ephemeral_5m_input_tokens=0, ephemeral_1h_input_tokens=1000+ ),+ ),+ )++ prompt_cost, _ = cost_per_token(model=model, usage_object=usage, custom_llm_provider="anthropic")++ assert prompt_cost == pytest.approx(1000 * expected_1hr_rate, rel=1e-9)+++ def test_every_one_hour_cache_write_rate_is_double_its_input_rate():+ """Guard against pasting one model's 1h cache-write price onto another: every provider+ LiteLLM tracks (Anthropic, Bedrock, Vertex, Azure) publishes the 1h write at 2x input."""++ cost_map = json.loads(+ (Path(__file__).parents[2] / "model_prices_and_context_window.json").read_text()+ )+ one_hour_prefix = "cache_creation_input_token_cost_above_1hr"+ deviations = {+ (name, key): (entry["input_cost_per_token" + key[len(one_hour_prefix) :]], entry[key])+ for name, entry in cost_map.items()+ if isinstance(entry, dict)+ for key in entry+ if key.startswith(one_hour_prefix)+ and entry[key] != pytest.approx(2 * entry["input_cost_per_token" + key[len(one_hour_prefix) :]], rel=1e-9)+ }++ assert deviations == {}diff --cc type-discipline-budget.jsonindex 627811a7f1d,4465580657b..1cd7aa8641d--- a/type-discipline-budget.json+++ b/type-discipline-budget.json@@@ -27,10 -27,10 +27,10 @@@"limit": 0},"LIT010": {- "limit": 16673- "limit": 16621++ "limit": 16620},"LIT011": {- "limit": 5588+ "limit": 5585},"LIT012": {"limit": 4510