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

# Sub-agents

> Delegate tasks to a specialist sub-agent with approval flow

Main agent delegates to a `math-specialist` sub-agent. Tool and delegation approval events from the specialist fan in to the main agent's stream.

Source: [`examples/agent_with_subagents/`](https://github.com/agenticenv/agent-sdk-go/tree/main/examples/agent_with_subagents)

## What it demonstrates

* `WithSubAgents` registering a specialist with its own tools and a distinct `WithName`
* Delegation approval (`CUSTOM` delegation events) and tool approval from sub-agent streams

## Run

From `examples/`:

```bash theme={null}
go run ./agent_with_subagents "Use the math specialist to compute 847 * 293"
```

Approve delegation and tool calls when prompted on stdin.

## Key code

```go theme={null}
mathAgent, err := agent.NewAgent(
    agent.WithName("math-specialist"),
    agent.WithToolRegistry(mathReg),
    agent.WithLLMClient(llmClient),
    // plus runtime options
)

mainAgent, err := agent.NewAgent(
    agent.WithName("main-agent"),
    agent.WithSubAgents(mathAgent),
    agent.WithLLMClient(llmClient),
    // plus runtime options
)
```

See [Sub-agents](/features/sub-agents).

## Expected output

```
Delegating to math-specialist...
Approve delegation to math-specialist? [y/n]: y
Tool call: calculator({"expression":"847*293"})
Approve? [y/n]: y
847 × 293 = 248,171
```

The main agent emits a delegation approval event before calling the sub-agent. The sub-agent then calls its tools and approval events fan in to the main agent's stream.

## Learn more

<CardGroup cols={2}>
  <Card title="Sub-agents" icon="sitemap" href="/features/sub-agents" horizontal>
    Depth limits, policies, streaming fan-in
  </Card>

  <Card title="Approvals" icon="shield-check" href="/features/approvals" horizontal>
    Delegation and tool approval
  </Card>
</CardGroup>


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