Skip to main content
Orchestrate

Subagents

Let an agent hand a focused task to a specialist agent with its own tools.

A subagent is a specialist that a lead agent hands one focused task to. It has its own tools, starts with a fresh context, and only its answer comes back to the lead. A tool on the lead calls it by key, like any other Actor.

Support agentask_specialist toolOrders specialistget_order toolEngineering specialistsandboxquestion

The get_order tool in get-order.ts is the one from Custom Tools.

  • The model picks the specialist and writes the question. The question has to carry everything the specialist needs, because the specialist can’t see the lead’s conversation.
  • Each specialist has only the tools for its job: orders has get_order, and engineering has a sandbox to read and run code. The support agent has neither.
  • Each question goes to a new specialist with an empty conversation. The tool keys it by an id that api.memo saves with the tool call.
  • The tool has replay: "safe", so Pi runs it again after a crash. The rerun gets the same id and passes it as the requestId, so it waits for the run already in progress instead of asking again.
  • If the lead’s run is cancelled, the tool cancels the specialist’s run too.
  • The tool waits for the answer and returns it as the tool result. To hand work off without waiting, see Agent-to-Agent Messages.