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.
Agent is a Dispatcher that transparently manages one connection pool per origin. When you make a request to https://api.example.com, Agent creates (and caches) a Pool for that origin. The next request to the same origin reuses the pool. When no requests remain for an origin, Agent closes and discards the pool automatically.
Agent is the right choice when your application talks to multiple services or when you want a single dispatcher wired up via setGlobalDispatcher so that every call to fetch() or undici.request() uses your configuration.
Agent extends Dispatcher. All methods (request, stream, pipeline,
dispatch, connect, upgrade, close, destroy) are inherited — see
the Dispatcher reference for full signatures.Constructor
new Agent(options?)
body.factory
(origin: URL, opts: object) => Dispatcher
default:"(origin, opts) => new Pool(origin, opts)"
Called once per origin to create the backing dispatcher. When
connections === 1, the default factory creates a Client instead of a
Pool. Override this to use custom classes, decorate with interceptors, or
apply per-origin configuration.Maximum number of distinct origins the agent will dispatch to simultaneously.
Throws
MaxOriginsReachedError when the limit is reached and a new origin
is requested.TLS and socket options forwarded to every pool/client created by
factory.
Pass a function for a custom connector.PoolOptions are accepted and forwarded to each pool created by factory.
Methods
All standardDispatcher methods are available. Agent.dispatch() requires that options.origin is set because Agent uses it to look up (or create) the correct pool.
agent.stats()
Returns per-origin statistics as a Record<string, ClientStats | PoolStats> keyed by origin string.
Inspecting per-origin stats
Properties
true after agent.close() has been called.true after agent.destroy() has been called or close has completed.Code examples
Basic Agent usage
Global dispatcher — applies to fetch() and undici.request()
Custom factory — per-origin interceptors
Capping the number of origins
setGlobalDispatcher and getGlobalDispatcher
These helpers control the dispatcher used by undici.request(), undici.fetch(), and other module-level functions.
Wiring up a global Agent
setGlobalDispatcher also exposes the dispatcher under
Symbol.for('undici.globalDispatcher.1') via a Dispatcher1Wrapper so
that Node.js built-in fetch (which uses the legacy v1 handler contract)
continues to work after you replace the global dispatcher.