From 704db1d66fe6281e008e34a8f6d3809ddba36713 Mon Sep 17 00:00:00 2001 From: Vidit Ostwal <110953813+Vidit-Ostwal@users.noreply.github.com> Date: Thu, 27 Aug 2026 02:39:35 +0530 Subject: [PATCH] fix(llms): map default Claude Sonnet 4.6 to its 1M context window (#7125) * fix(llms): map default Claude Sonnet 4.6 to its 1M context window Native AnthropicCompletion fell back to 200k for claude-sonnet-4-6, so the default instance understated the documented limit. * fix(llms): align Anthropic context windows with active Claude models Retired Claude 3/2/Instant IDs no longer have dedicated entries; the map now covers the current Claude API lineup and their documented 1M vs 200k windows. * fix(llms): map Claude Mythos 5 to its documented 1M context window Native AnthropicCompletion fell back to 200k for claude-mythos-5 even though Anthropic lists a 1M-token window. --- .../llms/providers/anthropic/completion.py | 21 +++++---- .../tests/llms/anthropic/test_anthropic.py | 45 +++++++++++++++++-- 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/lib/crewai/src/crewai/llms/providers/anthropic/completion.py b/lib/crewai/src/crewai/llms/providers/anthropic/completion.py index 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.py index 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 types from unittest.mock import AsyncMock, patch, MagicMock import pytest -from crewai.llm import LLM +from crewai.llm import CONTEXT_WINDOW_USAGE_RATIO, LLM from crewai.crew import Crew from crewai.agent import Agent from 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) + + +@pytest.mark.parametrize( + "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) + + +@pytest.mark.parametrize( + "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():