Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/strands/models/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class AnthropicModel(Model):
}

OVERFLOW_MESSAGES = {
"prompt is too long:",
"input is too long",
"input length exceeds context window",
"input and output tokens exceed your context limit",
Expand Down
30 changes: 30 additions & 0 deletions tests_integ/models/test_model_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

import strands
from strands import Agent
from strands.agent import NullConversationManager
from strands.models.anthropic import AnthropicModel
from strands.types.content import ContentBlock, Message
from strands.types.exceptions import ContextWindowOverflowException

"""
These tests only run if we have the anthropic api key
Expand Down Expand Up @@ -152,3 +155,30 @@ def test_structured_output_multi_modal_input(agent, yellow_img, yellow_color):
tru_color = agent.structured_output(type(yellow_color), content)
exp_color = yellow_color
assert tru_color == exp_color


@pytest.mark.asyncio
def test_input_and_max_tokens_exceed_context_limit():
"""Test that triggers 'input length and max_tokens exceed context limit' error."""

# Note that this test is written specifically in a style that allows us to swap out conversation_manager and
# verify behavior

model = AnthropicModel(
model_id="claude-sonnet-4-20250514",
max_tokens=64000,
)

large_message = "This is a very long text. " * 10000

messages = [
Message(role="user", content=[ContentBlock(text=large_message)]),
Message(role="assistant", content=[ContentBlock(text=large_message)]),
Message(role="user", content=[ContentBlock(text=large_message)]),
]

# NullConversationManager will propagate ContextWindowOverflowException directly instead of handling it
agent = Agent(model=model, conversation_manager=NullConversationManager())

with pytest.raises(ContextWindowOverflowException):
agent(messages)
Loading