Skip to content

fix(context): detect Anthropic 'prompt is too long' as context length error#821

Closed
Bartok9 wants to merge 1 commit into
NousResearch:mainfrom
Bartok9:fix/813-anthropic-context-length-detection
Closed

fix(context): detect Anthropic 'prompt is too long' as context length error#821
Bartok9 wants to merge 1 commit into
NousResearch:mainfrom
Bartok9:fix/813-anthropic-context-length-detection

Conversation

@Bartok9

@Bartok9 Bartok9 commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #813 — Anthropic's native API returns context overflow errors in a unique format that wasn't being detected:

prompt is too long: 233153 tokens > 200000 maximum

This format wasn't matched by the existing is_context_length_error phrases, causing the agent to abort immediately as a "non-retryable client error" instead of triggering context compression.

Changes

  1. run_agent.py: Added 'prompt is too long' to the context length error detection phrases
  2. tests/test_413_compression.py: Added test case for Anthropic's error format

Testing

  • All 10 tests in test_413_compression.py pass
  • New test specifically verifies Anthropic format triggers compression instead of abort

Impact

Users on Anthropic models (direct API or via OpenRouter) will now get proper context compression when hitting the 200k token limit, instead of an immediate failure.

… error (NousResearch#813)

Anthropic's native API returns context overflow errors in a unique format:
"prompt is too long: 233153 tokens > 200000 maximum"

This wasn't matched by the existing is_context_length_error phrases, causing
the agent to abort immediately instead of triggering context compression.

Added 'prompt is too long' to the detection list so these errors correctly
trigger compression and retry like other context length errors.

Closes NousResearch#813
@Bartok9

Bartok9 commented Mar 10, 2026

Copy link
Copy Markdown
Contributor Author

Note: The CI failure is unrelated to this PR. The test test_vision_tools.py::TestErrorLoggingExcInfo::test_analysis_error_logs_exc_info is also failing on main branch. Filed #835 to track.

@teknium1

Copy link
Copy Markdown
Contributor

Fixed in commit a54405e with a more comprehensive approach:

  1. Root cause fix: After tool execution, the compression check now estimates the next prompt size using last_prompt_tokens + last_completion_tokens + tool_result_chars // 3 — real API token counts as the base, with only a small estimate for the newly appended tool results. This catches the scenario where tool results (file reads, web extractions) push context past the limit before the next API call.

  2. Your safety net: Also added 'prompt is too long' to the error detection phrases, so if the estimate is still off, the error triggers compression instead of aborting.

Thanks for the clear issue report and clean PR @Bartok9!

@teknium1 teknium1 closed this Mar 11, 2026
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.

Anthropic "prompt is too long" 400 error not detected as context length error — aborts instead of compressing

2 participants