This commit is contained in:
John Karam 2026-10-29 13:09:35 +00:00 • committed by GitHub
commit 6eb581065c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 23 additions and 2 deletions

View file

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

View file

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