feat: add retry with exponential backoff for LLM API calls #40

Merged
pook merged 1 commit from feature/llm-retry-mechanism into main 2026-04-09 06:13:49 -04:00
Owner

Summary

  • Adds a reusable withRetry() utility with exponential backoff (1s, 2s, 4s, max 3 retries)
  • Wraps the OpenAI chat.completions.create call in llm.ts with the retry mechanism
  • Retries only on transient errors: HTTP 429, 500, 502, 503, 504, and network timeouts (ECONNREFUSED, ECONNRESET, ETIMEDOUT)
  • Does NOT retry client errors (400, 401, 403) — these fail immediately
  • Logs each retry attempt with attempt number and error reason

Files changed

  • packages/api/src/services/retry.ts — new retry utility module
  • packages/api/src/services/llm.ts — wraps OpenAI call with withRetry()
  • packages/api/tests/unit/retry.test.ts — 23 unit tests covering all retry scenarios

Test plan

  • 23 unit tests pass (bun test packages/api/tests/unit/retry.test.ts)
  • Tests cover: retryable status codes, non-retryable client errors, network errors, retry exhaustion, exponential backoff timing, logging

🤖 Generated with Claude Code

Closes #37

## Summary - Adds a reusable `withRetry()` utility with exponential backoff (1s, 2s, 4s, max 3 retries) - Wraps the OpenAI `chat.completions.create` call in `llm.ts` with the retry mechanism - Retries only on transient errors: HTTP 429, 500, 502, 503, 504, and network timeouts (ECONNREFUSED, ECONNRESET, ETIMEDOUT) - Does NOT retry client errors (400, 401, 403) — these fail immediately - Logs each retry attempt with attempt number and error reason ## Files changed - `packages/api/src/services/retry.ts` — new retry utility module - `packages/api/src/services/llm.ts` — wraps OpenAI call with `withRetry()` - `packages/api/tests/unit/retry.test.ts` — 23 unit tests covering all retry scenarios ## Test plan - [x] 23 unit tests pass (`bun test packages/api/tests/unit/retry.test.ts`) - [x] Tests cover: retryable status codes, non-retryable client errors, network errors, retry exhaustion, exponential backoff timing, logging 🤖 Generated with [Claude Code](https://claude.com/claude-code) Closes #37
Wraps OpenAI API calls in a retry mechanism that handles transient
failures (429, 500, 502, 503, 504, network timeouts) with exponential
backoff (1s, 2s, 4s, max 3 retries). Client errors (400/401/403) are
not retried. Each retry attempt is logged with attempt number and reason.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Author
Owner

Review notes (agent-bot):

  • Issue link: Added Closes #37 to the PR description. ✓
  • Retry logic looks well-scoped: correct transient error codes, no retry on 4xx client errors.
  • Consider adding jitter to the exponential backoff to avoid thundering herd on recovery (multiple callers retrying at the same interval).
  • 23 unit tests is good coverage for the retry utility.
**Review notes (agent-bot):** - Issue link: Added `Closes #37` to the PR description. ✓ - Retry logic looks well-scoped: correct transient error codes, no retry on 4xx client errors. - Consider adding jitter to the exponential backoff to avoid thundering herd on recovery (multiple callers retrying at the same interval). - 23 unit tests is good coverage for the retry utility.
pook merged commit 86614a4d83 into main 2026-04-09 06:13:49 -04:00
Sign in to join this conversation.
No reviewers
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!40
No description provided.