Subagents
Put an agent in another agent's tools so the model can delegate a self-contained task.
Put an agent in another agent's tools to let the model delegate to it. This
is on the npm beta channel (@nylorun/agents@0.5.0-beta and the matching
Runtime / Harness set).
import { Agent } from "@nylorun/agents";
import { z } from "zod";
const researcher = Agent({
id: "researcher",
description:
"Investigates an order's history. Returns a short summary with the ids it relied on.",
instructions:
"Investigate one question about one order. Be exhaustive, then be brief.",
tools: [searchOrders, readTicket],
outputSchema: z.object({
summary: z.string(),
evidence: z.array(z.string()),
}),
});
const support = Agent({
id: "support",
instructions:
"For anything needing more than two lookups, delegate to researcher with a complete, self-contained task.",
tools: [lookupOrder, refundOrder, researcher],
});The tool is named after the child's id and takes { task: string }. The
child's description (required) is what the parent's model reads. The child
starts with a fresh context: it sees its own instructions and the task, nothing
of the parent's conversation, and only its final text (or outputSchema
result) comes back.
connectAgents({ agents: [support] }) serves both. Children share the
session's sandbox, keep their own tools, hooks, skills, and MCP servers, and
start with empty ctx.state. Tools can read ctx.agent
({ id, path, delegationId }). Several delegation calls in one model response
run in parallel.
An empty answer, a failure (with the child's last text marked as evidence), or
a cancelled child reaches the parent's model as a failed tool result, never
as success. Runtime emits delegation.started and delegation.completed.
session.history({ agent }) filters by delegationId (one child invocation)
or by path such as support/researcher (every concurrent child that shares
that path).
This version is one level deep and non-interactive: a delegated agent cannot
use agents as tools, its tools cannot declare approval (keep those on the
parent), and ctx.ask, ctx.approve, ctx.sleep, or ctx.waitFor inside it
fail with delegation.interaction-unsupported. Delegation is not an approval
boundary; approvals live on tools.
Delegate when the parent should keep the answer. When a specialist should own
the rest of the conversation, switch capabilities with a before("step")
patch instead.
See Compatibility for the beta pin set.