Documentation Index
Fetch the complete documentation index at: https://mintlify.com/nodejs/undici/llms.txt
Use this file to discover all available pages before exploring further.
RetryAgent wraps any Dispatcher — a Client, Pool, Agent, or even another RetryAgent — and automatically retries requests that fail due to network errors or particular HTTP status codes. It uses exponential backoff with a configurable factor and respects Retry-After response headers from the server.
undici also offers an
interceptors.retry interceptor that can be composed
onto any dispatcher via .compose(). The key difference is scope:
RetryAgent wraps an entire dispatcher so every request through it is
retried, while interceptors.retry can be applied selectively at the
dispatcher or even at the request level when composed with .compose().Constructor
new RetryAgent(dispatcher, options?)
The underlying dispatcher to wrap. Can be a
Client, Pool, Agent,
ProxyAgent, or any other Dispatcher instance.Maximum number of retry attempts before the error is thrown to the caller.
Minimum milliseconds to wait before the first retry.
Maximum milliseconds to wait between retries, regardless of the backoff
calculation.
Multiplier applied to the timeout on each successive retry attempt
(exponential backoff). With
minTimeout: 500 and timeoutFactor: 2, waits
are: 500 ms, 1000 ms, 2000 ms, 4000 ms, …When
true, honour the Retry-After response header. The agent waits for
the indicated duration before retrying instead of using the backoff
calculation.HTTP response status codes that trigger a retry.
HTTP methods eligible for retry. Non-idempotent methods like
POST and
PATCH are excluded by default to prevent unintended duplicate writes.Node.js error codes that trigger a retry regardless of status code.
When
true, throws an error after the final retry attempt fails. Set to
false if you need access to the response body on error responses or want
to handle failures in a custom retry callback.Custom retry decision function. Called after every failed attempt. Invoke
callback() to retry, or callback(err) to stop retrying and propagate
the error.Methods
RetryAgent exposes dispatch(), close(), and destroy() which delegate to the wrapped dispatcher after applying retry logic.
Code examples
Basic RetryAgent
Custom retry configuration
Custom retry logic with callback
RetryAgent wrapping a ProxyAgent
Retrying without throwing on final failure
RetryAgent vs interceptors.retry
Both mechanisms add retry behaviour, but they differ in how they are applied:
RetryAgent | interceptors.retry | |
|---|---|---|
| Applied to | Entire wrapped dispatcher | Specific dispatcher via .compose() |
| Granularity | All requests through the agent | Can be scoped per-dispatcher or chained |
| Usage | new RetryAgent(dispatcher, opts) | dispatcher.compose(interceptors.retry(opts)) |
| Composable with other interceptors | Wrap the composed result | Yes, via chained .compose() |
Equivalent using interceptors.retry