From 840c3dfa5176f801885639a9ad40aa9a3b5fdf47 Mon Sep 17 00:00:00 2001 From: hx <1367557521@qq.com> Date: Sun, 27 Sep 2026 18:05:36 +0800 Subject: [PATCH 1/3] feat(anthropic): auto-inject context-1m beta header for [1m] model suffix A model name ending in [1m] (case-insensitive) now adds the context-1m-2025-08-07 anthropic-beta value and is sent upstream without the suffix, on both the chat completions and the /v1/messages paths Ported onto the current default branch: the pass-through module moved from experimental_pass_through to pass_through and the tests moved to tests/unit --- litellm/llms/anthropic/chat/transformation.py | 32 +++- litellm/llms/anthropic/common_utils.py | 49 +++++- .../messages/native/transformation.py | 15 +- .../llms/anthropic/test_context_1m_header.py | 144 ++++++++++++++++++ 4 files changed, 235 insertions(+), 5 deletions(-) create mode 100644 tests/unit/llms/anthropic/test_context_1m_header.py diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py index f198d76f72a..d564cef5034 100644 --- a/litellm/llms/anthropic/chat/transformation.py +++ b/litellm/llms/anthropic/chat/transformation.py @@ -130,12 +130,14 @@ from ..common_utils import ( STOP_REASON_FIELDS, AnthropicError, AnthropicModelInfo, + context_1m_requested, eager_input_streaming_flag, process_anthropic_headers, refusal_params, requires_native_compaction_beta, stop_reason_provider_specific_fields, strip_advisor_blocks_from_messages, + strip_context_1m_suffix, tools_marked_for_a_tool_config_point, ) @@ -2221,6 +2223,17 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): ) return tools + def _maybe_add_context_1m_beta( + self, + headers: dict, + *, + model: str, + optional_params: dict, + litellm_params: Mapping[str, object] | None, + ) -> None: + if context_1m_requested(model=model, optional_params=optional_params, litellm_params=litellm_params): + self._ensure_beta_header(headers, "context-1m-2025-08-07") + def _ensure_beta_header(self, headers: dict[str, str], beta_value: str) -> None: """ Ensure a beta header value is present in the anthropic-beta header. @@ -2279,7 +2292,12 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): ) def update_headers_with_optional_anthropic_beta( - self, headers: dict, optional_params: dict, messages: Sequence[object] = () + self, + headers: dict, + optional_params: dict, + messages: Sequence[object] = (), + litellm_params: Mapping[str, object] | None = None, + model: str = "", ) -> dict: """Update headers with optional anthropic beta.""" @@ -2292,6 +2310,10 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): if requires_native_compaction_beta(self._resolved_provider, optional_params, messages): self._ensure_beta_header(headers, ANTHROPIC_BETA_HEADER_VALUES.COMPACT_2026_09_04.value) + self._maybe_add_context_1m_beta( + headers, model=model, optional_params=optional_params, litellm_params=litellm_params + ) + _tools: Final = optional_params.get("tools", []) for tool in _tools: if tool.get("type", None) and tool.get("type").startswith(ANTHROPIC_HOSTED_TOOLS.WEB_FETCH.value): @@ -2475,7 +2497,11 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): ) # don't use verbose_logger.exception, if exception is raised self.update_headers_with_optional_anthropic_beta( - headers=headers, optional_params=optional_params, messages=anthropic_messages + headers=headers, + optional_params=optional_params, + messages=anthropic_messages, + litellm_params=litellm_params, + model=model, ) ## Auto-strip advisor blocks from history if advisor tool is absent. @@ -2563,7 +2589,7 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): request_params.pop("output_format") data: Final = { - "model": model, + "model": strip_context_1m_suffix(model), "messages": anthropic_messages, **optional_params, **({} if marked_tools is None else {"tools": list(marked_tools)}), diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.py index c9df903cbd6..2269d7ee14e 100644 --- a/litellm/llms/anthropic/common_utils.py +++ b/litellm/llms/anthropic/common_utils.py @@ -121,6 +121,46 @@ _BEDROCK_VERSION_SUFFIX_RE: Final = re.compile(r"-v\d+(?::\d+)?$") _INFERENCE_PROFILE_MINOR_RE: Final = re.compile(r":\d+$") _DATED_RELEASE_SUFFIX_RE: Final = re.compile(r"-\d{8}$") _DOTTED_VERSION_RE: Final = re.compile(r"(\d)\.(\d)") +_CONTEXT_1M_SUFFIX: Final = re.compile(r"\[1m\]$", flags=re.IGNORECASE) + + +def model_has_context_1m_suffix(model: object) -> bool: + return isinstance(model, str) and _CONTEXT_1M_SUFFIX.search(model) is not None + + +def strip_context_1m_suffix(model: str) -> str: + return _CONTEXT_1M_SUFFIX.sub("", model) + + +def _original_model_from(params: object) -> object: + if not isinstance(params, Mapping): + return None + return params.get("_original_model") + + +def context_1m_requested( + *, + model: object = "", + optional_params: object = None, + litellm_params: object = None, +) -> bool: + return any( + model_has_context_1m_suffix(candidate) + for candidate in ( + model, + _original_model_from(optional_params), + _original_model_from(litellm_params), + ) + ) + + +_CONTEXT_1M_BETA: Final = "context-1m-2025-08-07" + + +def context_1m_beta_values(supported: bool) -> frozenset[str]: + return frozenset((_CONTEXT_1M_BETA,)) if supported else frozenset() + + _CLAUDE_CODE_BILLING_HEADER_PREFIX: Final = "x-anthropic-billing-header:" OBJECT_MAPPING_ADAPTER: Final = TypeAdapter(dict[object, object]) OBJECT_LIST_ADAPTER: Final = TypeAdapter(list[object]) @@ -1429,8 +1469,9 @@ class AnthropicModelInfo(BaseLLMModelInfo): thinking: AnthropicThinkingParam | None = None, is_mid_conversation_tool_change_used: bool = False, wif_minted: bool = False, + context_1m_supported: bool = False, ) -> dict: - betas: Final = set() + betas: Final = set(context_1m_beta_values(context_1m_supported)) # Anthropic no longer requires the prompt-caching beta header # Prompt caching now works automatically when cache_control is used in messages # Reference: https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching @@ -1563,8 +1604,14 @@ class AnthropicModelInfo(BaseLLMModelInfo): user_anthropic_beta_headers: Final = self._get_user_anthropic_beta_headers( anthropic_beta_header=headers.get("anthropic-beta") ) + context_1m_supported: Final = context_1m_requested( + model=model, + optional_params=optional_params, + litellm_params=litellm_params, + ) anthropic_headers: Final = self.get_anthropic_headers( computer_tool_used=computer_tool_used, + context_1m_supported=context_1m_supported, prompt_caching_set=prompt_caching_set, pdf_used=pdf_used, api_key=resolved_api_key, diff --git a/litellm/llms/anthropic/messages/native/transformation.py b/litellm/llms/anthropic/messages/native/transformation.py index 5281ad99620..d92586acc31 100644 --- a/litellm/llms/anthropic/messages/native/transformation.py +++ b/litellm/llms/anthropic/messages/native/transformation.py @@ -48,9 +48,12 @@ from ...common_utils import ( context_management_betas, is_claude_model_target, merge_anthropic_beta_headers, + context_1m_beta_values, + context_1m_requested, optionally_handle_anthropic_oauth, requires_native_compaction_beta, strip_advisor_blocks_from_messages, + strip_context_1m_suffix, strip_encrypted_reasoning_blocks_from_anthropic_messages, tools_without_custom_field, ) @@ -462,6 +465,8 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): headers=headers, optional_params=optional_params, messages=messages, + model=model, + litellm_params=litellm_params, ) @staticmethod @@ -792,7 +797,7 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): ) ), max_tokens=max_tokens, - model=model, + model=strip_context_1m_suffix(model), **anthropic_messages_optional_request_params, ) request: Final = ( @@ -853,6 +858,8 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): messages: Sequence[object] = (), beta_map_provider: str | None = None, drop_params: bool = False, + model: str = "", + litellm_params: object = None, ) -> dict: """ Auto-inject anthropic-beta headers based on features used. @@ -863,6 +870,7 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): - output_format: adds 'structured-outputs-2025-11-13' - speed: adds 'fast-mode-2026-02-01' - a message carrying output_config: adds 'per-turn-control-2026-07-01' + - [1m] suffix: adds 'context-1m-2025-08-07' Args: headers: Request headers dict @@ -886,6 +894,11 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): if requires_native_compaction_beta(custom_llm_provider, optional_params, messages): beta_values.add(ANTHROPIC_BETA_HEADER_VALUES.COMPACT_2026_09_04.value) + beta_values.update( + context_1m_beta_values( + context_1m_requested(model=model, optional_params=optional_params, litellm_params=litellm_params) + ) + ) beta_values.update(context_management_betas(optional_params.get("context_management"))) # Check for structured outputs. Anthropic's newer request shape nests diff --git a/tests/unit/llms/anthropic/test_context_1m_header.py b/tests/unit/llms/anthropic/test_context_1m_header.py new file mode 100644 index 00000000000..505adb42501 --- /dev/null +++ b/tests/unit/llms/anthropic/test_context_1m_header.py @@ -0,0 +1,144 @@ +from litellm.llms.anthropic.chat.transformation import AnthropicConfig +from litellm.llms.anthropic.common_utils import ( + context_1m_requested, + strip_context_1m_suffix, +) +from litellm.llms.anthropic.pass_through.messages.transformation import ( + AnthropicMessagesConfig, +) + + +class TestAnthropicContext1mBetaHeader: + def test_strip_context_1m_suffix_is_pure(self): + assert strip_context_1m_suffix("claude-sonnet-4-6[1m]") == "claude-sonnet-4-6" + assert strip_context_1m_suffix("claude-sonnet-4-6[1M]") == "claude-sonnet-4-6" + assert strip_context_1m_suffix("claude-sonnet-4-6") == "claude-sonnet-4-6" + assert strip_context_1m_suffix("gpt-4o[1m]") == "gpt-4o" + + def test_context_1m_requested_from_model_or_stashed_original(self): + assert context_1m_requested(model="claude-sonnet-4-6[1m]") + assert context_1m_requested( + model="claude-sonnet-4-6", + litellm_params={"_original_model": "claude-sonnet-4-6[1M]"}, + ) + assert not context_1m_requested(model="claude-sonnet-4-6") + assert not context_1m_requested(model="gpt-4o") + + def test_get_anthropic_headers_with_context_1m(self): + headers = AnthropicConfig().get_anthropic_headers(api_key="sk-test", context_1m_supported=True) + assert "context-1m-2025-08-07" in headers.get("anthropic-beta", "") + + def test_get_anthropic_headers_without_context_1m(self): + headers = AnthropicConfig().get_anthropic_headers(api_key="sk-test", context_1m_supported=False) + assert "context-1m-2025-08-07" not in headers.get("anthropic-beta", "") + + def test_validate_environment_adds_context_1m_for_suffixed_model(self): + headers = AnthropicConfig().validate_environment( + headers={}, + model="claude-sonnet-4-6[1m]", + messages=[{"role": "user", "content": "hi"}], + optional_params={}, + litellm_params={}, + api_key="sk-test", + ) + assert "context-1m-2025-08-07" in headers.get("anthropic-beta", "") + + def test_validate_environment_no_context_1m_without_suffix(self): + headers = AnthropicConfig().validate_environment( + headers={}, + model="claude-sonnet-4-6", + messages=[{"role": "user", "content": "hi"}], + optional_params={}, + litellm_params={}, + api_key="sk-test", + ) + assert "context-1m-2025-08-07" not in headers.get("anthropic-beta", "") + + def test_validate_environment_uses_original_model_from_litellm_params(self): + headers = AnthropicConfig().validate_environment( + headers={}, + model="claude-sonnet-4-6", + messages=[{"role": "user", "content": "hi"}], + optional_params={}, + litellm_params={"_original_model": "claude-sonnet-4-6[1M]"}, + api_key="sk-test", + ) + assert "context-1m-2025-08-07" in headers.get("anthropic-beta", "") + + def test_update_headers_adds_context_1m_with_original_model(self): + headers = AnthropicConfig().update_headers_with_optional_anthropic_beta( + headers={}, + optional_params={"_original_model": "claude-sonnet-4-6[1m]"}, + ) + assert "context-1m-2025-08-07" in headers.get("anthropic-beta", "") + + def test_update_headers_no_context_1m_without_original_model(self): + headers = AnthropicConfig().update_headers_with_optional_anthropic_beta( + headers={}, + optional_params={}, + ) + assert "context-1m-2025-08-07" not in headers.get("anthropic-beta", "") + + def test_pass_through_adds_context_1m(self): + headers = AnthropicMessagesConfig._update_headers_with_anthropic_beta( + headers={}, + optional_params={"_original_model": "claude-sonnet-4-6[1m]"}, + ) + assert "context-1m-2025-08-07" in headers.get("anthropic-beta", "") + + def test_pass_through_no_context_1m_without_suffix(self): + headers = AnthropicMessagesConfig._update_headers_with_anthropic_beta( + headers={}, + optional_params={}, + ) + assert "context-1m-2025-08-07" not in headers.get("anthropic-beta", "") + + def test_pass_through_adds_context_1m_from_model_without_original_model(self): + headers, _ = AnthropicMessagesConfig().validate_anthropic_messages_environment( + headers={}, + model="claude-sonnet-4-6[1m]", + messages=[{"role": "user", "content": "hi"}], + optional_params={}, + litellm_params={}, + api_key="sk-test", + ) + assert "context-1m-2025-08-07" in headers.get("anthropic-beta", "") + + def test_pass_through_transform_strips_1m_from_upstream_model(self): + result = AnthropicMessagesConfig().transform_anthropic_messages_request( + model="claude-sonnet-4-6[1m]", + messages=[{"role": "user", "content": "hi"}], + anthropic_messages_optional_request_params={"max_tokens": 16}, + litellm_params={}, + headers={}, + ) + assert result["model"] == "claude-sonnet-4-6" + + def test_chat_transform_request_strips_1m_and_injects_header(self): + headers = {} + result = AnthropicConfig().transform_request( + model="claude-sonnet-4-6[1m]", + messages=[{"role": "user", "content": "hi"}], + optional_params={"max_tokens": 16}, + litellm_params={}, + headers=headers, + ) + assert result["model"] == "claude-sonnet-4-6" + assert "context-1m-2025-08-07" in headers.get("anthropic-beta", "") + + def test_chat_transform_request_uppercase_suffix_strips_and_injects_header(self): + headers = {} + result = AnthropicConfig().transform_request( + model="claude-sonnet-4-6[1M]", + messages=[{"role": "user", "content": "hi"}], + optional_params={"max_tokens": 16}, + litellm_params={}, + headers=headers, + ) + assert result["model"] == "claude-sonnet-4-6" + assert "context-1m-2025-08-07" in headers.get("anthropic-beta", "") + + def test_proxy_does_not_globally_strip_non_anthropic_suffix(self): + from litellm.proxy import route_llm_request + + assert not hasattr(route_llm_request, "stash_and_strip_context_1m_model_suffix") From dc8f825b0da106af4901376a7f570c6a79e53d7c Mon Sep 17 00:00:00 2001 From: hx <1367557521@qq.com> Date: Sun, 27 Sep 2026 18:05:43 +0800 Subject: [PATCH 2/3] fix(anthropic): normalize [1m] models before capability lookups Review follow-ups for the context-1m change The chat transform now treats a [1m] request exactly like its base model, so model-info lookups (default max_tokens, mid-conversation system support, thinking and speed handling) no longer miss on the suffixed name. Before this, anthropic/claude-sonnet-4-6[1m] silently fell back to a 4096 max_tokens default Detection now reads only the model string. The _original_model stash was never populated once proxy-level stripping was dropped, so those lookups and the duplicate injection in update_headers_with_optional_anthropic_beta are removed. The beta value lives in ANTHROPIC_BETA_HEADER_VALUES Tests move to the mirrored unit test files and cover litellm.acompletion and Router.aanthropic_messages end to end with the HTTP layer mocked --- litellm/llms/anthropic/chat/transformation.py | 41 ++--- litellm/llms/anthropic/common_utils.py | 46 +----- .../messages/native/transformation.py | 21 ++- litellm/types/llms/anthropic.py | 1 + .../test_anthropic_chat_transformation.py | 56 +++++++ .../test_anthropic_messages_context_1m.py | 57 +++++++ .../anthropic/test_anthropic_common_utils.py | 39 +++++ .../llms/anthropic/test_context_1m_header.py | 144 ------------------ 8 files changed, 182 insertions(+), 223 deletions(-) create mode 100644 tests/unit/llms/anthropic/messages/native/test_anthropic_messages_context_1m.py delete mode 100644 tests/unit/llms/anthropic/test_context_1m_header.py diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py index d564cef5034..7a1262e87c5 100644 --- a/litellm/llms/anthropic/chat/transformation.py +++ b/litellm/llms/anthropic/chat/transformation.py @@ -130,8 +130,8 @@ from ..common_utils import ( STOP_REASON_FIELDS, AnthropicError, AnthropicModelInfo, - context_1m_requested, eager_input_streaming_flag, + has_context_1m_suffix, process_anthropic_headers, refusal_params, requires_native_compaction_beta, @@ -2223,17 +2223,6 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): ) return tools - def _maybe_add_context_1m_beta( - self, - headers: dict, - *, - model: str, - optional_params: dict, - litellm_params: Mapping[str, object] | None, - ) -> None: - if context_1m_requested(model=model, optional_params=optional_params, litellm_params=litellm_params): - self._ensure_beta_header(headers, "context-1m-2025-08-07") - def _ensure_beta_header(self, headers: dict[str, str], beta_value: str) -> None: """ Ensure a beta header value is present in the anthropic-beta header. @@ -2292,12 +2281,7 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): ) def update_headers_with_optional_anthropic_beta( - self, - headers: dict, - optional_params: dict, - messages: Sequence[object] = (), - litellm_params: Mapping[str, object] | None = None, - model: str = "", + self, headers: dict, optional_params: dict, messages: Sequence[object] = () ) -> dict: """Update headers with optional anthropic beta.""" @@ -2310,10 +2294,6 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): if requires_native_compaction_beta(self._resolved_provider, optional_params, messages): self._ensure_beta_header(headers, ANTHROPIC_BETA_HEADER_VALUES.COMPACT_2026_09_04.value) - self._maybe_add_context_1m_beta( - headers, model=model, optional_params=optional_params, litellm_params=litellm_params - ) - _tools: Final = optional_params.get("tools", []) for tool in _tools: if tool.get("type", None) and tool.get("type").startswith(ANTHROPIC_HOSTED_TOOLS.WEB_FETCH.value): @@ -2378,6 +2358,15 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): """ Translate messages to anthropic format. """ + if has_context_1m_suffix(model): + return AnthropicConfig.transform_request( + self, + model=strip_context_1m_suffix(model), + messages=messages, + optional_params=optional_params, + litellm_params=litellm_params, + headers=headers, + ) ## VALIDATE REQUEST """Anthropic requires ``tools`` when messages include tool blocks; LiteLLM injects a dummy tool if omitted (no ``modify_params`` needed).""" from litellm.litellm_core_utils.prompt_templates.factory import ( @@ -2497,11 +2486,7 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): ) # don't use verbose_logger.exception, if exception is raised self.update_headers_with_optional_anthropic_beta( - headers=headers, - optional_params=optional_params, - messages=anthropic_messages, - litellm_params=litellm_params, - model=model, + headers=headers, optional_params=optional_params, messages=anthropic_messages ) ## Auto-strip advisor blocks from history if advisor tool is absent. @@ -2589,7 +2574,7 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): request_params.pop("output_format") data: Final = { - "model": strip_context_1m_suffix(model), + "model": model, "messages": anthropic_messages, **optional_params, **({} if marked_tools is None else {"tools": list(marked_tools)}), diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.py index 2269d7ee14e..d79ce6b9bec 100644 --- a/litellm/llms/anthropic/common_utils.py +++ b/litellm/llms/anthropic/common_utils.py @@ -124,43 +124,14 @@ _DOTTED_VERSION_RE: Final = re.compile(r"(\d)\.(\d)") _CONTEXT_1M_SUFFIX: Final = re.compile(r"\[1m\]$", flags=re.IGNORECASE) -def model_has_context_1m_suffix(model: object) -> bool: - return isinstance(model, str) and _CONTEXT_1M_SUFFIX.search(model) is not None +def has_context_1m_suffix(model: str) -> bool: + return _CONTEXT_1M_SUFFIX.search(model) is not None def strip_context_1m_suffix(model: str) -> str: return _CONTEXT_1M_SUFFIX.sub("", model) -def _original_model_from(params: object) -> object: - if not isinstance(params, Mapping): - return None - return params.get("_original_model") - - -def context_1m_requested( - *, - model: object = "", - optional_params: object = None, - litellm_params: object = None, -) -> bool: - return any( - model_has_context_1m_suffix(candidate) - for candidate in ( - model, - _original_model_from(optional_params), - _original_model_from(litellm_params), - ) - ) - - -_CONTEXT_1M_BETA: Final = "context-1m-2025-08-07" - - -def context_1m_beta_values(supported: bool) -> frozenset[str]: - return frozenset((_CONTEXT_1M_BETA,)) if supported else frozenset() - - _CLAUDE_CODE_BILLING_HEADER_PREFIX: Final = "x-anthropic-billing-header:" OBJECT_MAPPING_ADAPTER: Final = TypeAdapter(dict[object, object]) OBJECT_LIST_ADAPTER: Final = TypeAdapter(list[object]) @@ -1469,9 +1440,11 @@ class AnthropicModelInfo(BaseLLMModelInfo): thinking: AnthropicThinkingParam | None = None, is_mid_conversation_tool_change_used: bool = False, wif_minted: bool = False, - context_1m_supported: bool = False, + context_1m_requested: bool = False, ) -> dict: - betas: Final = set(context_1m_beta_values(context_1m_supported)) + betas: Final[set[str]] = set() + if context_1m_requested: + betas.add(ANTHROPIC_BETA_HEADER_VALUES.CONTEXT_1M_2025_08_07.value) # Anthropic no longer requires the prompt-caching beta header # Prompt caching now works automatically when cache_control is used in messages # Reference: https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching @@ -1604,14 +1577,9 @@ class AnthropicModelInfo(BaseLLMModelInfo): user_anthropic_beta_headers: Final = self._get_user_anthropic_beta_headers( anthropic_beta_header=headers.get("anthropic-beta") ) - context_1m_supported: Final = context_1m_requested( - model=model, - optional_params=optional_params, - litellm_params=litellm_params, - ) anthropic_headers: Final = self.get_anthropic_headers( computer_tool_used=computer_tool_used, - context_1m_supported=context_1m_supported, + context_1m_requested=has_context_1m_suffix(model), prompt_caching_set=prompt_caching_set, pdf_used=pdf_used, api_key=resolved_api_key, diff --git a/litellm/llms/anthropic/messages/native/transformation.py b/litellm/llms/anthropic/messages/native/transformation.py index d92586acc31..c443d57c7d9 100644 --- a/litellm/llms/anthropic/messages/native/transformation.py +++ b/litellm/llms/anthropic/messages/native/transformation.py @@ -46,10 +46,9 @@ from ...common_utils import ( AnthropicError, AnthropicModelInfo, context_management_betas, + has_context_1m_suffix, is_claude_model_target, merge_anthropic_beta_headers, - context_1m_beta_values, - context_1m_requested, optionally_handle_anthropic_oauth, requires_native_compaction_beta, strip_advisor_blocks_from_messages, @@ -377,7 +376,7 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): model=model, ), ) - return self.finalize_messages_headers(headers, optional_params, messages), api_base + return self.finalize_messages_headers(headers, optional_params, messages, model=model), api_base async def avalidate_anthropic_messages_environment( self, @@ -417,7 +416,7 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): model=model, ), ) - return self.finalize_messages_headers(oauth_headers, optional_params, messages), api_base + return self.finalize_messages_headers(oauth_headers, optional_params, messages, model=model), api_base def _require_auth_header(self, auth_header: Mapping[str, str] | None, model: str) -> Mapping[str, str]: if auth_header is None: @@ -456,6 +455,7 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): headers: dict, # mutable-ok: out-param optional_params: dict, # mutable-ok: out-param messages: list[object], # mutable-ok: mirrors the validate_anthropic_messages_environment contract + model: str = "", ) -> dict: # mutable-ok: out-param if "anthropic-version" not in headers: headers["anthropic-version"] = DEFAULT_ANTHROPIC_API_VERSION @@ -466,7 +466,6 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): optional_params=optional_params, messages=messages, model=model, - litellm_params=litellm_params, ) @staticmethod @@ -859,7 +858,6 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): beta_map_provider: str | None = None, drop_params: bool = False, model: str = "", - litellm_params: object = None, ) -> dict: """ Auto-inject anthropic-beta headers based on features used. @@ -870,7 +868,7 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): - output_format: adds 'structured-outputs-2025-11-13' - speed: adds 'fast-mode-2026-02-01' - a message carrying output_config: adds 'per-turn-control-2026-07-01' - - [1m] suffix: adds 'context-1m-2025-08-07' + - [1m] model suffix: adds 'context-1m-2025-08-07' Args: headers: Request headers dict @@ -879,6 +877,7 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): messages: Request messages, scanned for per-message output_config beta_map_provider: Provider whose beta map column gates safeguards, defaults to custom_llm_provider drop_params: Whether the deployment or request set drop_params + model: Requested model name, checked for the [1m] suffix """ beta_values: Final[set[str]] = set() @@ -894,11 +893,9 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): if requires_native_compaction_beta(custom_llm_provider, optional_params, messages): beta_values.add(ANTHROPIC_BETA_HEADER_VALUES.COMPACT_2026_09_04.value) - beta_values.update( - context_1m_beta_values( - context_1m_requested(model=model, optional_params=optional_params, litellm_params=litellm_params) - ) - ) + if has_context_1m_suffix(model): + beta_values.add(ANTHROPIC_BETA_HEADER_VALUES.CONTEXT_1M_2025_08_07.value) + beta_values.update(context_management_betas(optional_params.get("context_management"))) # Check for structured outputs. Anthropic's newer request shape nests diff --git a/litellm/types/llms/anthropic.py b/litellm/types/llms/anthropic.py index dc35af81091..d7aca811a02 100644 --- a/litellm/types/llms/anthropic.py +++ b/litellm/types/llms/anthropic.py @@ -840,6 +840,7 @@ class ANTHROPIC_BETA_HEADER_VALUES(str, Enum): WEB_FETCH_2025_09_10 = "web-fetch-2025-09-10" WEB_SEARCH_2025_03_05 = "web-search-2025-03-05" CONTEXT_MANAGEMENT_2025_06_27 = "context-management-2025-06-27" + CONTEXT_1M_2025_08_07 = "context-1m-2025-08-07" COMPACT_2026_01_12 = "compact-2026-01-12" COMPACT_2026_09_04 = "compact-2026-09-04" INLINE_TOOLS_2026_09_15 = "inline-tools-2026-09-15" diff --git a/tests/unit/llms/anthropic/chat/test_anthropic_chat_transformation.py b/tests/unit/llms/anthropic/chat/test_anthropic_chat_transformation.py index 63a304ab77d..7b58ded6972 100644 --- a/tests/unit/llms/anthropic/chat/test_anthropic_chat_transformation.py +++ b/tests/unit/llms/anthropic/chat/test_anthropic_chat_transformation.py @@ -10571,3 +10571,59 @@ def test_claude_cache_control_cap_does_not_count_a_message_level_hint_that_list_ assert sorted(_cached_texts(body)) == ["system prompt", "turn 1", "turn 2", "turn 3"] assert json.dumps(body).count('"cache_control"') == 4 + + +@pytest.mark.parametrize("optional_params", [{}, {"thinking": {"type": "enabled", "budget_tokens": 2048}}]) +def test_transform_request_treats_context_1m_model_as_its_base_model(optional_params: dict[str, object]) -> None: + def transform(model: str) -> dict[str, object]: + return AnthropicConfig().transform_request( + model=model, + messages=[{"role": "user", "content": "hi"}], + optional_params=copy.deepcopy(optional_params), + litellm_params={}, + headers={}, + ) + + assert transform("claude-sonnet-4-6[1m]") == transform("claude-sonnet-4-6") + + +@pytest.mark.parametrize( + "model,expected_betas", + [ + ("anthropic/claude-sonnet-4-6[1m]", {"context-1m-2025-08-07", "interleaved-thinking-2025-05-14"}), + ("anthropic/claude-sonnet-4-6", {"interleaved-thinking-2025-05-14"}), + ], +) +async def test_acompletion_context_1m_suffix_sends_base_model_with_beta( + model: str, + expected_betas: set[str], + monkeypatch: pytest.MonkeyPatch, + respx_mock: respx.MockRouter, +) -> None: + monkeypatch.setenv("DISABLE_AIOHTTP_TRANSPORT", "True") + monkeypatch.setenv("LITELLM_LOCAL_ANTHROPIC_BETA_HEADERS", "True") + monkeypatch.setattr(litellm.anthropic_beta_headers_manager, "_BETA_HEADERS_CONFIG", None) + route: Final = respx_mock.post("https://api.anthropic.com/v1/messages").respond( + 200, + json={ + "id": "msg_context_1m", + "type": "message", + "role": "assistant", + "model": "claude-sonnet-4-6", + "content": [{"type": "text", "text": "hi"}], + "stop_reason": "end_turn", + "usage": {"input_tokens": 1, "output_tokens": 1}, + }, + ) + + await litellm.acompletion( + model=model, + messages=[{"role": "user", "content": "hi"}], + api_key="test", + extra_headers={"anthropic-beta": "interleaved-thinking-2025-05-14"}, + ) + + request: Final = route.calls.last.request + assert json.loads(request.content)["model"] == "claude-sonnet-4-6" + assert {beta.strip() for beta in request.headers["anthropic-beta"].split(",")} == expected_betas + diff --git a/tests/unit/llms/anthropic/messages/native/test_anthropic_messages_context_1m.py b/tests/unit/llms/anthropic/messages/native/test_anthropic_messages_context_1m.py new file mode 100644 index 00000000000..4525769a7cf --- /dev/null +++ b/tests/unit/llms/anthropic/messages/native/test_anthropic_messages_context_1m.py @@ -0,0 +1,57 @@ +import json +from typing import Final + +import pytest +import respx + +import litellm +from litellm import Router + + +@pytest.mark.parametrize( + "deployment_model,expected_betas", + [ + ("anthropic/claude-sonnet-4-6[1m]", {"context-1m-2025-08-07", "interleaved-thinking-2025-05-14"}), + ("anthropic/claude-sonnet-4-6", {"interleaved-thinking-2025-05-14"}), + ], +) +async def test_router_messages_context_1m_deployment_sends_base_model_with_beta( + deployment_model: str, + expected_betas: set[str], + monkeypatch: pytest.MonkeyPatch, + respx_mock: respx.MockRouter, +) -> None: + monkeypatch.setenv("DISABLE_AIOHTTP_TRANSPORT", "True") + monkeypatch.setenv("LITELLM_LOCAL_ANTHROPIC_BETA_HEADERS", "True") + monkeypatch.setattr(litellm.anthropic_beta_headers_manager, "_BETA_HEADERS_CONFIG", None) + route: Final = respx_mock.post("https://api.anthropic.com/v1/messages").respond( + 200, + json={ + "id": "msg_context_1m", + "type": "message", + "role": "assistant", + "model": "claude-sonnet-4-6", + "content": [{"type": "text", "text": "hi"}], + "stop_reason": "end_turn", + "usage": {"input_tokens": 1, "output_tokens": 1}, + }, + ) + router: Final = Router( + model_list=[ + { + "model_name": "claude-sonnet-4-6[1m]", + "litellm_params": {"model": deployment_model, "api_key": "test"}, + } + ] + ) + + await router.aanthropic_messages( + model="claude-sonnet-4-6[1m]", + messages=[{"role": "user", "content": "hi"}], + max_tokens=16, + extra_headers={"anthropic-beta": "interleaved-thinking-2025-05-14"}, + ) + + request: Final = route.calls.last.request + assert json.loads(request.content)["model"] == "claude-sonnet-4-6" + assert {beta.strip() for beta in request.headers["anthropic-beta"].split(",")} == expected_betas diff --git a/tests/unit/llms/anthropic/test_anthropic_common_utils.py b/tests/unit/llms/anthropic/test_anthropic_common_utils.py index cf0b8a39865..5c78e7f2bc0 100644 --- a/tests/unit/llms/anthropic/test_anthropic_common_utils.py +++ b/tests/unit/llms/anthropic/test_anthropic_common_utils.py @@ -4728,3 +4728,42 @@ def test_tools_without_strict_drops_only_the_strict_key_and_counts_the_strict_to assert tools_without_strict(tools) == ToolsWithoutStrict( tools=({"name": "a"}, {"name": "b"}, {"name": "c"}), dropped_count=1 ) + + +@pytest.mark.parametrize( + "model,base_model", + [ + ("claude-sonnet-4-6[1m]", "claude-sonnet-4-6"), + ("claude-sonnet-4-6[1M]", "claude-sonnet-4-6"), + ("claude-sonnet-4-6", "claude-sonnet-4-6"), + ("claude-[1m]-sonnet-4-6", "claude-[1m]-sonnet-4-6"), + ("claude-sonnet-4-6[1m][1m]", "claude-sonnet-4-6[1m]"), + ("claude-sonnet-4-6[200k]", "claude-sonnet-4-6[200k]"), + ], +) +def test_context_1m_suffix_is_only_a_trailing_marker(model: str, base_model: str) -> None: + from litellm.llms.anthropic.common_utils import has_context_1m_suffix, strip_context_1m_suffix + + assert strip_context_1m_suffix(model) == base_model + assert has_context_1m_suffix(model) is (model != base_model) + + +@pytest.mark.parametrize("model,expects_context_1m", [("claude-sonnet-4-6[1m]", True), ("claude-sonnet-4-6", False)]) +def test_validate_environment_adds_context_1m_beta_only_for_suffixed_model( + model: str, expects_context_1m: bool +) -> None: + from litellm.llms.anthropic.common_utils import AnthropicModelInfo + + headers: Final = AnthropicModelInfo().validate_environment( + headers={"anthropic-beta": "interleaved-thinking-2025-05-14"}, + model=model, + messages=[{"role": "user", "content": "hi"}], + optional_params={}, + litellm_params={}, + api_key=FAKE_REGULAR_KEY, + ) + + betas: Final = {beta.strip() for beta in headers["anthropic-beta"].split(",")} + assert ("context-1m-2025-08-07" in betas) is expects_context_1m + assert "interleaved-thinking-2025-05-14" in betas + diff --git a/tests/unit/llms/anthropic/test_context_1m_header.py b/tests/unit/llms/anthropic/test_context_1m_header.py deleted file mode 100644 index 505adb42501..00000000000 --- a/tests/unit/llms/anthropic/test_context_1m_header.py +++ /dev/null @@ -1,144 +0,0 @@ -from litellm.llms.anthropic.chat.transformation import AnthropicConfig -from litellm.llms.anthropic.common_utils import ( - context_1m_requested, - strip_context_1m_suffix, -) -from litellm.llms.anthropic.pass_through.messages.transformation import ( - AnthropicMessagesConfig, -) - - -class TestAnthropicContext1mBetaHeader: - def test_strip_context_1m_suffix_is_pure(self): - assert strip_context_1m_suffix("claude-sonnet-4-6[1m]") == "claude-sonnet-4-6" - assert strip_context_1m_suffix("claude-sonnet-4-6[1M]") == "claude-sonnet-4-6" - assert strip_context_1m_suffix("claude-sonnet-4-6") == "claude-sonnet-4-6" - assert strip_context_1m_suffix("gpt-4o[1m]") == "gpt-4o" - - def test_context_1m_requested_from_model_or_stashed_original(self): - assert context_1m_requested(model="claude-sonnet-4-6[1m]") - assert context_1m_requested( - model="claude-sonnet-4-6", - litellm_params={"_original_model": "claude-sonnet-4-6[1M]"}, - ) - assert not context_1m_requested(model="claude-sonnet-4-6") - assert not context_1m_requested(model="gpt-4o") - - def test_get_anthropic_headers_with_context_1m(self): - headers = AnthropicConfig().get_anthropic_headers(api_key="sk-test", context_1m_supported=True) - assert "context-1m-2025-08-07" in headers.get("anthropic-beta", "") - - def test_get_anthropic_headers_without_context_1m(self): - headers = AnthropicConfig().get_anthropic_headers(api_key="sk-test", context_1m_supported=False) - assert "context-1m-2025-08-07" not in headers.get("anthropic-beta", "") - - def test_validate_environment_adds_context_1m_for_suffixed_model(self): - headers = AnthropicConfig().validate_environment( - headers={}, - model="claude-sonnet-4-6[1m]", - messages=[{"role": "user", "content": "hi"}], - optional_params={}, - litellm_params={}, - api_key="sk-test", - ) - assert "context-1m-2025-08-07" in headers.get("anthropic-beta", "") - - def test_validate_environment_no_context_1m_without_suffix(self): - headers = AnthropicConfig().validate_environment( - headers={}, - model="claude-sonnet-4-6", - messages=[{"role": "user", "content": "hi"}], - optional_params={}, - litellm_params={}, - api_key="sk-test", - ) - assert "context-1m-2025-08-07" not in headers.get("anthropic-beta", "") - - def test_validate_environment_uses_original_model_from_litellm_params(self): - headers = AnthropicConfig().validate_environment( - headers={}, - model="claude-sonnet-4-6", - messages=[{"role": "user", "content": "hi"}], - optional_params={}, - litellm_params={"_original_model": "claude-sonnet-4-6[1M]"}, - api_key="sk-test", - ) - assert "context-1m-2025-08-07" in headers.get("anthropic-beta", "") - - def test_update_headers_adds_context_1m_with_original_model(self): - headers = AnthropicConfig().update_headers_with_optional_anthropic_beta( - headers={}, - optional_params={"_original_model": "claude-sonnet-4-6[1m]"}, - ) - assert "context-1m-2025-08-07" in headers.get("anthropic-beta", "") - - def test_update_headers_no_context_1m_without_original_model(self): - headers = AnthropicConfig().update_headers_with_optional_anthropic_beta( - headers={}, - optional_params={}, - ) - assert "context-1m-2025-08-07" not in headers.get("anthropic-beta", "") - - def test_pass_through_adds_context_1m(self): - headers = AnthropicMessagesConfig._update_headers_with_anthropic_beta( - headers={}, - optional_params={"_original_model": "claude-sonnet-4-6[1m]"}, - ) - assert "context-1m-2025-08-07" in headers.get("anthropic-beta", "") - - def test_pass_through_no_context_1m_without_suffix(self): - headers = AnthropicMessagesConfig._update_headers_with_anthropic_beta( - headers={}, - optional_params={}, - ) - assert "context-1m-2025-08-07" not in headers.get("anthropic-beta", "") - - def test_pass_through_adds_context_1m_from_model_without_original_model(self): - headers, _ = AnthropicMessagesConfig().validate_anthropic_messages_environment( - headers={}, - model="claude-sonnet-4-6[1m]", - messages=[{"role": "user", "content": "hi"}], - optional_params={}, - litellm_params={}, - api_key="sk-test", - ) - assert "context-1m-2025-08-07" in headers.get("anthropic-beta", "") - - def test_pass_through_transform_strips_1m_from_upstream_model(self): - result = AnthropicMessagesConfig().transform_anthropic_messages_request( - model="claude-sonnet-4-6[1m]", - messages=[{"role": "user", "content": "hi"}], - anthropic_messages_optional_request_params={"max_tokens": 16}, - litellm_params={}, - headers={}, - ) - assert result["model"] == "claude-sonnet-4-6" - - def test_chat_transform_request_strips_1m_and_injects_header(self): - headers = {} - result = AnthropicConfig().transform_request( - model="claude-sonnet-4-6[1m]", - messages=[{"role": "user", "content": "hi"}], - optional_params={"max_tokens": 16}, - litellm_params={}, - headers=headers, - ) - assert result["model"] == "claude-sonnet-4-6" - assert "context-1m-2025-08-07" in headers.get("anthropic-beta", "") - - def test_chat_transform_request_uppercase_suffix_strips_and_injects_header(self): - headers = {} - result = AnthropicConfig().transform_request( - model="claude-sonnet-4-6[1M]", - messages=[{"role": "user", "content": "hi"}], - optional_params={"max_tokens": 16}, - litellm_params={}, - headers=headers, - ) - assert result["model"] == "claude-sonnet-4-6" - assert "context-1m-2025-08-07" in headers.get("anthropic-beta", "") - - def test_proxy_does_not_globally_strip_non_anthropic_suffix(self): - from litellm.proxy import route_llm_request - - assert not hasattr(route_llm_request, "stash_and_strip_context_1m_model_suffix") From 4b1d761d14a7b3e79fc45b7362395a4b95412f6f Mon Sep 17 00:00:00 2001 From: hx <1367557521@qq.com> Date: Thu, 15 Oct 2026 19:22:55 +0800 Subject: [PATCH 3/3] test(anthropic): cover sync /v1/messages context-1m path and pin tests to our beta constant Add the context-1m beta through the immutable all_betas union instead of mutating the betas set, assert against ANTHROPIC_BETA_HEADER_VALUES rather than the vendor string, and move the tests to the latest Sonnet model --- litellm/llms/anthropic/common_utils.py | 5 ++- .../test_anthropic_chat_transformation.py | 13 ++++--- .../test_anthropic_messages_context_1m.py | 35 +++++++++++++++---- .../anthropic/test_anthropic_common_utils.py | 17 ++++----- 4 files changed, 48 insertions(+), 22 deletions(-) diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.py index d79ce6b9bec..e4fe67c00ed 100644 --- a/litellm/llms/anthropic/common_utils.py +++ b/litellm/llms/anthropic/common_utils.py @@ -1442,9 +1442,7 @@ class AnthropicModelInfo(BaseLLMModelInfo): wif_minted: bool = False, context_1m_requested: bool = False, ) -> dict: - betas: Final[set[str]] = set() - if context_1m_requested: - betas.add(ANTHROPIC_BETA_HEADER_VALUES.CONTEXT_1M_2025_08_07.value) + betas: Final = set() # Anthropic no longer requires the prompt-caching beta header # Prompt caching now works automatically when cache_control is used in messages # Reference: https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching @@ -1501,6 +1499,7 @@ class AnthropicModelInfo(BaseLLMModelInfo): all_betas: Final = betas.union( self.thinking_betas(thinking), + (ANTHROPIC_BETA_HEADER_VALUES.CONTEXT_1M_2025_08_07.value,) if context_1m_requested else (), (ANTHROPIC_MID_CONVERSATION_TOOL_CHANGES_BETA_HEADER,) if is_mid_conversation_tool_change_used else (), ) diff --git a/tests/unit/llms/anthropic/chat/test_anthropic_chat_transformation.py b/tests/unit/llms/anthropic/chat/test_anthropic_chat_transformation.py index 7b58ded6972..6d82791f4dd 100644 --- a/tests/unit/llms/anthropic/chat/test_anthropic_chat_transformation.py +++ b/tests/unit/llms/anthropic/chat/test_anthropic_chat_transformation.py @@ -10584,14 +10584,17 @@ def test_transform_request_treats_context_1m_model_as_its_base_model(optional_pa headers={}, ) - assert transform("claude-sonnet-4-6[1m]") == transform("claude-sonnet-4-6") + assert transform("claude-sonnet-5-5[1m]") == transform("claude-sonnet-5-5") @pytest.mark.parametrize( "model,expected_betas", [ - ("anthropic/claude-sonnet-4-6[1m]", {"context-1m-2025-08-07", "interleaved-thinking-2025-05-14"}), - ("anthropic/claude-sonnet-4-6", {"interleaved-thinking-2025-05-14"}), + ( + "anthropic/claude-sonnet-5-5[1m]", + {ANTHROPIC_BETA_HEADER_VALUES.CONTEXT_1M_2025_08_07.value, "interleaved-thinking-2025-05-14"}, + ), + ("anthropic/claude-sonnet-5-5", {"interleaved-thinking-2025-05-14"}), ], ) async def test_acompletion_context_1m_suffix_sends_base_model_with_beta( @@ -10609,7 +10612,7 @@ async def test_acompletion_context_1m_suffix_sends_base_model_with_beta( "id": "msg_context_1m", "type": "message", "role": "assistant", - "model": "claude-sonnet-4-6", + "model": "claude-sonnet-5-5", "content": [{"type": "text", "text": "hi"}], "stop_reason": "end_turn", "usage": {"input_tokens": 1, "output_tokens": 1}, @@ -10624,6 +10627,6 @@ async def test_acompletion_context_1m_suffix_sends_base_model_with_beta( ) request: Final = route.calls.last.request - assert json.loads(request.content)["model"] == "claude-sonnet-4-6" + assert json.loads(request.content)["model"] == "claude-sonnet-5-5" assert {beta.strip() for beta in request.headers["anthropic-beta"].split(",")} == expected_betas diff --git a/tests/unit/llms/anthropic/messages/native/test_anthropic_messages_context_1m.py b/tests/unit/llms/anthropic/messages/native/test_anthropic_messages_context_1m.py index 4525769a7cf..0b5a8bec354 100644 --- a/tests/unit/llms/anthropic/messages/native/test_anthropic_messages_context_1m.py +++ b/tests/unit/llms/anthropic/messages/native/test_anthropic_messages_context_1m.py @@ -6,13 +6,36 @@ import respx import litellm from litellm import Router +from litellm.llms.anthropic.pass_through.messages.transformation import AnthropicMessagesConfig +from litellm.types.llms.anthropic import ANTHROPIC_BETA_HEADER_VALUES + + +@pytest.mark.parametrize("model,expects_context_1m", [("claude-sonnet-5-5[1m]", True), ("claude-sonnet-5-5", False)]) +def test_sync_messages_environment_adds_context_1m_beta_only_for_suffixed_model( + model: str, expects_context_1m: bool +) -> None: + headers, _ = AnthropicMessagesConfig().validate_anthropic_messages_environment( + headers={"anthropic-beta": "interleaved-thinking-2025-05-14"}, + model=model, + messages=[{"role": "user", "content": "hi"}], + optional_params={}, + litellm_params={}, + api_key="test", + ) + + betas: Final = {beta.strip() for beta in headers["anthropic-beta"].split(",")} + assert (ANTHROPIC_BETA_HEADER_VALUES.CONTEXT_1M_2025_08_07.value in betas) is expects_context_1m + assert "interleaved-thinking-2025-05-14" in betas @pytest.mark.parametrize( "deployment_model,expected_betas", [ - ("anthropic/claude-sonnet-4-6[1m]", {"context-1m-2025-08-07", "interleaved-thinking-2025-05-14"}), - ("anthropic/claude-sonnet-4-6", {"interleaved-thinking-2025-05-14"}), + ( + "anthropic/claude-sonnet-5-5[1m]", + {ANTHROPIC_BETA_HEADER_VALUES.CONTEXT_1M_2025_08_07.value, "interleaved-thinking-2025-05-14"}, + ), + ("anthropic/claude-sonnet-5-5", {"interleaved-thinking-2025-05-14"}), ], ) async def test_router_messages_context_1m_deployment_sends_base_model_with_beta( @@ -30,7 +53,7 @@ async def test_router_messages_context_1m_deployment_sends_base_model_with_beta( "id": "msg_context_1m", "type": "message", "role": "assistant", - "model": "claude-sonnet-4-6", + "model": "claude-sonnet-5-5", "content": [{"type": "text", "text": "hi"}], "stop_reason": "end_turn", "usage": {"input_tokens": 1, "output_tokens": 1}, @@ -39,19 +62,19 @@ async def test_router_messages_context_1m_deployment_sends_base_model_with_beta( router: Final = Router( model_list=[ { - "model_name": "claude-sonnet-4-6[1m]", + "model_name": "claude-sonnet-5-5[1m]", "litellm_params": {"model": deployment_model, "api_key": "test"}, } ] ) await router.aanthropic_messages( - model="claude-sonnet-4-6[1m]", + model="claude-sonnet-5-5[1m]", messages=[{"role": "user", "content": "hi"}], max_tokens=16, extra_headers={"anthropic-beta": "interleaved-thinking-2025-05-14"}, ) request: Final = route.calls.last.request - assert json.loads(request.content)["model"] == "claude-sonnet-4-6" + assert json.loads(request.content)["model"] == "claude-sonnet-5-5" assert {beta.strip() for beta in request.headers["anthropic-beta"].split(",")} == expected_betas diff --git a/tests/unit/llms/anthropic/test_anthropic_common_utils.py b/tests/unit/llms/anthropic/test_anthropic_common_utils.py index 5c78e7f2bc0..0ac271db73d 100644 --- a/tests/unit/llms/anthropic/test_anthropic_common_utils.py +++ b/tests/unit/llms/anthropic/test_anthropic_common_utils.py @@ -4733,12 +4733,12 @@ def test_tools_without_strict_drops_only_the_strict_key_and_counts_the_strict_to @pytest.mark.parametrize( "model,base_model", [ - ("claude-sonnet-4-6[1m]", "claude-sonnet-4-6"), - ("claude-sonnet-4-6[1M]", "claude-sonnet-4-6"), - ("claude-sonnet-4-6", "claude-sonnet-4-6"), - ("claude-[1m]-sonnet-4-6", "claude-[1m]-sonnet-4-6"), - ("claude-sonnet-4-6[1m][1m]", "claude-sonnet-4-6[1m]"), - ("claude-sonnet-4-6[200k]", "claude-sonnet-4-6[200k]"), + ("claude-sonnet-5-5[1m]", "claude-sonnet-5-5"), + ("claude-sonnet-5-5[1M]", "claude-sonnet-5-5"), + ("claude-sonnet-5-5", "claude-sonnet-5-5"), + ("claude-[1m]-sonnet-5-5", "claude-[1m]-sonnet-5-5"), + ("claude-sonnet-5-5[1m][1m]", "claude-sonnet-5-5[1m]"), + ("claude-sonnet-5-5[200k]", "claude-sonnet-5-5[200k]"), ], ) def test_context_1m_suffix_is_only_a_trailing_marker(model: str, base_model: str) -> None: @@ -4748,11 +4748,12 @@ def test_context_1m_suffix_is_only_a_trailing_marker(model: str, base_model: str assert has_context_1m_suffix(model) is (model != base_model) -@pytest.mark.parametrize("model,expects_context_1m", [("claude-sonnet-4-6[1m]", True), ("claude-sonnet-4-6", False)]) +@pytest.mark.parametrize("model,expects_context_1m", [("claude-sonnet-5-5[1m]", True), ("claude-sonnet-5-5", False)]) def test_validate_environment_adds_context_1m_beta_only_for_suffixed_model( model: str, expects_context_1m: bool ) -> None: from litellm.llms.anthropic.common_utils import AnthropicModelInfo + from litellm.types.llms.anthropic import ANTHROPIC_BETA_HEADER_VALUES headers: Final = AnthropicModelInfo().validate_environment( headers={"anthropic-beta": "interleaved-thinking-2025-05-14"}, @@ -4764,6 +4765,6 @@ def test_validate_environment_adds_context_1m_beta_only_for_suffixed_model( ) betas: Final = {beta.strip() for beta in headers["anthropic-beta"].split(",")} - assert ("context-1m-2025-08-07" in betas) is expects_context_1m + assert (ANTHROPIC_BETA_HEADER_VALUES.CONTEXT_1M_2025_08_07.value in betas) is expects_context_1m assert "interleaved-thinking-2025-05-14" in betas