Constconst researcher = ai.defineAgent({
name: 'researcher',
description: 'Searches the web and summarizes findings.',
...
});
const coder = ai.defineAgent({ name: 'coder', ... });
const orchestrator = ai.defineAgent({
name: 'orchestrator',
system: 'You are a helpful project assistant.',
use: [
agents({
agents: [
'researcher', // auto-discovered description
{ name: 'coder', description: 'Writes TypeScript code' }, // explicit override
],
maxDelegations: 5,
historyLength: 4,
artifactStrategy: 'session', // pair with artifacts() middleware
}),
artifacts(),
],
});
Creates a middleware that enables sub-agent delegation.
For every agent listed in the configuration the middleware injects a dedicated delegation tool (e.g.
delegate_to_researcher) whose description is automatically populated from the agent's registry metadata — or can be overridden in configuration. A<sub-agents>block is appended to the system prompt listing the available agents and their descriptions.When the model calls a delegation tool the middleware:
Artifact handling is controlled by the
artifactStrategyoption:"inline"(default): Artifact content is included in the tool result so the orchestrator model can reason about it, AND artifacts are merged into the parent session (prefixed with an invocation ID for namespacing)."session": Artifacts are merged into the parent session only. The tool result mentions artifact names but not content. Pair with theartifactsmiddleware to give the modelread_artifact/write_artifacttools.If a sub-agent triggers an interrupt, it is reported back to the orchestrator as a normal tool response (not propagated as a
ToolInterruptError). There is no stateful sub-agent runtime to resume into, so interactive, back-and-forth interaction with an interrupted sub-agent is a future feature.