diff --git a/litellm/litellm_core_utils/llm_cost_calc/utils.py b/litellm/litellm_core_utils/llm_cost_calc/utils.py index fdc6127814f..0ba0bd83880 100644 --- a/litellm/litellm_core_utils/llm_cost_calc/utils.py +++ b/litellm/litellm_core_utils/llm_cost_calc/utils.py @@ -309,15 +309,14 @@ def _get_token_base_cost( float, _get_cost_per_unit(model_info, "cache_creation_input_token_cost_above_1hr"), ) - cache_read_cost = cast(float, _get_cost_per_unit(model_info, cache_read_cost_key)) - # Some model entries only carry the legacy `input_cost_per_token_cache_hit` field - # instead of the canonical `cache_read_input_token_cost` (e.g. DeepSeek entries - # added before the field was standardized). Without this fallback, cache-hit - # tokens silently cost $0 for those models. See BerriAI/litellm#28854. - if model_info.get("cache_read_input_token_cost") is None: - legacy_cache_hit_cost: Final = model_info.get("input_cost_per_token_cache_hit") - if legacy_cache_hit_cost is not None: - cache_read_cost = cast(float, _get_cost_per_unit(model_info, "input_cost_per_token_cache_hit")) + use_legacy_cache_hit_key: Final = ( + model_info.get("cache_read_input_token_cost") is None + and model_info.get("input_cost_per_token_cache_hit") is not None + ) + effective_cache_read_key: Final = ( + "input_cost_per_token_cache_hit" if use_legacy_cache_hit_key else cache_read_cost_key + ) + cache_read_cost = cast(float, _get_cost_per_unit(model_info, effective_cache_read_key)) ## CHECK IF ABOVE THRESHOLD # Optimization: collect threshold keys first to avoid sorting all model_info keys.