Skip to content

fix: add 429 rate limit retry with exponential backoff#832

Open
xodn348 wants to merge 5 commits intoanthropics:mainfrom
xodn348:fix/812-429-rate-limit-retry
Open

fix: add 429 rate limit retry with exponential backoff#832
xodn348 wants to merge 5 commits intoanthropics:mainfrom
xodn348:fix/812-429-rate-limit-retry

Conversation

@xodn348
Copy link
Copy Markdown

@xodn348 xodn348 commented Apr 16, 2026

Summary

Closes #812

This PR adds automatic retry logic for 429 rate limit errors in the Claude Agent SDK Python client.

Changes

  • ****: New RateLimitError exception class with retry_after and original_error fields
  • ****: Export RateLimitError from public API
  • ****: Add rate_limit_max_retries field to ClaudeAgentOptions (default: 3)
  • ****: Retry loop with exponential backoff in process_query()
  • ****: 14 tests covering all retry scenarios

Retry Behavior

  • Detects 429 errors by inspecting error messages and stderr for rate_limit_error or 429
  • Parses Retry-After / retryAfter header to respect server-specified wait time
  • Falls back to exponential backoff: min(2.0 * 2^(attempt-1), 60.0) + jitter
  • Respects rate_limit_max_retries option (default 3); raises RateLimitError after exhaustion
  • Non-rate-limit errors propagate immediately without retry

Testing

pytest tests/test_rate_limit_retry.py -v
# 14 passed in 0.27s

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.

Agent SDK should handle 429 rate limits gracefully instead of crashing

1 participant