Skip to content

The agent engineering platform.

anthropic: store cache ttl details on usage metadata (#31393)

ccurme0e3f35effe21
diff --git a/libs/partners/anthropic/langchain_anthropic/chat_models.py b/libs/partners/anthropic/langchain_anthropic/chat_models.py
index 3bc75804f0..60a95747de 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 = (