From 5362bf5666356a97d7efb9e3d9ba3cffbf52cc1d Mon Sep 17 00:00:00 2001 From: ccurme Date: Thu, 19 Feb 2026 21:01:49 -0500 Subject: [PATCH] fix(anthropic): set max input tokens based on 1m context beta header (#35341) --- .../langchain_anthropic/chat_models.py | 6 ++++++ .../tests/unit_tests/test_chat_models.py | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/libs/partners/anthropic/langchain_anthropic/chat_models.py b/libs/partners/anthropic/langchain_anthropic/chat_models.py index e1abc27e8d9..c22b76df86b 100644 --- a/libs/partners/anthropic/langchain_anthropic/chat_models.py +++ b/libs/partners/anthropic/langchain_anthropic/chat_models.py @@ -1024,6 +1024,12 @@ class ChatAnthropic(BaseChatModel): """Set model profile if not overridden.""" if self.profile is None: self.profile = _get_default_model_profile(self.model) + if ( + self.profile is not None + and self.betas + and "context-1m-2025-08-07" in self.betas + ): + self.profile["max_input_tokens"] = 1_000_000 return self @cached_property diff --git a/libs/partners/anthropic/tests/unit_tests/test_chat_models.py b/libs/partners/anthropic/tests/unit_tests/test_chat_models.py index 1eb74c71008..d4f894e4e7c 100644 --- a/libs/partners/anthropic/tests/unit_tests/test_chat_models.py +++ b/libs/partners/anthropic/tests/unit_tests/test_chat_models.py @@ -2171,6 +2171,23 @@ def test_profile() -> None: assert model.profile == {"tool_calling": False} +def test_profile_1m_context_beta() -> None: + model = ChatAnthropic(model="claude-sonnet-4-5") + assert model.profile + assert model.profile["max_input_tokens"] == 200000 + + model = ChatAnthropic(model="claude-sonnet-4-5", betas=["context-1m-2025-08-07"]) + assert model.profile + assert model.profile["max_input_tokens"] == 1000000 + + model = ChatAnthropic( + model="claude-sonnet-4-5", + betas=["token-efficient-tools-2025-02-19"], + ) + assert model.profile + assert model.profile["max_input_tokens"] == 200000 + + async def test_model_profile_not_blocking() -> None: with blockbuster_ctx(): model = ChatAnthropic(model="claude-sonnet-4-5")