Skip to content

How to Delegate Work to Sub-agents with Deep Agents

Problem

When I build AI agents for complex workflows, I hit the same wall every time: context window limits. My agent starts great, but as the conversation grows, it forgets earlier instructions or gets confused by too much information.

I tried breaking tasks into smaller prompts, but that created a new problem—managing state across multiple conversations. Then I discovered Deep Agents and its task tool for spawning sub-agents with isolated context windows.

What I needed

My research agent had to do three things that fought each other:

  1. Search the web for information
  2. Analyze and synthesize findings
  3. Generate a final report

Each step required different tools and context. When I tried to do everything in one conversation, the agent got confused. It would mix up search queries with analysis, or forget to include sources in the final report.

I needed a way to delegate focused tasks to fresh agent instances without losing the overall workflow.

How sub-agents work in Deep Agents

Deep Agents provides a task tool that spawns specialized sub-agents. Each sub-agent gets:

  • Its own isolated context window
  • A focused system prompt
  • Specific tools for its task
  • No memory pollution from the main conversation

This means the main agent can delegate work, and sub-agents work independently without context overflow.

The three sub-agent types

I found three types of sub-agents in Deep Agents:

  1. SubAgent - Declarative specification with name, description, and system_prompt
  2. CompiledSubAgent - Pre-built runnable graph for reusable patterns
  3. AsyncSubAgent - Remote/background agent for LangSmith deployments

For most use cases, the declarative SubAgent is what you need.

My first attempt

I started by creating a simple research agent with a web search sub-agent.

research_agent.py
from deepagents import create_deep_agent
agent = create_deep_agent(
subagents=[
{
"name": "researcher",
"description": "Searches the web for information on a given topic",
"system_prompt": "You are a research assistant. Search for information and return a summary of your findings.",
"tools": [web_search_tool],
}
]
)

This worked, but I realized I was missing something. The sub-agent needed better instructions on what to return.

Refining the approach

I updated the system prompt to be more specific about outputs.

research_agent_v2.py
from deepagents import create_deep_agent
agent = create_deep_agent(
subagents=[
{
"name": "researcher",
"description": "Searches the web for information on a given topic",
"system_prompt": """You are a research assistant.
Search for information and return:
1. Key findings as bullet points
2. Source URLs for each finding
3. Any contradictions you find
Be thorough but concise. Focus on facts, not opinions.""",
"tools": [web_search_tool],
}
]
)

Better, but I still had one sub-agent doing too much. I needed to split responsibilities.

Adding multiple sub-agents

I created separate sub-agents for each phase of research.

research_agent_v3.py
from deepagents import create_deep_agent
agent = create_deep_agent(
subagents=[
{
"name": "web_researcher",
"description": "Searches the web for factual information",
"system_prompt": "You search the web for facts. Return findings with source URLs.",
"tools": [web_search_tool],
},
{
"name": "doc_researcher",
"description": "Searches official documentation",
"system_prompt": "You search official docs. Return relevant sections with citations.",
"tools": [doc_search_tool],
},
{
"name": "synthesizer",
"description": "Combines research into a coherent report",
"system_prompt": "You synthesize research findings into a clear report. Resolve contradictions and cite sources.",
"tools": [], # No external tools needed
}
]
)

Now the main agent could delegate to the right sub-agent for each task.

The delegation strategy

I found the Deep Agents documentation includes delegation instructions. Here’s the pattern:

delegation_strategy.txt
Simple queries: Use 1 sub-agent for focused research
Comparisons: Use 1 sub-agent per element being compared
Multi-faceted research: Use 1 sub-agent per aspect
Max concurrent sub-agents: 3 (to avoid rate limits)

This strategy prevents overwhelming the system while ensuring each sub-agent has a clear, focused task.

A complete example

Here’s how I structured a deep research workflow.

deep_research.py
from deepagents import create_deep_agent, SubAgent
# Define tools
web_search_tool = create_web_search_tool()
doc_search_tool = create_doc_search_tool()
# Create sub-agents
researchers = [
SubAgent(
name="fact_checker",
description="Verifies claims by searching authoritative sources",
system_prompt="""You are a fact-checker.
Given a claim, search for authoritative sources.
Return:
- Whether the claim is supported
- Sources that support or contradict
- Confidence level (high/medium/low)""",
tools=[web_search_tool, doc_search_tool],
),
SubAgent(
name="explainer",
description="Explains complex topics in simple terms",
system_prompt="""You are an explainer.
Given a topic, search for information and explain it simply.
Return:
- A plain-English explanation
- Key concepts with definitions
- Common misconceptions""",
tools=[web_search_tool],
),
SubAgent(
name="citation_finder",
description="Finds academic and official citations",
system_prompt="""You find citations.
Given a topic, search for:
- Academic papers
- Official documentation
- Authoritative blog posts
Return citations in a consistent format.""",
tools=[doc_search_tool],
)
]
# Create main agent
agent = create_deep_agent(
subagents=researchers,
model="claude-opus-4-20250514",
)
# Run the agent
result = agent.invoke({
"messages": [
{
"role": "user",
"content": "Research sub-agents in AI systems. Focus on: 1) How they work, 2) Best practices, 3) Common pitfalls."
}
]
})
print(result["messages"][-1]["content"])

