ai-retry has 45,039 recorded weekly downloads on npm, a decrease of 46.4% over the past 30 days, ranking #812 of 3,247 tracked listings.
About ai-retry
ai-retry handles API failures, content filters, timeouts and other errors. It retries the request, or switches to a different model or provider.
You declare a list of typed conditions, for example httpStatus(529), finishReason('content-filter') or timeout(). Each condition ends with an action. .retry() tries the same model again, and .switch() tries a fallback model. When a request fails, or succeeds with a bad result, ai-retry checks the list from top to bottom and uses the first match. It counts attempts per model, so a chain cannot loop.
ai-retry covers two kinds of failure:
- Error-based: The model throws, for example on a timeout, a rate limit or an API error.
- Result-based: The response is successful but not usable, for example on a content filter, a schema mismatch or empty output.
Features
- Two layers: Retry the model request, or run the whole AI SDK call again.
- Typed conditions: Match errors and results with
error(),httpStatus(),finishReason(),timeout(),result()and others. - Retry or switch: Try the same model again, or switch to a different model.
- Fallbacks: Give a list of models or providers to try in sequence.
- Timeouts: Recover a request or a stream that times out.
- Streaming: Recover a stream that fails before the first content part arrives.
- Telemetry: Get an OpenTelemetry span for each request and each attempt.
Two layers
ai-retry has the same retry system at two points of the AI SDK call chain. The point decides which failures a retry can recover.
Retryable model
createRetryableModel wraps a model instance. The retry runs inside the doGenerate, doStream and doEmbed methods of the model. You create it once and give it to any function that takes a model parameter, for example generateText, streamText or embed.
sequenceDiagram
participant You as your code
create participant RM as createRetryableModel()
participant SDK as generateText()
You->>RM: createRetryableModel({ model: gpt-5,retries: [httpStatus(529).switch(opus-5)] })
participant M as openai('gpt-5')
participant F as anthropic('opus-5')
You->>SDK: generateText({ model: retryableModel, prompt })
activate SDK
SDK->>RM: doGenerate(options)
activate RM
Note over M: attempt 1
RM->>M: doGenerate(options)
M-->>RM: ✗ APICallError 529
Note over RM: httpStatus(529) matches<br/>switch to fallback model
Note over F: attempt 2
RM->>F: doGenerate(options)
F-->>RM: ✓ result
RM-->>SDK: ✓ result
deactivate RM
SDK-->>You: ✓ result
deactivate SDK
The retryable model is below the call (generateText, streamText, …). It sees provider errors and raw results, but it cannot see the arguments of the call, such as a timeout argument or an inbound abortSignal. When one of those fires, the AI SDK stops the call and discards the result of any retry below it.
Retryable call
retryableGenerateText and the other retryable functions wrap the function call itself. Each attempt is a new call to generateText, streamText or another function, with its own abort signal. Thus a retry can also recover a timeout.
sequenceDiagram
participant You as your code
participant RC as retryableGenerateText()
participant SDK as generateText()
participant M as openai('gpt-5')
participant F as openai('gpt-5-mini')
You->>RC: retryableGenerateText({ model, prompt, timeout: { totalMs }, retry })
activate RC
RC->>SDK: generateText({ model: gpt-5, … })
activate SDK
Note over RC,SDK: attempt 1
SDK->>M: doGenerate(options)
Note over M: model stalls...
Note over SDK: timeout fires <br/>call is aborted
SDK-->>RC: ✗ TimeoutError
deactivate SDK
Note over RC: timeout() matches<br/>re-run the full call
RC->>SDK: generateText({ model: gpt-5-mini, … })
activate SDK
Note over RC,SDK: attempt 2
SDK->>F: doGenerate(options)
F-->>SDK: ✓ result
SDK-->>RC: ✓ result
deactivate SDK
RC-->>You: ✓ result
deactivate RC
Recovery matrix…
Description from the npm listing.
- ai
- ai-sdk
- fallback
- retry
README badge
Show your rank in a README or on your website. The badge updates automatically.
Charts
Images: 30d · 1y · all time · 1600×900 dark
Adoption over time
7/30/90-day growth
Ranking history
Listing placement
Listing placement
Filled points are observed values; hollow points come from Internet Archive snapshots of the listing page. Dashed segments span periods with no observations — values between points are not interpolated.
Each band is one page of 20. Weekly downloads on a linear scale. Placement is projected from weekly downloads, so it can differ from the marketplace's own sort order. Hover a dot to see the listing.
Recent observations
Full history (592)Similar npm AI SDKs plugins
All 20 ai-retry alternativesData source
Weekly downloads (trailing 7 complete days) come from the official npm downloads API. Downloads include CI and mirrors and are not equivalent to installs or users. npm publishes no ratings.
Listing: https://www.npmjs.com/package/ai-retry. First seen Oct 16, 2026; last successful collection Dec 6, 2026. See the methodology.