Skip to main content
Sub-agents let a main agent delegate work to specialist agents — each with its own LLM, tools, prompts, and Temporal task queue. The main agent sees each specialist as a delegation tool the LLM can invoke. Use sub-agents for domain specialists (math, research, coding) without concentrating every capability into one prompt and tool list.

Setup

Build each specialist with NewAgent, then register it on the main agent:
mathAgent, err := agent.NewAgent(
    agent.WithName("MathSpecialist"),
    agent.WithDescription("Arithmetic; uses calculator tools."),
    agent.WithTemporalConfig(&agent.TemporalConfig{
        Host: "localhost", Port: 7233, Namespace: "default",
        TaskQueue: "my-app-math",  // unique task queue for this specialist
    }),
    agent.WithLLMClient(llmClient),
    agent.WithToolRegistry(mathTools),
    agent.WithToolApprovalPolicy(agent.AutoToolApprovalPolicy()),
)
if err != nil {
    return err
}
defer mathAgent.Close()

mainAgent, err := agent.NewAgent(
    agent.WithName("Orchestrator"),
    agent.WithSystemPrompt("You are a helpful assistant."),
    agent.WithTemporalConfig(&agent.TemporalConfig{
        Host: "localhost", Port: 7233, Namespace: "default",
        TaskQueue: "my-app-main",
    }),
    agent.WithLLMClient(llmClient),
    agent.WithSubAgents(mathAgent),
    agent.WithMaxSubAgentDepth(2),
)
if err != nil {
    return err
}
defer mainAgent.Close()

result, err := mainAgent.Run(ctx, "What is 144 divided by 12?", nil)
OptionDescription
WithSubAgentsRegister specialists at creation
WithMaxSubAgentDepthMax delegation nesting depth — default 2
Each specialist needs a unique TaskQueue when using Temporal. Use clear WithName and WithDescription values — they appear in the delegation tool schema the LLM sees for routing decisions.

Behavior

  • Conversation isolation — sub-agents do not inherit the main agent’s conversation ID. They run without session history from the parent.
  • Independent approval policies — parent and child policies are independent (see Approvals).
  • Worker pairing — with DisableLocalWorker, pair each NewAgentWorker with the same options as the NewAgent it runs.
  • Validation at build — sub-agent graphs are validated for cycles and depth violations at NewAgent. Errors fail fast.
Sub-agents do not share the parent’s conversation history. If you need the specialist to have context from the parent session, include it in the delegation prompt or tool call arguments.

Streaming

When streaming, events from all delegated sub-agents fan in to the main agent’s stream, bracketed by STEP_STARTED and STEP_FINISHED. See Streaming for the full event reference and approval handling patterns.

Example

Sub-agents

Orchestrator delegating to a math specialist

A2A

Remote agents over the A2A protocol

Approvals

Parent vs specialist approval policies

Dynamic Capabilities

Register sub-agents after NewAgent

Multiple Agents

Instance IDs and task queue patterns