From ebd580aad8dd45054baad5d1b6818dd913ce6882 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 17 May 2026 06:15:36 +0000 Subject: [PATCH] fix(cost): apply regional/service-tier suffix to above-threshold cache keys The above-threshold input/output cost keys were resolved through _get_service_tier_cost_key (e.g. input_cost_per_token_above_272k_tokens_regional), but the cache-read/creation above-threshold keys were hardcoded to the standard form, so regional cache-read pricing above the threshold (e.g. cache_read_input_token_cost_above_272k_tokens_regional on gpt-5.4/gpt-5.5) was never consumed. Resolve cache_creation/cache_creation_1hr/cache_read above-threshold keys via _get_service_tier_cost_key when service_tier is set, and treat the standard above-threshold key as the fallback (matching the existing fallback semantics inside _get_cost_per_unit). Co-authored-by: Yassin Kortam --- .../litellm_core_utils/llm_cost_calc/utils.py | 45 ++++++++++++++++--- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/litellm/litellm_core_utils/llm_cost_calc/utils.py b/litellm/litellm_core_utils/llm_cost_calc/utils.py index 24c04e0bc45..cde8e2c1016 100644 --- a/litellm/litellm_core_utils/llm_cost_calc/utils.py +++ b/litellm/litellm_core_utils/llm_cost_calc/utils.py @@ -270,16 +270,41 @@ def _get_token_base_cost( ), ) - # Apply tiered pricing to cache costs - cache_creation_tiered_key = ( + # Apply tiered pricing to cache costs. Prefer a + # service_tier-specific above-threshold key when available + # (e.g. cache_read_input_token_cost_above_272k_tokens_regional), + # falling back to the standard above-threshold key. + cache_creation_base_key = ( f"cache_creation_input_token_cost_above_{threshold_str}_tokens" ) - cache_creation_1hr_tiered_key = f"cache_creation_input_token_cost_above_1hr_above_{threshold_str}_tokens" - cache_read_tiered_key = ( + cache_creation_1hr_base_key = f"cache_creation_input_token_cost_above_1hr_above_{threshold_str}_tokens" + cache_read_base_key = ( f"cache_read_input_token_cost_above_{threshold_str}_tokens" ) + cache_creation_tiered_key = ( + _get_service_tier_cost_key( + cache_creation_base_key, service_tier + ) + if service_tier + else cache_creation_base_key + ) + cache_creation_1hr_tiered_key = ( + _get_service_tier_cost_key( + cache_creation_1hr_base_key, service_tier + ) + if service_tier + else cache_creation_1hr_base_key + ) + cache_read_tiered_key = ( + _get_service_tier_cost_key(cache_read_base_key, service_tier) + if service_tier + else cache_read_base_key + ) - if cache_creation_tiered_key in model_info: + if ( + cache_creation_tiered_key in model_info + or cache_creation_base_key in model_info + ): cache_creation_cost = cast( float, _get_cost_per_unit( @@ -289,7 +314,10 @@ def _get_token_base_cost( ), ) - if cache_creation_1hr_tiered_key in model_info: + if ( + cache_creation_1hr_tiered_key in model_info + or cache_creation_1hr_base_key in model_info + ): cache_creation_cost_above_1hr = cast( float, _get_cost_per_unit( @@ -299,7 +327,10 @@ def _get_token_base_cost( ), ) - if cache_read_tiered_key in model_info: + if ( + cache_read_tiered_key in model_info + or cache_read_base_key in model_info + ): cache_read_cost = cast( float, _get_cost_per_unit(