From 0e3f35effe215734c662dec1f61fc417f2e9ddad Mon Sep 17 00:00:00 2001 From: ccurme Date: Wed, 28 May 2025 13:52:37 -0400 Subject: [PATCH] anthropic: store cache ttl details on usage metadata (#31393) --- libs/partners/anthropic/langchain_anthropic/chat_models.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libs/partners/anthropic/langchain_anthropic/chat_models.py b/libs/partners/anthropic/langchain_anthropic/chat_models.py index 3bc75804f08..60a95747de5 100644 --- a/libs/partners/anthropic/langchain_anthropic/chat_models.py +++ b/libs/partners/anthropic/langchain_anthropic/chat_models.py @@ -2129,6 +2129,13 @@ def _create_usage_metadata(anthropic_usage: BaseModel) -> UsageMetadata: "cache_read": getattr(anthropic_usage, "cache_read_input_tokens", None), "cache_creation": getattr(anthropic_usage, "cache_creation_input_tokens", None), } + # Add (beta) cache TTL information if available + cache_creation = getattr(anthropic_usage, "cache_creation", None) + if cache_creation: + for k in ["ephemeral_1h_input_tokens", "ephemeral_5m_input_tokens"]: + v = getattr(cache_creation, k, None) + if v: + input_token_details[k] = v # Anthropic input_tokens exclude cached token counts. input_tokens = (