What happened when I ran this

The main agent analyzed my request and decided to:

  1. Spawn fact_checker to verify claims about sub-agent architecture
  2. Spawn explainer to get a simple explanation of the concept
  3. Spawn citation_finder to gather official documentation links

Each sub-agent ran with its own context, did focused work, and returned results. The main agent then synthesized everything into a coherent response.

Parallel execution

One benefit I discovered: sub-agents can run in parallel when tasks are independent.

parallel_research.py
from deepagents import create_deep_agent
agent = create_deep_agent(
subagents=[
{
"name": "researcher_a",
"description": "Researches topic A",
"system_prompt": "Focus on technical aspects.",
"tools": [web_search_tool],
},
{
"name": "researcher_b",
"description": "Researches topic B",
"system_prompt": "Focus on business aspects.",
"tools": [web_search_tool],
},
{
"name": "researcher_c",
"description": "Researches topic C",
"system_prompt": "Focus on user experience aspects.",
"tools": [web_search_tool],
}
]
)
# The main agent can invoke multiple sub-agents concurrently
# Deep Agents handles the parallel execution automatically

The main agent sends tasks to all three researchers at once, then combines results. This cut my research time significantly for multi-faceted topics.

Common mistakes I made

Mistake 1: Overlapping responsibilities

At first, I gave sub-agents overlapping responsibilities.

bad_overlap.py
# BAD: Overlapping responsibilities
subagents=[
{
"name": "researcher",
"description": "Search for and analyze information", # Too broad!
},
{
"name": "analyzer",
"description": "Analyze search results and findings", # Overlaps with researcher!
}
]

This caused confusion. Sub-agents would duplicate work or contradict each other.

Mistake 2: Too many tools per sub-agent

I gave every sub-agent all available tools.

too_many_tools.py
# BAD: Every sub-agent has all tools
all_tools = [web_search, doc_search, code_search, email_sender, file_writer]
subagents=[
{"name": "task1", "tools": all_tools}, # Overwhelming!
{"name": "task2", "tools": all_tools}, # Confusing!
]

Better to give each sub-agent only the tools it needs.

right_tools.py
# GOOD: Each sub-agent has focused tools
subagents=[
{
"name": "web_searcher",
"description": "Searches the web",
"tools": [web_search_tool], # Only web search
},
{
"name": "doc_writer",
"description": "Writes documentation",
"tools": [file_writer], # Only file writing
}
]

Mistake 3: Vague system prompts

My first system prompts were too vague.

vague_prompt.py
# BAD: Vague instructions
"system_prompt": "You are a helpful assistant." # Too generic!

I needed to be specific about what the sub-agent should do and return.

specific_prompt.py
# GOOD: Specific instructions
"system_prompt": """You are a web researcher.
Given a query:
1. Search for 5 relevant sources
2. Extract key information from each
3. Return findings with URLs
Format your response as:
- Finding 1: [text] (Source: [url])
- Finding 2: [text] (Source: [url])
..."""

When to use sub-agents

I found sub-agents useful for:

  • Research tasks where different aspects need different expertise
  • Multi-step workflows where each step has distinct tools
  • Parallel processing of independent tasks
  • Context isolation when previous context would confuse the task

Not useful for:

  • Simple single-step tasks
  • Tasks that require remembering everything from earlier
  • Quick lookups where context isn’t an issue

Summary

In this post, I showed how Deep Agents’ task tool enables sub-agent delegation with isolated context windows. The key point is sub-agents prevent context overflow and enable parallel execution of complex workflows.

I covered three sub-agent types (SubAgent, CompiledSubAgent, AsyncSubAgent), shared working code examples, and explained the delegation strategy. The main takeaways:

  • Each sub-agent gets an isolated context window
  • Be specific about responsibilities and outputs
  • Give each sub-agent only the tools it needs
  • Use parallel execution for independent tasks
  • Keep a maximum of 3 concurrent sub-agents to avoid rate limits

If you’re hitting context limits with complex agent workflows, sub-agents are the solution.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments