Skip to content

Conversation

@yoonian
Copy link

@yoonian yoonian commented Oct 23, 2025

Description

Added handling for "input length and max_tokens exceed context limit" to raise ContextWindowOverflowException

Related Issues

Documentation PR

Previously, a generic error was raised instead of ContextWindowOverflowException.
Now it correctly maps the "input length and max_tokens exceed context limit" error.

Handle Anthropic/LLM API errors containing "input length and max_tokens exceed context limit" by explicitly raising ContextWindowOverflowException instead of generic RemoteProtocolError(for strands-agent 0.12.0) or EventLoopException or anthropic.BadRequestError(for anthropic 0.71.0, strands-agent 0.13.0)

Type of Change

Bug fix

Testing

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Handle Anthropic/LLM API errors containing "input length and `max_tokens` exceed context limit" by explicitly raising ContextWindowOverflowException instead of generic RemoteProtocolError(for strands-agent 0.12.0) or EventLoopException or anthropic.BadRequestError(for anthropic 0.71.0, strands-agent 0.13.0)
@yoonian yoonian changed the title fix: raise ContextWindowOverflowException when context limit exceeded fix: incorrect exception raised when Claude context limit exceeded Oct 23, 2025
@yoonian yoonian changed the title fix: incorrect exception raised when Claude context limit exceeded fix(anthropic): incorrect exception raised when context limit exceeded Oct 23, 2025
Copy link
Member

@dbschmigelski dbschmigelski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under what conditions where you seeing this?

When I ran a test using

def test_input_and_max_tokens_exceed_context_limit():
    """Test that triggers 'input length and max_tokens exceed context limit' error."""
    # Create a model with very high max_tokens
    model = AnthropicModel(
        model_id="claude-3-7-sonnet-20250219",
        max_tokens=200000,  # Very high max_tokens
    )
    
    # Create very large input that when combined with max_tokens exceeds context window
    large_text = "This is a very long text. " * 50000  # ~150k tokens
    
    agent = Agent(model=model)
    
    with pytest.raises(ContextWindowOverflowException):
        agent(large_text)

I ended up seeing

{
    'type': 'error',
    'error': {
        'type': 'invalid_request_error',
        'message': 'max_tokens: 200000 > 64000, which is the maximum allowed number of output tokens for claude-3-7-sonnet-20250219'
    },
    'request_id': 'req_XXXXXXXX'
}

Which we will have to see if we should handle this or not but I want to reproduce the message you are proposing.

@yoonian
Copy link
Author

yoonian commented Nov 4, 2025

I am using the model 'claude-sonnet-4-20250514'

@zastrowm
Copy link
Member

zastrowm commented Nov 4, 2025

I also was not able to reproduce the:

input length and max_tokens exceed context limit

case. However, in attempting to reproduce I did get a "prompt is too long" error, which I created #1137 to address.

@yoonian do you have code that reproduces the error that you saw?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants