diff --git a/litellm/cost_calculator.py b/litellm/cost_calculator.py index a4a9a5bee39..948f7aad445 100644 --- a/litellm/cost_calculator.py +++ b/litellm/cost_calculator.py @@ -38,11 +38,11 @@ from litellm.litellm_core_utils.llm_cost_calc.utils import ( BilledTokenRates, CostCalculatorUtils, calculate_cache_writing_cost, - calculate_cost_component, generic_cost_per_character, generic_cost_per_token, get_batch_cost_rates, get_billable_input_tokens, + get_cost_per_unit, get_regional_uplift_multiplier, get_service_tier_cost_key, get_token_type_cost_breakdown, @@ -3040,7 +3040,10 @@ def batch_cost_calculator( # Add cache read cost if applicable cache_read_cost_key: Final = get_service_tier_cost_key("cache_read_input_token_cost", None) - total_prompt_cost += calculate_cost_component(model_info, cache_read_cost_key, cache_read_tokens) / 2 + cache_read_cost: Final = get_cost_per_unit(model_info, cache_read_cost_key, None) + total_prompt_cost += ( + cache_read_tokens * (input_cost_per_token if cache_read_cost is None else cache_read_cost) / 2 + ) cache_creation_cost: Final = model_info.get("cache_creation_input_token_cost") or input_cost_per_token total_prompt_cost += cache_creation_tokens * cache_creation_cost / 2 diff --git a/tests/unit/test_cost_calculator.py b/tests/unit/test_cost_calculator.py index 84f6dab9eff..72b38efee01 100644 --- a/tests/unit/test_cost_calculator.py +++ b/tests/unit/test_cost_calculator.py @@ -4310,6 +4310,24 @@ def test_batch_cost_calculator_cache_creation_falls_back_to_input_rate(): assert prompt_cost == pytest.approx((1000 * 3e-6 + 8000 * 3e-7 + 2000 * 3e-6) / 2) +def test_batch_cost_calculator_cache_read_falls_back_to_input_rate(): + from litellm.cost_calculator import batch_cost_calculator + + model_info: ModelInfo = { + "supported_openai_params": [], + "input_cost_per_token": 3e-6, + "output_cost_per_token": 15e-6, + } + prompt_cost, _ = batch_cost_calculator( + usage=_batch_cache_usage(), + model="my-fine-tuned-model", + custom_llm_provider="openai", + model_info=model_info, + ) + + assert prompt_cost == pytest.approx(11000 * 3e-6 / 2) + + def test_completion_cost_bills_interactions_api_response(): from litellm.types.interactions import InteractionsAPIResponse