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]

test: cover nested cache_creation_input_tokens in responses bridge and spend logs

mateo-berri490079e7df8c
diff --git a/tests/test_litellm/proxy/spend_tracking/test_spend_tracking_utils.py b/tests/test_litellm/proxy/spend_tracking/test_spend_tracking_utils.py
index 33d835652cd..e5add059260 100644
--- a/tests/test_litellm/proxy/spend_tracking/test_spend_tracking_utils.py
+++ b/tests/test_litellm/proxy/spend_tracking/test_spend_tracking_utils.py
@@ -124,6 +124,30 @@ def test_get_logging_payload_maps_openai_cache_write_tokens_to_cache_creation_in
assert additional_usage_values["prompt_tokens_details"]["cache_write_tokens"] == 800
+def test_get_logging_payload_maps_nested_cache_creation_input_tokens():
+ """
+ Regression (LIT-5757): DashScope nests cache_creation_input_tokens inside
+ prompt_tokens_details; SpendLogs must record it as cache_creation_input_tokens.
+ """
+ additional_usage_values: Final = _get_additional_usage_values_for_usage(
+ litellm.Usage(
+ prompt_tokens=2059,
+ completion_tokens=31,
+ total_tokens=2090,
+ prompt_tokens_details={
+ "cached_tokens": 0,
+ "text_tokens": 2059,
+ "cache_type": "ephemeral",
+ "cache_creation_input_tokens": 2048,
+ "cache_creation": {"ephemeral_5m_input_tokens": 2048},
+ },
+ )
+ )
+
+ assert additional_usage_values["cache_creation_input_tokens"] == 2048
+ assert additional_usage_values["prompt_tokens_details"]["cache_write_tokens"] == 2048
+
+
def test_get_logging_payload_preserves_anthropic_cache_creation_input_tokens():
additional_usage_values = _get_additional_usage_values_for_usage(
litellm.Usage(
diff --git a/tests/test_litellm/test_responses_api_bridge_non_stream.py b/tests/test_litellm/test_responses_api_bridge_non_stream.py
index 25a3bc2dbba..c272b151865 100644
--- a/tests/test_litellm/test_responses_api_bridge_non_stream.py
+++ b/tests/test_litellm/test_responses_api_bridge_non_stream.py
@@ -1,6 +1,6 @@
import os
import sys
-from typing import Optional
+from typing import Final, Optional
from unittest.mock import Mock
import pytest
@@ -192,6 +192,32 @@ def test_transform_usage_with_cached_tokens_only():
print("✓ Transformation works with cached_tokens only")
+def test_transform_usage_maps_nested_cache_creation_input_tokens():
+ """
+ Regression (LIT-5757): DashScope nests cache_creation_input_tokens inside
+ prompt_tokens_details; the bridge must surface it as cache_write_tokens.
+ """
+ usage: Final = Usage(
+ prompt_tokens=2059,
+ completion_tokens=31,
+ total_tokens=2090,
+ prompt_tokens_details={
+ "cached_tokens": 0,
+ "text_tokens": 2059,
+ "cache_type": "ephemeral",
+ "cache_creation_input_tokens": 2048,
+ "cache_creation": {"ephemeral_5m_input_tokens": 2048},
+ },
+ )
+
+ responses_usage: Final = LiteLLMCompletionResponsesConfig._transform_chat_completion_usage_to_responses_usage(
+ usage
+ )
+
+ assert responses_usage.input_tokens_details is not None
+ assert responses_usage.input_tokens_details.cache_write_tokens == 2048
+
+
def test_transform_usage_with_reasoning_tokens_only():
"""
Test transformation when only reasoning_tokens is provided (no cached_tokens).