Skip to content

Add retry with exponential backoff for sync and async clients - #23

Merged
mehmetkr merged 2 commits into
mainfrom
retry-with-backoff
Dec 1, 2026
Merged

mehmetkr merged 2 commits into
mainfrom
retry-with-backoff

Conversation

@mehmetkr

@mehmetkr mehmetkr commented Dec 1, 2026

Copy link
Copy Markdown
Owner

Summary

  • Add automatic retry with exponential backoff to both BaseClient and AsyncBaseClient, matching the anthropic-python/openai-python SDK pattern
  • Retry on status 408, 429, and 5xx (excluding 409, which Trigger.dev uses for optimistic locking)
  • Formula: min(0.5 * 2^attempt, 8.0) * (1 - 0.25 * random()) with DEFAULT_MAX_RETRIES=2
  • Respect retry-after and retry-after-ms headers (ms preferred), capped at 60s (exceeding cap raises immediately)
  • Move retry_after property from RateLimitError to APIStatusError base class so all retryable status errors can expose it

Test plan

  • 38 new tests in tests/test_retry.py covering:
    • Unit tests for _should_retry_status (retryable and non-retryable codes)
    • Unit tests for _calculate_retry_delay (base delay, exponential growth, max cap, jitter, retry-after override, retry-after cap)
    • Unit tests for retry_after property on APIStatusError (inheritance, retry-after-ms preference, non-numeric ignored)
    • Sync integration: retries on 500/429/connection error/timeout, no retry on 400/401/404/409, exhaust retries, max_retries=0, retry-after respected, retry-after cap
    • Async integration: full parity with sync tests
  • 193 total tests passing, zero regressions
  • CI green: ruff, ruff format, pyright strict, mypy strict, pytest

@mehmetkr
mehmetkr merged commit 8f4518c into main Dec 1, 2026
7 checks passed
@mehmetkr
mehmetkr deleted the retry-with-backoff branch December 1, 2026 13:18
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.

1 participant