Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix: include cache_creation_token_details in cost guard to prevent undercharging ephemeral tokens

Ryan Crabbe1d8e3a6f2814
diff --git a/litellm/litellm_core_utils/llm_cost_calc/utils.py b/litellm/litellm_core_utils/llm_cost_calc/utils.py
index 38cd69110d5..f35096717cc 100644
--- a/litellm/litellm_core_utils/llm_cost_calc/utils.py
+++ b/litellm/litellm_core_utils/llm_cost_calc/utils.py
@@ -535,7 +535,7 @@ def _calculate_input_cost(
)
### CACHE WRITING COST - Now uses tiered pricing
- if prompt_tokens_details["cache_creation_tokens"]:
+ if prompt_tokens_details["cache_creation_tokens"] or prompt_tokens_details["cache_creation_token_details"] is not None:
prompt_cost += calculate_cache_writing_cost(
cache_creation_tokens=prompt_tokens_details["cache_creation_tokens"],
cache_creation_token_details=prompt_tokens_details[
diff --git a/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py b/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py
index 9e70f3e08d5..8c6dfca63b4 100644
--- a/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py
+++ b/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py
@@ -23,6 +23,8 @@ sys.path.insert(
) # Adds the parent directory to the system path
from litellm.litellm_core_utils.llm_cost_calc.utils import (
+ _calculate_input_cost,
+ PromptTokensDetailsResult,
calculate_cache_writing_cost,
generic_cost_per_token,
)
@@ -559,6 +561,48 @@ def test_calculate_cache_writing_cost():
assert result_zero == 0.0
+def test_cache_writing_cost_with_zero_creation_tokens_and_ephemeral_details():
+ """
+ Regression test: when cache_creation_tokens is 0 but cache_creation_token_details
+ has non-zero ephemeral tokens, the cost must still be calculated.
+ This ensures the guard in _calculate_input_cost doesn't skip
+ calculate_cache_writing_cost when only ephemeral token details are present.
+ """
+ cache_creation_cost = 3.75e-06
+ cache_creation_cost_above_1hr = 6e-06
+
+ prompt_tokens_details: PromptTokensDetailsResult = {
+ "cache_hit_tokens": 0,
+ "cache_creation_tokens": 0,
+ "cache_creation_token_details": CacheCreationTokenDetails(
+ ephemeral_5m_input_tokens=100,
+ ephemeral_1h_input_tokens=200,
+ ),
+ "text_tokens": 0,
+ "audio_tokens": 0,
+ "image_tokens": 0,
+ "character_count": 0,
+ "image_count": 0,
+ "video_length_seconds": 0.0,
+ }
+
+ model_info: ModelInfo = {}
+
+ result = _calculate_input_cost(
+ prompt_tokens_details=prompt_tokens_details,
+ model_info=model_info,
+ prompt_base_cost=0.0,
+ cache_read_cost=0.0,
+ cache_creation_cost=cache_creation_cost,
+ cache_creation_cost_above_1hr=cache_creation_cost_above_1hr,
+ )
+
+ # Expected: (100 * 3.75e-06) + (200 * 6e-06) = 0.000375 + 0.0012 = 0.001575
+ expected = (100 * cache_creation_cost) + (200 * cache_creation_cost_above_1hr)
+ assert result > 0, "Cost should not be zero when ephemeral token details are present"
+ assert round(result, 6) == round(expected, 6)
+
+
def test_service_tier_flex_pricing():
"""Test that flex service tier uses correct pricing (approximately 50% of standard)."""
# Set up environment for local model cost map