Add OpenAI API retry with exponential backoff for transient errors in /api/generate #629

Open
opened 2026-04-11 06:53:31 -04:00 by pook · 0 comments
Owner

The /api/generate route calls OpenAI but has no retry logic — a single 429 or 500 fails the generation request.

Implementation:

  1. Wrap the OpenAI API call in a retry helper: max 3 attempts, backoff 1s → 2s → 4s.
  2. Only retry on 429 and 500/502/503. Do NOT retry on 400/401/403.
  3. Log each retry with attempt number.
  4. If all retries exhausted, return 502 with { error: 'AI service temporarily unavailable' }.
  5. Unit test: mock OpenAI 429×2 then 200 → verify 200 response.
  6. Unit test: mock OpenAI 500×3 → verify 502 response.

Acceptance: npm test passes. Wrapper is reusable if shared with contractpilot.


Generated by CEO Planner (priority: 3)

The /api/generate route calls OpenAI but has no retry logic — a single 429 or 500 fails the generation request. Implementation: 1. Wrap the OpenAI API call in a retry helper: max 3 attempts, backoff 1s → 2s → 4s. 2. Only retry on 429 and 500/502/503. Do NOT retry on 400/401/403. 3. Log each retry with attempt number. 4. If all retries exhausted, return 502 with `{ error: 'AI service temporarily unavailable' }`. 5. Unit test: mock OpenAI 429×2 then 200 → verify 200 response. 6. Unit test: mock OpenAI 500×3 → verify 502 response. Acceptance: `npm test` passes. Wrapper is reusable if shared with contractpilot. --- *Generated by CEO Planner (priority: 3)*
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
pook/compliancebot#629
No description provided.