Skip to main content

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-purpose this.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

Use this.retry() to retry any async operation:
By default, this.retry() makes up to 3 attempts with jittered exponential backoff.

this.retry()

The retry() method is available on every Agent instance. It retries the provided function on any thrown error by default.
Parameters:
  • 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. Return false to stop retrying immediately. If not provided, all errors are retried.
Returns: the result of fn on success. Throws: the last error if all attempts fail or shouldRetry returns false.

Examples

Basic retry:
Custom retry options:
Using the attempt number:
Selective retry with 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:
If the callback throws, it is retried according to the retry options. If all attempts fail, the error is logged and routed through 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:
If the callback throws, it is retried before the task is dequeued. After all attempts are exhausted, the task is dequeued and the error is logged.

Validation

Retry options are validated eagerly when you call this.retry(), queue(), schedule(), or scheduleEvery(). Invalid options throw immediately instead of failing later at execution time:
Validation resolves partial options against class-level or built-in defaults before checking cross-field constraints. This means { 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 via static options:
You only need to specify the fields you want to change — unset fields fall back to the built-in defaults:
Class-level defaults are used as fallbacks when a call site does not specify retry options. Per-call-site options always take priority:
To disable retries for a specific task, set maxAttempts: 1:

RetryOptions

The delay between retries uses full jitter exponential backoff:
This means early retries are fast (often under 200ms), and later retries back off to avoid overwhelming a failing service. The randomization (jitter) prevents multiple agents from retrying at the exact same moment.

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:
These options are persisted and used when:
  • Restoring server connections after hibernation
  • Establishing connections after OAuth completion
Default: 3 attempts, 500ms base delay, 5s max delay.

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.
These internal retries use hardcoded defaults and are not configurable.

Patterns

Retry with Logging

Retry with Fallback

Combining Retries with Scheduling

For operations that might take a long time to recover (minutes or hours), combine this.retry() for immediate retries with this.schedule() for delayed retries:

Limitations

  • No dead-letter queue. If a queued or scheduled task fails all retry attempts, it is removed. Implement your own persistence if you need to track failed tasks.
  • Retry delays block the agent. During the backoff delay, the Durable Object is awake but idle. For short delays (under 3 seconds) this is fine. For longer recovery times, use this.schedule() instead.
  • Queue retries are head-of-line blocking. Queue items are processed sequentially. If one item is being retried with long delays, it blocks all subsequent items. If you need independent retry behavior, use this.retry() inside the callback rather than per-task retry options on queue().
  • No circuit breaker. The retry system does not track failure rates across calls. If a service is persistently down, each task will exhaust its retry budget independently.
  • shouldRetry is only available on this.retry(). The shouldRetry predicate cannot be used with schedule() or queue() because functions cannot be serialized to the database. For scheduled/queued tasks, handle non-retryable errors inside the callback itself.
  • Scheduling — schedule tasks for future execution
  • Queue — background task queue
  • Workflows — durable multi-step processing with automatic retries