refactor(cost): select cache-read key before cast to satisfy LIT006 budget

This commit is contained in:
Anuj7411 2026-08-28 08:08:53 +05:30
parent af1fb83022
commit c8a8438095

View file

@ -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.