Skip to content

fix(models): stop retrying context window errors - #922

Open
swayamg20 wants to merge 1 commit into
SWE-agent:mainfrom
swayamg20:fix/issue-921-context-overflow-retries
Open

fix(models): stop retrying context window errors#922
swayamg20 wants to merge 1 commit into
SWE-agent:mainfrom
swayamg20:fix/issue-921-context-overflow-retries

Conversation

@swayamg20

Copy link
Copy Markdown

Summary

  • Classify only HTTP 400 context-window overflows using canonical provider error fields and a conservative message fallback.
  • Raise provider-specific API-error subclasses across the OpenRouter tool-call, text-based, and Responses adapters, plus Requesty, so permanent failures abort immediately.
  • Preserve existing retries for unrelated 400 responses, transient 5xx responses, and network errors.

Closes #921

Testing

  • Real local HTTP-server tests verify context overflows make one request while other API errors retain the configured retry count.
  • pytest -q tests/models/test_context_window_errors.py tests/models/test_openrouter_textbased_model.py (16 passed)
  • pytest -q -n auto (552 passed, 76 skipped)
  • Ruff check and format validation on all changed files
  • All applicable Python pre-commit hooks on all changed files

@EvolveAegis

EvolveAegis commented Aug 2, 2026

Copy link
Copy Markdown

Independent corroboration from a black-box PoC (no source patching): against the shipped default stop_after_attempt(10), a context_length_exceeded HTTP 400 from the provider goes through up to 10 total attempts (1 initial plus 9 retries) before giving up, while a 401 correctly aborts at attempt 1. We also verified the bug is provider-symmetric: OpenRouter and Requesty share the same retry.py, so both exhibit the same retry waste. This PR's is_context_window_error() plus a dedicated ContextWindowError in abort_exceptions moves the 400 from "retry to budget" to "abort at attempt 1", and the added test proving generic 400/500/token_limit_exceeded still retry confirms it doesn't over-suppress. One scope note: only context_length_exceeded-shaped 400s abort; other permanent 4xx still retry to the budget, worth a follow-up.

@swayamg20

Copy link
Copy Markdown
Author

Thanks for independently validating this. Agreed that broader permanent-4xx handling should remain a follow-up, this PR intentionally scopes the non-retry behavior to recognized context-window errors.

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.

Permanent context-overflow (400) retried 10 times — full oversized history resent each attempt

2 participants