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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
feat(anthropic/chat/transformation.py): separate 5m vs. 1h cache creation token details for anthropic cost tracking
Krrish Dholakiae32ce6b053a9
diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.pyindex 1a59d677415..e54aeaf995f 100644--- a/litellm/llms/anthropic/chat/transformation.py+++ b/litellm/llms/anthropic/chat/transformation.py@@ -45,7 +45,10 @@ from litellm.types.llms.openai import (OpenAIMcpServerTool,OpenAIWebSearchOptions,)-from litellm.types.utils import CompletionTokensDetailsWrapper+from litellm.types.utils import (+ CacheCreationTokenDetails,+ CompletionTokensDetailsWrapper,+)from litellm.types.utils import Message as LitellmMessagefrom litellm.types.utils import PromptTokensDetailsWrapper, ServerToolUsefrom litellm.utils import (@@ -820,6 +823,7 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):_usage = usage_objectcache_creation_input_tokens: int = 0cache_read_input_tokens: int = 0+ cache_creation_token_details: Optional[CacheCreationTokenDetails] = Noneweb_search_requests: Optional[int] = Noneif ("cache_creation_input_tokens" in _usage@@ -842,8 +846,20 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):int, _usage["server_tool_use"]["web_search_requests"])+ if "cache_creation" in _usage and _usage["cache_creation"] is not None:+ cache_creation_token_details = CacheCreationTokenDetails(+ ephemeral_5m_input_tokens=_usage["cache_creation"].get(+ "ephemeral_5m_input_tokens"+ ),+ ephemeral_1h_input_tokens=_usage["cache_creation"].get(+ "ephemeral_1h_input_tokens"+ ),+ )+prompt_tokens_details = PromptTokensDetailsWrapper(cached_tokens=cache_read_input_tokens,+ cache_creation_tokens=cache_read_input_tokens,+ cache_creation_token_details=cache_creation_token_details,)completion_token_details = (CompletionTokensDetailsWrapper(diff --git a/litellm/types/utils.py b/litellm/types/utils.pyindex fbf2bad98ba..350d2511234 100644--- a/litellm/types/utils.py+++ b/litellm/types/utils.py@@ -862,6 +862,11 @@ class CompletionTokensDetailsWrapper("""Text tokens generated by the model."""+class CacheCreationTokenDetails(BaseModel):+ ephemeral_5m_input_tokens: Optional[int] = None+ ephemeral_1h_input_tokens: Optional[int] = None++class PromptTokensDetailsWrapper(PromptTokensDetails): # wrapper for older openai versions@@ -886,6 +891,9 @@ class PromptTokensDetailsWrapper(cache_creation_tokens: Optional[int] = None"""Number of cache creation tokens sent to the model. Used for Anthropic prompt caching."""+ cache_creation_token_details: Optional[CacheCreationTokenDetails] = None+ """Details of cache creation tokens sent to the model. Used for tracking 5m/1h cache creation tokens for Anthropic prompt caching."""+def __init__(self, *args, **kwargs):super().__init__(*args, **kwargs)if self.character_count is None:@@ -898,6 +906,8 @@ class PromptTokensDetailsWrapper(del self.web_search_requestsif self.cache_creation_tokens is None:del self.cache_creation_tokens+ if self.cache_creation_token_details is None:+ del self.cache_creation_token_detailsclass ServerToolUse(BaseModel):diff --git a/tests/local_testing/test_anthropic_prompt_caching.py b/tests/local_testing/test_anthropic_prompt_caching.pyindex 3bf9fc7077b..ad054616cec 100644--- a/tests/local_testing/test_anthropic_prompt_caching.py+++ b/tests/local_testing/test_anthropic_prompt_caching.py@@ -320,7 +320,7 @@ async def test_anthropic_api_prompt_caching_basic_with_cache_creation():random_id)* 400,- "cache_control": {"type": "ephemeral"},+ "cache_control": {"type": "ephemeral", "ttl": "1h"},}],},@@ -331,7 +331,7 @@ async def test_anthropic_api_prompt_caching_basic_with_cache_creation():{"type": "text","text": "What are the key terms and conditions in this agreement?",- "cache_control": {"type": "ephemeral"},+ "cache_control": {"type": "ephemeral", "ttl": "5m"},}],},@@ -580,7 +580,6 @@ async def test_anthropic_api_prompt_caching_streaming():if hasattr(chunk, "usage") and hasattr(chunk.usage, "cache_creation_input_tokens"):- print("chunk.usage", chunk.usage)is_cache_creation_input_tokens_in_usage = Trueidx += 1