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
perf(cost-calc): precompute service-tier cost-key suffixes (#31431)
Yassin Kortam0e1a3babf047
diff --git a/litellm/litellm_core_utils/llm_cost_calc/utils.py b/litellm/litellm_core_utils/llm_cost_calc/utils.pyindex d2c97a5d6e7..e407dd70bf0 100644--- a/litellm/litellm_core_utils/llm_cost_calc/utils.py+++ b/litellm/litellm_core_utils/llm_cost_calc/utils.py@@ -33,6 +33,11 @@ _IMAGE_RESPONSE_CALL_TYPES = frozenset(# Pre-resolved DataResidency enum values for fast membership checks_VALID_DATA_RESIDENCIES = frozenset(r.value for r in DataResidency)+# Pre-resolved service-tier cost-key suffixes (e.g. "_priority"). Used per+# request in the cost-calc path, so the f-strings are built once here instead+# of being rebuilt for every model_info key on every call.+_SERVICE_TIER_SUFFIXES: tuple[str, ...] = tuple(f"_{st.value}" for st in ServiceTier)+def _get_token_detail_value(details: object, key: str) -> Optional[int]:if isinstance(details, dict):@@ -252,7 +257,7 @@ def _get_token_base_cost(kfor k in model_infoif k.startswith("input_cost_per_token_above_")- and not any(k.endswith(f"_{st.value}") for st in ServiceTier)+ and not k.endswith(_SERVICE_TIER_SUFFIXES)]if not threshold_keys:return (@@ -418,9 +423,8 @@ def _get_cost_per_unit(# If the service tier key doesn't exist or is None, try to fall back to the standard keyif cost_per_unit is None:- # Check if any service tier suffix exists in the cost key using ServiceTier enum- for service_tier in ServiceTier:- suffix = f"_{service_tier.value}"+ # Check if any service tier suffix exists in the cost key+ for suffix in _SERVICE_TIER_SUFFIXES:if suffix in cost_key:# Extract the base key by removing the matched suffixbase_key = cost_key.replace(suffix, "")diff --git a/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py b/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.pyindex 72fee9bfb40..b75b6d78090 100644--- a/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py+++ b/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py@@ -1720,3 +1720,51 @@ def test_priority_service_tier_above_threshold_falls_back_to_standard_for_cache_expected_completion = 1_000 * 2.25e-5assert prompt_cost == pytest.approx(expected_prompt, rel=1e-9)assert completion_cost == pytest.approx(expected_completion, rel=1e-9)+++def test_service_tier_suffixes_constant_in_sync_with_enum():+ from litellm.litellm_core_utils.llm_cost_calc.utils import _SERVICE_TIER_SUFFIXES+ from litellm.types.utils import ServiceTier++ assert _SERVICE_TIER_SUFFIXES == tuple(f"_{st.value}" for st in ServiceTier)+++def test_get_cost_per_unit_falls_back_from_service_tier_key_to_base():+ from litellm.litellm_core_utils.llm_cost_calc.utils import _get_cost_per_unit++ model_info = {"input_cost_per_token": 2e-6}+ # service-tier key is absent -> falls back to the base key+ assert _get_cost_per_unit(model_info, "input_cost_per_token_priority") == 2e-6+ # service-tier key present -> used directly, no fallback+ model_info_direct = {+ "input_cost_per_token_priority": 5e-6,+ "input_cost_per_token": 2e-6,+ }+ assert (+ _get_cost_per_unit(model_info_direct, "input_cost_per_token_priority") == 5e-6+ )+++def test_threshold_keys_exclude_service_tier_variants():+ from typing import cast++ from litellm.litellm_core_utils.llm_cost_calc.utils import _get_token_base_cost+ from litellm.types.utils import ModelInfo, Usage++ # The service-tier-suffixed above-threshold key must be excluded from+ # threshold detection. The _priority variant has a higher threshold (300k),+ # so if it were not excluded it would sort first and drive a 9e-6 rate for+ # this non-tier request. With the exclusion only the standard 200k key+ # applies, giving 3e-6.+ model_info = cast(+ ModelInfo,+ {+ "input_cost_per_token": 1e-6,+ "input_cost_per_token_above_200k_tokens": 3e-6,+ "input_cost_per_token_above_300k_tokens_priority": 9e-6,+ "output_cost_per_token": 2e-6,+ },+ )+ usage = Usage(prompt_tokens=350_000, completion_tokens=1_000, total_tokens=351_000)+ prompt_base, *_ = _get_token_base_cost(model_info=model_info, usage=usage)+ assert prompt_base == 3e-6