Overview
Retry failed operations with exponential backoff and jitter. The Agents SDK provides built-in retry support for scheduled tasks, queued tasks, and a general-purposethis.retry() method for your own code.
Transient failures are common when calling external APIs, interacting with other services, or running background tasks. The retry system handles these automatically:
- Exponential backoff — each retry waits longer than the last
- Jitter — randomized delays prevent thundering herd problems
- Configurable — tune attempts, delays, and caps per call site
- Built-in — schedule, queue, and workflow operations retry automatically
Quick Start
Usethis.retry() to retry any async operation:
this.retry() makes up to 3 attempts with jittered exponential backoff.
this.retry()
Theretry() method is available on every Agent instance. It retries the provided function on any thrown error by default.
fn— the async function to retry. Receives the current attempt number (1-indexed).options— optional retry configuration (see RetryOptions below). Options are validated eagerly — invalid values throw immediately.options.shouldRetry— optional predicate called with the thrown error and the next attempt number. Returnfalseto stop retrying immediately. If not provided, all errors are retried.
fn on success.
Throws: the last error if all attempts fail or shouldRetry returns false.
Examples
Basic retry:shouldRetry:
Use shouldRetry to stop retrying on specific errors. The predicate receives both the error and the next attempt number:
Retries in Schedules
Pass retry options when creating a schedule:onError(). The schedule is still removed (for one-time schedules) or rescheduled (for cron/interval) regardless of success or failure.
Retries in Queues
Pass retry options when adding a task to the queue:Validation
Retry options are validated eagerly when you callthis.retry(), queue(), schedule(), or scheduleEvery(). Invalid options throw immediately instead of failing later at execution time:
{ baseDelayMs: 5000 } is caught immediately when the resolved maxDelayMs is 3000, rather than failing later at execution time.
Default Behavior
Even without explicit retry options, scheduled and queued callbacks are retried with sensible defaults:
These defaults apply to
this.retry(), queue(), schedule(), and scheduleEvery(). Per-call-site options override them.
Class-Level Defaults
Override the defaults for your entire agent viastatic options:
maxAttempts: 1:
RetryOptions
How It Works
Backoff Strategy
The retry system uses the “Full Jitter” strategy from the AWS Architecture Blog. Given 3 attempts with default settings:
With
maxAttempts: 5 and baseDelayMs: 500:
MCP Server Retries
When adding an MCP server, you can configure retry options for connection and reconnection attempts:- Restoring server connections after hibernation
- Establishing connections after OAuth completion
Internal Retries
The SDK also uses retries internally for platform operations:- Workflow operations (
terminateWorkflow,pauseWorkflow,resumeWorkflow,restartWorkflow,sendEventToWorkflow) — retried with Durable Object-aware error detection. Transient DO errors are retried; overloaded errors are not.
Patterns
Retry with Logging
Retry with Fallback
Combining Retries with Scheduling
For operations that might take a long time to recover (minutes or hours), combinethis.retry() for immediate retries with this.schedule() for delayed retries:
Limitations
Related
- Scheduling — schedule tasks for future execution
- Queue — background task queue
- Workflows — durable multi-step processing with automatic retries