Skip to content

fix: align API retry logic with Claude Code (Retry-After header, jitter, 10 retries)#1327

Merged
lewis617 merged 1 commit into
mainfrom
worktree-just-low-fox
Jul 2, 2026
Merged

fix: align API retry logic with Claude Code (Retry-After header, jitter, 10 retries)#1327
lewis617 merged 1 commit into
mainfrom
worktree-just-low-fox

Conversation

@lewis617

@lewis617 lewis617 commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

Summary

Align API retry logic in openaiClient.ts with Claude Code's withRetry.ts to avoid ineffective retries during rate limiting.

Changes

  • Honor Retry-After response header: when the server returns 429 with a Retry-After header, wait the specified duration instead of using exponential backoff. This was the root cause of the first 3 retries being wasted — the server hadn't lifted the rate limit yet.
  • Increase max retries: 5 → 10
  • Faster initial retry with jitter: base delay 2000ms → 500ms, with 25% random jitter to avoid thundering herd
  • Max delay cap: 32s (prevents unbounded waits)
  • Log improvement: include retryAfter value in warning logs

Spec

Updated specs/012-ai-error-handling.md:

  • User story 3 expanded to 3 acceptance scenarios (Retry-After priority, exponential backoff+jitter, 5xx shared strategy)
  • FR-005 rewritten with full retry parameters
  • FR-010 simplified to reference FR-005

Test

  • Updated exponential backoff test for new delay sequence (500→1000→2000→...→32000)
  • Updated max retries test (5→10, 6→11 total calls)
  • Added Retry-After header test: verifies 30s header is honored and no retry happens before that
  • All 15 tests pass, type-check and lint pass

Before vs After

Before After
Retry-After header Ignored Honored (bypasses backoff)
Max retries 5 10
Base delay 2000ms 500ms
Jitter None 25%
Max delay None 32s

…er, 10 retries)

- Honor Retry-After response header instead of fixed exponential backoff
- Increase max retries from 5 to 10
- Change base delay from 2000ms to 500ms with 25% jitter
- Add max delay cap of 32s
- Update spec 012-ai-error-handling.md (user story 3, FR-005, FR-010)
- Add test for Retry-After header behavior
@lewis617 lewis617 merged commit de967cf into main Jul 2, 2026
1 check passed
@lewis617 lewis617 deleted the worktree-just-low-fox branch July 2, 2026 10:41
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