diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.py index 869a7c5fbc42..f341ac0cbfc1 100644 --- a/litellm/llms/anthropic/common_utils.py +++ b/litellm/llms/anthropic/common_utils.py @@ -14,6 +14,7 @@ from litellm.llms.base_llm.base_utils import BaseLLMModelInfo, BaseTokenCounter from litellm.llms.base_llm.chat.transformation import BaseLLMException from litellm.types.llms.anthropic import ( + ANTHROPIC_BETA_HEADER_VALUES, ANTHROPIC_HOSTED_TOOLS, ANTHROPIC_OAUTH_BETA_HEADER, ANTHROPIC_OAUTH_TOKEN_PREFIX, @@ -417,6 +418,29 @@ def get_anthropic_beta_list( if mcp_server_used: betas.append("mcp-client-2025-04-04") + if optional_params: + context_management = optional_params.get("context_management") + edits: list = [] + if isinstance(context_management, dict) and "edits" in context_management: + edits = context_management.get("edits", []) + elif isinstance(context_management, list): + edits = context_management + has_compact = any( + edit.get("type", "") in ("compact_20260112", "compaction") + for edit in edits + ) + has_other_edits = any( + edit.get("type", "") + and edit.get("type", "") not in ("compact_20260112", "compaction") + for edit in edits + ) + if has_compact: + betas.append(ANTHROPIC_BETA_HEADER_VALUES.COMPACT_2026_01_12.value) + elif has_other_edits: + betas.append( + ANTHROPIC_BETA_HEADER_VALUES.CONTEXT_MANAGEMENT_2025_06_27.value + ) + return list(set(betas)) def get_anthropic_headers( diff --git a/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py b/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py index 2f57ce5d1806..d3883b08a06c 100644 --- a/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py +++ b/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py @@ -1280,3 +1280,81 @@ def test_anthropic_messages_config_http_retry_helpers(self): config.transform_anthropic_messages_request_on_http_error(err, data) assert "thinking" not in data assert data["messages"] == [] + + +class TestGetAnthropicBetaListContextManagement: + """ + Regression tests for https://github.com/BerriAI/litellm/issues/27532 + get_anthropic_beta_list must include compact/context-management betas + when context_management is in optional_params, so Bedrock InvokeModel + receives the required anthropic_beta field. + """ + + def test_compact_edit_adds_compact_beta(self): + from litellm.llms.anthropic.common_utils import AnthropicModelInfo + + info = AnthropicModelInfo() + betas = info.get_anthropic_beta_list( + model="claude-sonnet-4-6", + optional_params={ + "context_management": { + "edits": [{"type": "compact_20260112"}], + } + }, + ) + assert "compact-2026-01-12" in betas + + def test_non_compact_edit_adds_context_management_beta(self): + from litellm.llms.anthropic.common_utils import AnthropicModelInfo + + info = AnthropicModelInfo() + betas = info.get_anthropic_beta_list( + model="claude-sonnet-4-6", + optional_params={ + "context_management": { + "edits": [{"type": "summarize"}], + } + }, + ) + assert "context-management-2025-06-27" in betas + + def test_openai_list_format_adds_compact_beta(self): + from litellm.llms.anthropic.common_utils import AnthropicModelInfo + + info = AnthropicModelInfo() + betas = info.get_anthropic_beta_list( + model="claude-sonnet-4-6", + optional_params={ + "context_management": [{"type": "compaction"}], + }, + ) + assert "compact-2026-01-12" in betas + + def test_no_context_management_no_extra_betas(self): + from litellm.llms.anthropic.common_utils import AnthropicModelInfo + + info = AnthropicModelInfo() + betas = info.get_anthropic_beta_list( + model="claude-sonnet-4-6", + optional_params={"max_tokens": 100}, + ) + assert "compact-2026-01-12" not in betas + assert "context-management-2025-06-27" not in betas + + def test_mixed_edits_uses_compact_beta(self): + from litellm.llms.anthropic.common_utils import AnthropicModelInfo + + info = AnthropicModelInfo() + betas = info.get_anthropic_beta_list( + model="claude-sonnet-4-6", + optional_params={ + "context_management": { + "edits": [ + {"type": "summarize"}, + {"type": "compact_20260112"}, + ], + } + }, + ) + assert "compact-2026-01-12" in betas + assert "context-management-2025-06-27" not in betas