AI API Retry Strategies
A practical guide to implementing reliable retry strategies for AI APIs, including exponential backoff, jitter, rate limits, timeouts, idempotency, and retry budgets.
AI API requests can fail for many reasons. A provider may temporarily be overloaded, a request may hit a rate limit, a network connection may fail, or an upstream service may experience a temporary outage. Some failures are temporary and worth retrying, while others indicate a permanent problem that should be returned to the application immediately.
A retry strategy determines what the application should do after a failed AI API request. A good strategy can make an application significantly more reliable without unnecessarily increasing latency or API costs. A bad strategy can do the opposite: repeated retries can amplify traffic, increase costs, create retry storms, and make an existing provider outage worse.
The goal is therefore not to retry every failed request. The goal is to retry only failures that are likely to recover, use controlled delays, stop after a reasonable number of attempts, and preserve predictable behavior for the user.
What Is an AI API Retry Strategy?
A retry strategy defines how an application repeats an AI API request after a failure. It typically specifies which errors are retryable, how long to wait, how many attempts are allowed, and when the application should stop retrying.
Request
↓
AI API
↓
Failure
↓
Is it retryable?
├── No → Return error
└── Yes
↓
Wait
↓
Retry
↓
Success or another failureRetries should normally be implemented on the server-side layer that communicates with the AI provider. This gives the application control over credentials, retry limits, logging, budgets, and provider-specific behavior.
Why AI APIs Need Retry Logic
External AI services are distributed systems. Even when your application is functioning correctly, requests can fail because of temporary network or provider conditions.
| Failure | Potentially retryable? |
|---|---|
| Temporary network failure | Usually yes |
| Connection timeout | Often yes |
| Temporary provider error | Often yes |
| Rate limit | Usually yes, after waiting |
| Invalid API key | No |
| Malformed request | No |
| Unsupported model | No |
| Invalid parameters | No |
| Permission denied | Usually no |
The exact behavior depends on the provider and the specific error. Status codes and provider documentation should be used together with application-level knowledge.
Retryable vs Non-Retryable Errors
The first decision in any retry system is whether the failure is temporary. Retrying a permanent error wastes time and may repeatedly generate the same failure.
- Temporary network failures are commonly retryable.
- Timeouts can be retryable.
- Temporary server-side errors can be retryable.
- Rate-limit responses can be retryable after an appropriate delay.
- Invalid authentication credentials are not normally retryable.
- Invalid request parameters are not normally retryable.
- Unsupported models are not normally retryable.
- Permission errors generally require configuration changes rather than retries.
The Simplest Retry Loop
for (let attempt = 1; attempt <= 3; attempt++) {
try {
return await callAiApi();
} catch (error) {
if (!isRetryable(error)) {
throw error;
}
await wait(1000);
}
}
throw new Error("AI request failed");Although this demonstrates the basic idea, a production implementation should normally use a more sophisticated delay strategy and distinguish different failure types.
Fixed Delay Retries
A fixed-delay strategy waits the same amount of time between attempts. For example, the application might wait one second after every retry.
Attempt 1 → failure
↓ 1s
Attempt 2 → failure
↓ 1s
Attempt 3 → failure
↓
Give upFixed delays are easy to implement but can be problematic when many clients fail simultaneously. All clients may retry at approximately the same time, producing another traffic spike.
Exponential Backoff
Exponential backoff increases the delay after each failed attempt. Instead of retrying at a constant interval, the application progressively gives the provider more time to recover.
Attempt 1 → failure
↓ 1s
Attempt 2 → failure
↓ 2s
Attempt 3 → failure
↓ 4s
Attempt 4 → failure
↓ 8s
Give upA common conceptual formula is a base delay multiplied by an exponentially increasing factor. In production, the delay should usually also have a maximum cap so that one request cannot remain in the retry process indefinitely.
Why Exponential Backoff Helps
When an AI provider is temporarily overloaded, immediately repeating failed requests can increase the load. Exponential backoff reduces the frequency of retries and gives the upstream service time to recover.
Provider overloaded
↓
Requests fail
↓
Clients wait progressively longer
↓
Retry traffic decreases
↓
Provider has time to recoverAdding Jitter
Exponential backoff alone can still cause synchronized retries. If many clients receive an error at the same time and all calculate exactly the same delay, they may retry together.
Without jitter:
Client A → wait 2s → retry
Client B → wait 2s → retry
Client C → wait 2s → retry
With jitter:
Client A → wait 1.7s → retry
Client B → wait 2.3s → retry
Client C → wait 1.9s → retryJitter introduces a small amount of randomness into the delay. This spreads retries over time and reduces synchronized traffic bursts.
Exponential Backoff with Jitter
function getRetryDelay(attempt: number) {
const baseDelay = 1000;
const maxDelay = 30000;
const exponential = Math.min(
baseDelay * 2 ** (attempt - 1),
maxDelay
);
const jitter = Math.random() * exponential;
return jitter;
}The exact jitter algorithm can vary. Full jitter, equal jitter, and decorrelated jitter are different approaches that can be selected according to the application's workload.
Respecting Retry-After
When an AI provider returns a rate-limit response, it may provide a Retry-After value indicating how long the client should wait before trying again.
AI API
↓
429 Too Many Requests
↓
Retry-After: 10
↓
Wait 10 seconds
↓
RetryIf the provider supplies an explicit retry delay, the application should generally respect it rather than blindly applying its own shorter delay.
Retry Limits
Every retry system needs a maximum number of attempts or a maximum retry duration. Without a hard boundary, a temporary failure can turn into a request that occupies server resources for an excessive amount of time.
| Limit | Purpose |
|---|---|
| Maximum attempts | Prevent unlimited retries |
| Maximum delay | Prevent very long waits |
| Maximum total retry time | Bound request duration |
| Retry budget | Control aggregate retry traffic |
A retry policy might allow two or three additional attempts for a user-facing request, while a background job could use a longer retry window.
User-Facing vs Background Retries
Retry policies should depend on the type of operation. A user waiting for an answer has a different tolerance for delays than a background job processing a large document.
| Workload | Typical strategy |
|---|---|
| Interactive chat | Few retries, short total delay |
| Text generation | Limited retries with backoff |
| Background processing | More retries may be acceptable |
| Batch processing | Longer retry window |
| Critical internal job | Persistent queue with controlled retries |
The values should be determined from the application's user experience and reliability requirements rather than copied blindly from another system.
Timeouts and Retries
Retries and timeouts should be designed together. A request that can wait indefinitely before failing can make the retry system ineffective and consume server resources.
Request
↓
Timeout
↓
Retryable failure?
↓
Backoff
↓
Retry
↓
Maximum attempts/time reached?
↓
Return failureThe application should distinguish between the timeout for an individual provider request and the total time allowed for all attempts.
Retrying Rate Limits
Rate-limit errors are often retryable, but immediate retries are counterproductive. The application should wait according to provider guidance or its own controlled backoff policy.
It is also important to distinguish provider rate limits from your application's own rate limits. If your server has intentionally rejected a user because they exceeded your quota, there is no reason for the backend to retry the request against the AI provider.
Avoiding Retry Storms
A retry storm occurs when many clients repeatedly retry failed operations at the same time. Instead of reducing the impact of an outage, retries can amplify it.
Provider outage
↓
10,000 requests fail
↓
10,000 immediate retries
↓
Provider receives another spike
↓
More failures
↓
More retriesExponential backoff, jitter, maximum attempts, concurrency limits, and circuit breakers can all reduce the risk of retry storms.
Circuit Breakers
A circuit breaker temporarily stops sending requests to an unhealthy dependency after repeated failures. Instead of allowing every new request to reach a provider that is clearly unavailable, the application can fail fast for a short period.
Normal
↓
Provider failures increase
↓
Circuit opens
↓
Requests fail fast
↓
Wait
↓
Test provider
↓
Circuit closes if healthyCircuit breakers are especially useful for high-traffic applications or systems that depend on multiple external services.
Idempotency and AI Retries
Retrying an operation can sometimes cause duplicate side effects. AI generation itself is usually read-like from the application's perspective, but an AI request may be part of a larger workflow that creates records, sends messages, charges credits, or triggers external actions.
If an operation can create side effects, use an idempotency strategy so that retrying the same logical request does not accidentally perform the side effect multiple times.
Client request
↓
Idempotency key
↓
Process operation
↓
Store result
↓
Repeated request
↓
Return existing resultRetries and AI Costs
Every retry can potentially create another billable AI request. Retry logic should therefore be included in cost calculations rather than treated as free reliability infrastructure.
| Scenario | Potential impact |
|---|---|
| One failed request | One additional attempt |
| Repeated transient failures | Multiple provider calls |
| Large prompts | Repeated input-token costs |
| Long outputs | Repeated output-token costs |
| Retry storm | Rapid cost increase |
This is another reason to keep retry counts conservative and to monitor retry frequency separately from normal request volume.
Retry Budgets
A retry budget limits how much additional traffic the application is willing to generate because of failures. Instead of allowing every request to retry freely, the system can restrict retries when failure rates become unusually high.
Normal traffic
↓
Retry budget available
↓
Retries allowed
Failure spike
↓
Retry budget consumed
↓
Fewer retries
↓
Protect provider and applicationRetry budgets are especially valuable for high-volume services where a provider outage could otherwise cause a large multiplication of requests.
Retries in a Queue-Based Architecture
Long-running AI operations are often better handled as background jobs instead of keeping an HTTP request open through multiple retries.
User
↓
Create job
↓
Queue
↓
Worker
↓
AI provider
↓
Failure
↓
Retry with backoff
↓
Success → save resultA queue allows retry state to survive server restarts and makes it easier to control concurrency, retry schedules, and failed jobs.
Retrying Streaming Requests
Streaming requests require additional care. If a stream has already delivered part of the response to the user and then fails, blindly starting a new generation can produce duplicated or inconsistent output.
AI stream
↓
chunk 1
↓
chunk 2
↓
network failure
↓
What should happen?
Options:
- show partial result
- restart generation
- resume if supported
- ask user to retryFor interactive applications, it is often better to surface the partial result and provide a controlled retry action rather than automatically starting an expensive duplicate generation.
Retries with Multiple AI Providers
Applications using multiple providers can sometimes use fallback routing. If one provider is temporarily unavailable, another provider may handle the request.
Request
↓
Provider A
↓
Temporary failure
↓
Fallback policy
↓
Provider BProvider fallback should not be treated as a universal retry mechanism. Different models can have different capabilities, pricing, context limits, output formats, and safety characteristics. The fallback model must be compatible with the operation.
A Practical Retry Function
async function withRetry<T>(
operation: () => Promise<T>,
maxAttempts = 3
): Promise<T> {
let lastError: unknown;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
return await operation();
} catch (error) {
lastError = error;
if (!isRetryable(error) || attempt === maxAttempts) {
throw error;
}
const baseDelay = 1000 * 2 ** (attempt - 1);
const jitter = Math.random() * baseDelay;
const delay = Math.min(baseDelay + jitter, 30000);
await new Promise((resolve) => {
setTimeout(resolve, delay);
});
}
}
throw lastError;
}A production implementation should additionally consider provider-specific Retry-After values, request cancellation, total retry duration, logging, metrics, and different policies for different error classes.
Logging Retry Attempts
Retry attempts should be observable. Useful metrics include the original error, attempt number, delay, endpoint, model, total duration, and final result.
| Metric | Why it matters |
|---|---|
| Retry count | Shows how often failures require recovery |
| Retry rate | Reveals provider or network instability |
| Retry success rate | Shows whether retries are useful |
| Final failure rate | Measures unresolved failures |
| Retry latency | Shows user-facing delay |
| Retry token usage | Measures additional AI cost |
Avoid logging sensitive prompts or private AI responses unnecessarily. Operational logs should contain enough information to diagnose reliability problems without becoming an accidental store of sensitive user data.
How to Tune Retry Policies
Retry parameters should be based on real application behavior. A retry policy that works for a low-volume internal tool may be inappropriate for a public application with thousands of concurrent users.
- Measure provider failure frequency.
- Measure how often retries succeed.
- Measure the additional latency caused by retries.
- Measure additional token and API costs.
- Separate user-facing and background workloads.
- Adjust maximum attempts according to operation importance.
- Use provider Retry-After information when available.
- Watch for synchronized retry patterns.
Recommended Retry Strategy
For many AI web applications, a practical baseline is to retry only transient network and server-side failures, use exponential backoff with jitter, respect provider retry instructions, cap the delay, and stop after a small number of attempts.
AI request
↓
Classify error
↓
Non-retryable → fail immediately
↓
Retryable
↓
Check retry budget
↓
Respect Retry-After if provided
↓
Exponential backoff + jitter
↓
Retry
↓
Maximum attempts/time?
├── No → continue
└── Yes → fail gracefullyCommon Mistakes
- Retrying every error.
- Retrying immediately without a delay.
- Using the same fixed delay for every attempt.
- Ignoring jitter.
- Ignoring Retry-After.
- Allowing unlimited attempts.
- Ignoring total retry duration.
- Retrying application-level quota failures.
- Retrying invalid API credentials.
- Ignoring the additional AI cost of retries.
- Automatically retrying partially completed streams.
- Allowing thousands of clients to retry simultaneously.
- Failing to log retry behavior.
Best Practices Checklist
- Classify errors before retrying.
- Retry only failures that are likely to be temporary.
- Use exponential backoff for repeated failures.
- Add jitter to avoid synchronized retries.
- Respect provider Retry-After information.
- Set a maximum number of attempts.
- Set a maximum retry delay.
- Consider a maximum total retry duration.
- Use timeouts for individual provider requests.
- Use concurrency limits for high-volume workloads.
- Consider circuit breakers for unreliable dependencies.
- Use idempotency for operations with side effects.
- Track retry frequency and success rate.
- Track additional AI usage and cost.
- Use queue-based retries for long-running background work.
- Keep user-facing retries shorter than background-job retries.
Frequently Asked Questions
How many times should an AI API request be retried?
There is no universal number, but user-facing AI requests commonly benefit from a small number of additional attempts. The correct value depends on the provider, operation, latency requirements, and cost. Background jobs can usually tolerate longer retry policies.
Should I retry HTTP 429 responses from an AI API?
Often yes, but only after an appropriate delay. If the provider supplies a Retry-After value, the application should generally respect it. Retrying immediately can make rate limiting worse.
Why is jitter important for AI API retries?
Without jitter, many clients that fail at the same time can calculate the same retry delay and send another burst simultaneously. Jitter spreads retry attempts over time and reduces synchronized traffic spikes.
Should every AI API error be retried?
No. Invalid credentials, malformed requests, unsupported models, and many permission errors are not temporary. Retrying them usually adds latency and unnecessary traffic without improving the result.
Can retries increase AI API costs?
Yes. Every retry may create another provider request and consume additional input and output tokens. Retry policies should therefore be included in cost monitoring and should have strict limits.
Conclusion
A reliable AI application should expect some external API requests to fail. The solution is not to retry everything, but to distinguish temporary failures from permanent ones and recover from temporary failures in a controlled way.
Exponential backoff with jitter is a strong general-purpose strategy because it progressively reduces retry pressure while preventing large groups of clients from retrying simultaneously. Provider-provided Retry-After information should also be respected whenever available.
Retries should always have clear boundaries. Maximum attempts, maximum delays, timeouts, retry budgets, concurrency limits, and circuit breakers can prevent a temporary provider problem from becoming a larger reliability or cost problem for your application.
Finally, measure retry behavior. A high retry rate may indicate a provider problem, an overly aggressive request pattern, or an application configuration issue. When retries are treated as part of the application's reliability and cost architecture rather than as a simple catch-and-repeat loop, AI integrations become considerably more predictable and resilient.