Skip to content

The agent engineering platform.

fix(anthropic): remove beta header warning for TTL (#32832)

Mason Daugherty4e39c164bb55
diff --git a/libs/partners/anthropic/langchain_anthropic/chat_models.py b/libs/partners/anthropic/langchain_anthropic/chat_models.py
index 66f07c7ffd..b1052a1f10 100644
--- a/libs/partners/anthropic/langchain_anthropic/chat_models.py
+++ b/libs/partners/anthropic/langchain_anthropic/chat_models.py
@@ -999,24 +999,27 @@ class ChatAnthropic(BaseChatModel):
.. dropdown:: Extended caching
- .. versionadded:: 0.3.15
-
The cache lifetime is 5 minutes by default. If this is too short, you can
- apply one hour caching by enabling the ``'extended-cache-ttl-2025-04-11'``
- beta header:
+ apply one hour caching by setting ``ttl`` to ``'1h'``.
.. code-block:: python
llm = ChatAnthropic(
model="claude-3-7-sonnet-20250219",
- betas=["extended-cache-ttl-2025-04-11"],
)
- and specifying ``"cache_control": {"type": "ephemeral", "ttl": "1h"}``.
+ messages = [{
+ "role": "user",
+ "content": [
+ {
+ "type": "text",
+ "text": f"{long_text}",
+ "cache_control": {"type": "ephemeral", "ttl": "1h"},
+ },
+ ],
+ }]
- .. important::
- Specifying a `ttl` key under `cache_control` will not work unless the
- beta header is set!
+ response = llm.invoke(messages)
Details of cached token counts will be included on the ``InputTokenDetails``
of response's ``usage_metadata``:
@@ -1432,23 +1435,6 @@ class ChatAnthropic(BaseChatModel):
# If cache_control is provided in kwargs, add it to last message
# and content block.
if "cache_control" in kwargs and formatted_messages:
- cache_control = kwargs["cache_control"]
-
- # Validate TTL usage requires extended cache TTL beta header
- if (
- isinstance(cache_control, dict)
- and "ttl" in cache_control
- and (
- not self.betas or "extended-cache-ttl-2025-04-11" not in self.betas
- )
- ):
- msg = (
- "Specifying a 'ttl' under 'cache_control' requires enabling "
- "the 'extended-cache-ttl-2025-04-11' beta header. "
- "Set betas=['extended-cache-ttl-2025-04-11'] when initializing "
- "ChatAnthropic."
- )
- warnings.warn(msg, stacklevel=2)
if isinstance(formatted_messages[-1]["content"], list):
formatted_messages[-1]["content"][-1]["cache_control"] = kwargs.pop(
"cache_control"