Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
fix(llms): map default Claude Sonnet 4.6 to its 1M context window (#7125)
Vidit Ostwal704db1d66fe6
diff --git a/lib/crewai/src/crewai/llms/providers/anthropic/completion.py b/lib/crewai/src/crewai/llms/providers/anthropic/completion.pyindex e2afec133..e6bc4255f 100644--- a/lib/crewai/src/crewai/llms/providers/anthropic/completion.py+++ b/lib/crewai/src/crewai/llms/providers/anthropic/completion.py@@ -1953,16 +1953,19 @@ class AnthropicCompletion(BaseLLM):"""Get the context window size for the model."""from crewai.llm import CONTEXT_WINDOW_USAGE_RATIO+ # Current offered models. Unknown and retired IDs fall back to 200k.context_windows = {- "claude-3-5-sonnet": 200000,- "claude-3-5-haiku": 200000,- "claude-3-opus": 200000,- "claude-3-sonnet": 200000,- "claude-3-haiku": 200000,- "claude-3-7-sonnet": 200000,- "claude-2.1": 200000,- "claude-2": 100000,- "claude-instant": 100000,+ "claude-fable-5": 1000000,+ "claude-mythos-5": 1000000,+ "claude-opus-5": 1000000,+ "claude-sonnet-5": 1000000,+ "claude-opus-4-8": 1000000,+ "claude-opus-4-7": 1000000,+ "claude-opus-4-6": 1000000,+ "claude-sonnet-4-6": 1000000,+ "claude-opus-4-5": 200000,+ "claude-sonnet-4-5": 200000,+ "claude-haiku-4-5": 200000,}for model_prefix, size in context_windows.items():diff --git a/lib/crewai/tests/llms/anthropic/test_anthropic.py b/lib/crewai/tests/llms/anthropic/test_anthropic.pyindex c26475010..f177a2bc4 100644--- a/lib/crewai/tests/llms/anthropic/test_anthropic.py+++ b/lib/crewai/tests/llms/anthropic/test_anthropic.py@@ -4,7 +4,7 @@ import typesfrom unittest.mock import AsyncMock, patch, MagicMockimport pytest-from crewai.llm import LLM+from crewai.llm import CONTEXT_WINDOW_USAGE_RATIO, LLMfrom crewai.crew import Crewfrom crewai.agent import Agentfrom crewai.task import Task@@ -116,6 +116,44 @@ def test_anthropic_completion_defaults_to_sonnet_4_6():assert llm.max_tokens == 128000+def test_default_anthropic_completion_uses_sonnet_4_6_context_window():+ from crewai.llms.providers.anthropic.completion import AnthropicCompletion++ llm = AnthropicCompletion()+ assert llm.get_context_window_size() == int(1_000_000 * CONTEXT_WINDOW_USAGE_RATIO)+++ "model",+ [+ "claude-fable-5",+ "claude-mythos-5",+ "claude-opus-5",+ "claude-sonnet-5",+ "claude-opus-4-8",+ "claude-opus-4-7",+ "claude-opus-4-6",+ "claude-sonnet-4-6",+ ],+)+def test_current_1m_anthropic_models_use_1m_context_window(model: str) -> None:+ from crewai.llms.providers.anthropic.completion import AnthropicCompletion++ llm = AnthropicCompletion(model=model)+ assert llm.get_context_window_size() == int(1_000_000 * CONTEXT_WINDOW_USAGE_RATIO)+++ "model",+ ["claude-opus-4-5", "claude-sonnet-4-5", "claude-haiku-4-5"],+)+def test_claude_4_5_family_uses_200k_context_window(model: str) -> None:+ from crewai.llms.providers.anthropic.completion import AnthropicCompletion++ llm = AnthropicCompletion(model=model)+ assert llm.get_context_window_size() == int(200000 * CONTEXT_WINDOW_USAGE_RATIO)++def test_anthropic_defaults_max_tokens_to_model_limit():"""Native Anthropic used to default to 4096, which truncates large tool calls."""llm = LLM(model="anthropic/claude-unknown-future")@@ -500,11 +538,10 @@ def test_anthropic_context_window_size():"""Test that Anthropic models return correct context window sizes"""- llm = LLM(model="anthropic/claude-3-5-sonnet-20241022")+ llm = LLM(model="anthropic/claude-haiku-4-5")context_size = llm.get_context_window_size()- assert context_size > 100000- assert context_size <= 200000 # But not exceed the actual limit+ assert context_size == int(200000 * CONTEXT_WINDOW_USAGE_RATIO)def test_anthropic_message_formatting():