Skip to main content
Sub-agents enable the lead agent to delegate complex, multi-step tasks to specialized agents that run in parallel or sequentially.

Overview

Complex tasks rarely fit in a single pass. The lead agent can spawn sub-agents on the fly to:
  • Research multiple topics concurrently
  • Execute specialized workflows in parallel
  • Handle time-consuming operations asynchronously
  • Isolate context for focused sub-tasks
Example Flow:

The task() Tool

Sub-agents are spawned via the task() tool:

Parameters

string
required
Brief task summary (shown in UI)
string
required
Detailed instructions for the sub-agent
string
default:"general-purpose"
Type of sub-agent to use: general-purpose or bash
integer
default:"20"
Maximum conversation turns (prevents infinite loops)

Sub-agent Types

DeerFlow includes two built-in sub-agent types:
Description: Full-capability agent with all tools except task()Tools:
  • Sandbox tools (bash, read_file, write_file, etc.)
  • Research tools (web_search, web_fetch)
  • Built-in tools (present_files, ask_clarification)
  • Skills access
  • Memory access
Use Cases:
  • Research tasks
  • Code generation
  • Data analysis
  • Content creation
Example:

Concurrency Limits

DeerFlow enforces a maximum of 3 concurrent sub-agents to prevent resource exhaustion. Enforcement: The SubagentLimitMiddleware truncates excess task() calls:
If the agent requests more than 3 sub-agents, only the first 3 are executed.

Execution Flow

1. Task Submission

The lead agent calls task():

2. Background Execution

The task is submitted to SubagentExecutor:

3. Progress Events

The sub-agent emits events during execution:

4. Result Collection

The lead agent receives results:

Context Isolation

Each sub-agent runs in its own isolated context: Isolated:
  • Conversation history
  • Tool call history
  • Intermediate results
Shared:
  • Sandbox filesystem
  • Thread data directory
  • Skills access
  • Memory (read-only)
Sub-agents cannot see the lead agent’s conversation or other sub-agents’ contexts.

Timeout Handling

Sub-agents have a 15-minute timeout:
Best Practice: Break long tasks into smaller sub-tasks.

Parallel vs Sequential

Parallel Execution

Multiple task() calls in a single response run concurrently:

Sequential Execution

Wait for results between calls:

Error Handling

Sub-agents can fail in several ways:
The sub-agent encountered an error during execution:
Action: Retry with corrected prompt or handle the error.
The sub-agent exceeded the 15-minute limit:
Action: Break the task into smaller sub-tasks.
More than 3 sub-agents requested:
Action: Wait for existing tasks to complete.

Best Practices

Sub-agents work better with detailed instructions:Good: “Research PyTorch’s distributed training capabilities, focusing on DDP and FSDP. Include code examples and performance benchmarks.”Bad: “Research PyTorch”
When tasks are independent, run them concurrently:
Sub-agents share the same sandbox filesystem:
Limit turns based on task complexity:
  • Simple tasks: max_turns=5
  • Medium tasks: max_turns=20 (default)
  • Complex tasks: max_turns=50

Use Cases

Research Aggregation

Multi-Step Workflow

Specialized Tasks

Next Steps

Sub-agent Tool API

Complete task() tool reference

Configuration

Configure sub-agent behavior

Sandbox

Understand shared filesystem

Context Engineering

Learn about context isolation