feat(adapters): auto-retry with exponential backoff [closes #118]#292
Merged
EmersonBraun merged 1 commit intomainfrom Apr 15, 2026
Merged
feat(adapters): auto-retry with exponential backoff [closes #118]#292EmersonBraun merged 1 commit intomainfrom
EmersonBraun merged 1 commit intomainfrom
Conversation
Phase 1, story #118. Every adapter now retries transient failures by default — no caller code needed. Retry policy (default 3 attempts, configurable per-adapter): Retried (transient): HTTP 408 request timeout HTTP 429 rate limit HTTP 500 / 502 / 503 / 504 server errors Network errors (fetch throws TypeError, ECONNRESET, etc.) NOT retried (terminal): HTTP 4xx other than 408/429 — bad request or auth issue, retrying just repeats the failure AbortError — caller cancelled Backoff: exponential (base 500ms × 2^attempt, capped at 8s) with full jitter by default. Respects Retry-After response header when present (both seconds-format and HTTP-date). Mid-stream: once the response body starts streaming, retries stop. Partial output is already on the wire — restarting would duplicate it. Configurable per-adapter: openai({ apiKey, model, retry: { maxAttempts: 5, baseDelayMs: 1000, maxDelayMs: 30_000, jitter: true, onRetry: ({ attempt, delayMs, reason }) => console.log('retry', attempt, 'in', delayMs, 'ms —', reason), }, }) Opt out: retry: { maxAttempts: 1 }. Custom retryOn predicate also supported. Architecture: - New fetchWithRetry primitive in utils.ts — pure, testable, injectable sleep for unit tests - createStreamSource takes optional retry param (back-compat — undefined = use defaults) - Every adapter's config gets an optional retry?: RetryOptions field - Threaded into openai, anthropic, gemini, ollama, vercel-ai Tests: - 13 new cases covering: success path, terminal 4xx, 429 retry, 5xx retry chain, Retry-After (seconds + HTTP date), exponential backoff math, exhaustion behavior, abort propagation, network error retry, mid-attempt abort, onRetry hook, custom retryOn - Total: 63 / 63 passing on @agentskit/adapters (was 50) Refs #118 #211
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Phase 1 #118. Every adapter now retries transient failures by default — no caller code needed.
Retry policy
Retry-AfterheaderDefaults: 3 attempts, base 500ms × 2^n with full jitter, max 8s per delay.
Configuration
Opt out entirely:
retry: { maxAttempts: 1 }.Custom decision logic:
retry: { retryOn: ({ response, error }) => ... }.Standalone primitive
Wired into
openai,anthropic,gemini,ollama,vercelAIretry?: RetryOptionsfield on their configTests
13 new cases on
@agentskit/adapters(was 50, now 63 passing):Test plan
fetchWithRetryexportedRefs #118 #211