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:
User: "Research the top 5 AI frameworks and create a comparison report"Lead Agent: ├── Sub-agent 1: Research TensorFlow ├── Sub-agent 2: Research PyTorch ├── Sub-agent 3: Research JAX ├── Sub-agent 4: Research scikit-learn └── Sub-agent 5: Research Hugging Face Transformers (All run in parallel) Results combined → Comparison report generated
DeerFlow enforces a maximum of 3 concurrent sub-agents to prevent resource exhaustion.Enforcement: The SubagentLimitMiddleware truncates excess task() calls:
class SubagentLimitMiddleware: async def after_model(self, state: ThreadState): task_calls = [msg for msg in state["messages"] if isinstance(msg, AIMessage) and msg.tool_calls] if len(task_calls) > MAX_CONCURRENT_SUBAGENTS: # Truncate excess calls state["messages"][-1].tool_calls = task_calls[:3]
If the agent requests more than 3 sub-agents, only the first 3 are executed.
Multiple task() calls in a single response run concurrently:
# Lead agent's single responsetask(description="Research Python", prompt="...", subagent_type="general-purpose")task(description="Research Go", prompt="...", subagent_type="general-purpose")task(description="Research Rust", prompt="...", subagent_type="general-purpose")# All 3 run simultaneously
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”
Leverage parallelism
When tasks are independent, run them concurrently:
# Step 1: Data collectiontask( description="Collect data", prompt="Scrape data from sources A, B, C", subagent_type="general-purpose")# Step 2: Analysis (after data collected)task( description="Analyze data", prompt="Analyze collected data and generate insights", subagent_type="general-purpose")# Step 3: Visualizationtask( description="Create charts", prompt="Create visualizations from analysis", subagent_type="general-purpose")
# Environment setup with bash specialisttask( description="Setup environment", prompt="Install Python 3.11, create venv, install requirements.txt", subagent_type="bash", max_turns=10)# Code generation with general-purposetask( description="Generate API client", prompt="Create a Python API client for the REST API", subagent_type="general-purpose", max_turns=30)