> ## Documentation Index
> Fetch the complete documentation index at: https://docs.restate.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Sub-agents, handoffs, and forks

> Let the model delegate a subtask to another agent, transfer the conversation to a specialist, or split work across copies of itself.

`agentTool`, `handoff`, and `forkAgentTool` let the model decide when another agent takes part in a run. To decide the control flow in your own code, see [Agents in workflows](https://docs.restate.dev/harness/workflows.md).

## Sub-agents and handoffs

`agentTool` runs another agent as a tool. `handoff` transfers the run to another agent:

```typescript {"CODE_LOAD::harness/src/multi_agent.ts#in_run"}  theme={null}
const lead = agent({
  model,
  instructions: "Answer customer questions. Delegate research, and transfer billing questions.",
  tools: {
    research: agentTool(researcher, {description: "Research one question in depth."}),
    toBilling: handoff(billing, "Transfer billing questions to the billing specialist."),
  },
});
```

* **`agentTool`:**
  * The sub-agent receives the task as a new conversation, together with the parent's `context`. Its answer becomes the tool result, and its token usage counts toward the parent's usage.
  * Its progress events reach the parent as `tool-progress` events, and its approval requests reach the parent's `onApproval` handlers. The sub-agent's own `onHumanApproval` and guardrails apply to its tool calls, not the parent's.
  * A sub-agent that stops at a limit returns a tool error with the code `agent_stopped` and its summary as the message. An interrupted sub-agent returns a `cancelled` result.
  * Pass `background: true` to let the parent continue while the sub-agent runs.
* **`handoff`:**
  * After the current tool calls finish, the target agent's model, instructions, and tools take over the steps of the same run and its history. Tools that you passed to `run` stay available.
  * The agent that started the run keeps its output schema, limits, `beforeRun`, `beforeSteering`, and `afterRun`, and its models for summaries of the context and of a stopped run. Its `beforeStep`, `beforeTool`, and `afterTool` keep running together with the target's.
  * The `onApproval` handlers of both agents receive approval requests, and the target's `onHumanApproval` decides them if the target sets one.
  * The handoff takes effect only if its tool call succeeds, so a `beforeTool` rejection keeps the current agent. A step can hand off once: the run rejects a batch with two handoffs before any of its calls run.

## Forks

`forkAgentTool` starts a copy of the running agent for one task. Unlike a sub-agent, a fork starts with the conversation so far:

```typescript {"CODE_LOAD::harness/src/multi_agent.ts#fork"}  theme={null}
const investigator = agent({
  model,
  instructions: "Investigate incidents. Check plausible causes in parallel with investigate.",
  tools: {
    investigate: forkAgentTool({
      description: "Check one hypothesis with a copy of yourself that sees this conversation.",
    }),
  },
});
```

* The fork copies the run's history, agent, instructions, policies, limits, and tools as of the step that called the tool. The provider can serve the shared part of the request from its prompt cache.
* The fork skips `beforeRun` and `afterRun`. Use the parent's `beforeTool` and `afterTool` hooks to check the fork's task and reply.
* Only the fork's plain-text reply returns to the parent. Its tool calls stay out of the parent's history.
* A fork can't start forks or sub-agents. Such a call returns an error with the code `fork_nested`.
* `maxConcurrent` (default 6) limits how many forks of one fork tool run at once in a run. A call over the limit returns an error with the code `fork_limit`.

Use a fork when the subtask needs the conversation so far and the same model and tools. Use `agentTool` for a specialist with its own model, instructions, or tools, or for work that needs a clean context.

Sub-agents and forks run in the parent's invocation, so their steps add to the parent's journal. For long, independent work, run the agent in its own service, and let the model call it as a [Restate service tool](https://docs.restate.dev/harness/service-tools.md) or call it from your code as a [worker service](https://docs.restate.dev/harness/workflows.md#worker-services).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.