ai-retry has 41,306 recorded weekly downloads on npm, a decrease of 20.7% over the past 30 days, ranking #628 of 2,322 tracked listings.
About ai-retry
Automatically handle API failures, content filtering, timeouts and other errors by switching between different AI models and providers.
ai-retry wraps a base model with a list of typed retry conditions. When a request fails with an error, or the response is not satisfying, it walks the conditions top-down to find a suitable fallback. It tracks which models have been tried and how many attempts have been made to prevent infinite loops.
Two retry shapes are supported:
- Error-based: the model throws (timeouts, rate limits, API errors).
- Result-based: the model returns a successful response that still needs retrying (content filtering, schema mismatch, etc.).
Installation
[!NOTE] Version compatibility:
- Use
ai-retry@0.xfor AI SDK v5- Use
ai-retry@1.xfor AI SDK v6- Use
ai-retry@2.xfor AI SDK v7
npm install ai-retry@1 # for AI SDK v6
npm install ai-retry@2 # for AI SDK v7
Usage
[!NOTE] The condition API is the recommended way to configure retries. Existing code keeps working:
- The root
createRetryableexport and the function-style retryables (contentFilterTriggered,requestTimeout, …) are deprecated but still functional. PrefercreateRetryableModelfromai-retry/<family>-model— it is typed for that family and resolves gateway strings for it.See the migration guide to move existing code to the condition API.
Create a retryable model with a base model and a list of conditions plus the action to take when a condition matches.
import { anthropic } from '@ai-sdk/anthropic';
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
import {
createRetryableModel,
error,
finishReason,
httpStatus,
} from 'ai-retry/language-model';
const retryableModel = createRetryableModel({
model: openai('gpt-4o'),
retries: [
// Fall back to a different model on HTTP 529 or any "overloaded" message
httpStatus(529, 'overloaded').switch({
model: anthropic('claude-sonnet-4-0'),
}),
// Fall back when the response was content-filtered
finishReason('content-filter').switch({ model: openai('gpt-4o-mini') }),
// Retry the same model with exponential backoff on retryable errors
error.isRetryable(true).retry({ delay: 1_000, backoffFactor: 2 }),
],
});
const result = await generateText({
model: retryableModel,
prompt: 'Hello world!',
// Let ai-retry own retries; see "Max retries" below
maxRetries: 0,
});
console.log(result.text);
This also works with embedding models and image models, each through their own entry point:
import { openai } from '@ai-sdk/openai';
import { embed } from 'ai';
import { createRetryableModel, httpStatus } from 'ai-retry/embedding-model';
const retryableModel = createRetryableModel({
model: openai.textEmbedding('text-embedding-3-large'),
retries: [
httpStatus(529).switch({
model: openai.textEmbedding('text-embedding-3-small'),
}),
],
});
const result = await embed({ model: retryableModel, value: 'Hello world!' });
import { google } from '@ai-sdk/google';
import { openai } from '@ai-sdk/openai';
import { generateImage } from 'ai';
import { createRetryableModel, noImage } from 'ai-retry/image-model';
const retryableModel = createRetryableModel({
model: openai.image('dall-e-3'),
retries: [
noImage().switch({ model: google.image('gemini-3-pro-image-preview') }),
],
});
const result = await generateImage({
model: retryableModel,
prompt: 'A sunset over mountains',
});
Entry points
Pick the entry point that matches the model you pass to createRetryableModel. Each module exposes the helpers that make sense for that model family already typed for it, so no manual type annotations are needed.
| Entry point | For models passed to |
|---|---|
ai-retry/language-model | generateText, generateObject, streamText, streamObject |
ai-retry/embedding-model | embed, embedMany |
ai-retry/image-model | generateImage |
import { createRetryableModel } from 'ai-retry/language-model';
import { createRetryableModel } from 'ai-retry/image-model';
import { createRetryableModel } from 'ai-retry/embedding-model';
Each entry point re-exports createRetryableModel plus every condition for that family. The condition helpers can also be imported from the dedicated /conditions subpath:
import {
error,
httpStatus,
finishReason,
} from 'ai-retry/language-model/conditions';
// or
import * as conditions from 'ai-retry/language-model/conditions';
Vercel AI Gateway…
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 (544)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 Oct 20, 2026. See the methodology.