For translation, open lesson in new tab and use Chrome translate

Delegation

Lesson Video Walkthrough

The video and the text below cover the same material, so pick whichever format you prefer. If they ever differ, the text is the most up-to-date version.

Lesson Video Walkthrough (Python)

The video and the text below cover the same material, so pick whichever format you prefer. If they ever differ, the text is the most up-to-date version.

Long-running agents need coordination. A big task can span many steps, tool calls, files, and intermediate results. Without an explicit way to track the plan and isolate specialized work, the main agent can drift or overload its context.

Deep Agents gives the main agent two coordination tools:

A two-panel drawing. Left: a to-do list with checkboxes (two checked, one in progress, one pending) labeled Planning, opt-in via TodoListMiddleware. Right: a small robot supervisor connected to three robot subagents, labeled Subagents.

Planning and delegation solve different problems. Planning keeps the main agent organized. Delegation moves specialized or context-heavy work out of the main agent's context.


Planning: the to-do list

A capable model can draft a solid plan for a multi-step job. The hard part is follow-through. As the conversation grows and the agent juggles tool calls and intermediate results, it tends to drift, losing track of what's already done and what's still left.

Deep Agents addresses this with an opt-in write_todos tool: add TodoListMiddleware to the agent's middleware list and the model gets a tool for writing its plan out as a structured to-do list and updating each item's status as it works:

from langchain.agents.middleware import TodoListMiddleware
from deepagents import create_deep_agent

agent = create_deep_agent(
    tools=[...],
    middleware=[TodoListMiddleware()],
)

The list is saved in the agent's state. Across turns, it persists when you use the same thread with a checkpointer, following the thread pattern introduced earlier in the course. On a long task the agent always has an explicit, up-to-date plan to check against instead of trying to hold the whole thing in its head. Think of it as writing the steps on a whiteboard and ticking them off as it goes.

Planning is not delegation. It is the supervisor's task list: a way for the main agent to keep itself organized before, during, and after tool calls.


Delegation: how teams tackle a big project

When a project is too big for one person, we bring in a team.

How people tackle a big project A supervisor splits the work; specialists run in parallel Splits the project into tasks and combines the results Supervisor Finds the sources, facts, and background Research Drafts and edits the copy Writing Pulls the numbers and runs the analysis Data Handles the layout and visuals Design Hover over a teammate to see their role

A few things make this work:


What a subagent is

Optional: What are subagents? (4-minute video)

This video is optional. It's a quick visual explanation of subagents if you'd like one before reading on; the section below covers the same ideas.

A subagent is a full agent the main agent can call to do a focused task and report back. It can have its own instructions, tools, skills, model, and isolated context. The team pattern carries straight over.

A main agent robot at the top connected to four subagent robots below, each labeled with its own prompt, tools, model, and context.

Subagent-as-tool. Deep Agents exposes subagents through a built-in task tool. To the main agent, delegating looks just like calling any other tool. The main agent calls task with an assignment and gets a result back, picking which subagent to use from each one's description, the same way it decides to call any other tool.


Context isolation

Lesson Video Walkthrough

The video and the text below cover the same material, so pick whichever format you prefer. If they ever differ, the text is the most up-to-date version.

Lesson Video Walkthrough (Python)

The video and the text below cover the same material, so pick whichever format you prefer. If they ever differ, the text is the most up-to-date version.

On the left, a main agent with a long message history; one task message flows out to a subagent on the right, which has its own small fresh context and its own tools, and one result message flows back.

This is what makes subagents a context-engineering tool. A subagent does its work without cluttering the main agent's context:

The payoff: a large, messy subtask gets compressed into a single clean result, as long as the subagent is told to return a concise summary. The main agent's context stays focused on coordination, even as the work piles up behind the scenes.

The Devil Wears Prada Analogy

A still from The Devil Wears Prada showing Miranda Priestly and Andy Sachs sitting in the back of a car, labeled 'Model' over Miranda and 'Subagent' over Andy.

If you've seen The Devil Wears Prada, this is basically it: Miranda Priestly doesn't want to hear how Andy got something done, just the result. Subagent = Andy Sachs; the subagent does the messy work and hands the main agent only what it needs to know, not tool call noise or irrelevant details.

Miranda also doesn't wait on one assistant at a time: she sends several off at once, each on their own errand. A main agent can do the same, running multiple subagents in parallel, each in its own separate context.

Miranda Priestly, labeled 'Model', with arrows fanning out to three assistants labeled 'Subagents': Emily on the phone, Andy carrying a tray of coffees, and Nigel reviewing a layout, each handling a separate task at the same time.

New update: fork mode (experimental). By default, a subagent only sees the task description it's given. Set "mode": "fork" to see the parent's conversation so far and use the parent's system prompt instead.

reviewer = {
    "name": "reviewer",
    "description": "Review the plan we just discussed.",
    "mode": "fork",
}

Use it when the subagent needs context from the conversation that would be hard to write into a task description. The cost is a larger context for the subagent. A forked subagent can't set its own skills (it uses the parent's).

Added in deepagents 0.7.12. The labs don't use it.

New update: fork mode (experimental). By default, a subagent only sees the task description it's given. Set mode: "fork" to see the parent's conversation so far and use the parent's system prompt instead.

const reviewer: SubAgent = {
  name: "reviewer",
  description: "Review the plan we just discussed.",
  mode: "fork",
};

Use it when the subagent needs context from the conversation that would be hard to write into a task description. The cost is a larger context for the subagent. A forked subagent can't set its own skills (it uses the parent's).

The labs don't use it.


Synchronous vs. asynchronous

Subagents can run in two modes. The choice comes down to one question: does the main agent need the result before it can keep going?

Side-by-side timelines. Left, synchronous: the main agent is blocked and waiting while three subagents run, resuming only when the slowest finishes. Right, asynchronous: the main agent launches a background job, stays responsive, checks status, and collects the result later.

This course uses synchronous subagents in the hands-on lab. Async subagents are useful for production/background workflows where the main agent should stay responsive while longer work continues elsewhere.


Optional: Live Demo

Try it yourself: ask it to compare options or brief multiple topics, then watch the subagents run in parallel.

* This demo runs a create_deep_agent orchestrator with three subagents (researcher, analyzer, writer) invoked in parallel through task(), the same subagent-as-tool model described above.

For example, try: "Compare React, Vue, and Svelte for building a new team dashboard."

Watch:

  • The main agent spawns 2-3 subagents in a single turn, each with one narrow assignment.
  • Each subagent's card updates live as it completes.
  • The main agent waits for all of them (synchronous), then synthesizes one final answer.

When to reach for a subagent

Use a subagent when:

Skip the subagent when:


Recap

In this lesson you learned how planning and delegation support long-running agents:


Next

Complete the Quiz above, then continue to the next lesson where you'll build a subagent team: a main agent that fans out parallel searches to subagents and synthesizes their results into a weekly newsletter.


References

Documentation:

Blog:

Go deeper: read the source (optional)

The lesson shows how to use this. To see how it's implemented, start with these files:

Go deeper: read the source (optional)

The lesson shows how to use this. To see how it's implemented, start with these files:

Check your understanding

Question: You build a deep agent with just a model and a search tool, and no middleware argument. On a 12-step job, the trace never shows a write_todos call. What is the most likely reason?
A) write_todos needs a checkpointer, and is removed when none is passed
B) write_todos only appears after summarization compacts the history
C) write_todos is opt-in and needs TodoListMiddleware in the list
D) write_todos is only given to subagents, so planning must be delegated
Question: The main agent needs four independent research jobs done by synchronous subagents. What would you most likely see in the trace?
A) Four task calls in one AIMessage; the main agent resumes once all return
B) One task call carrying all four jobs, worked through by a single subagent
C) Four task calls over four turns, since only one can be pending at a time
D) Four start_async_task calls, and a reply before any subagent finishes
Question: A subagent reads 30 web pages and makes 40 tool calls before it finishes. What is added to the main agent's message history?
A) All 40 tool calls and results, appended after the task call
B) A summary of the 40 steps written by SummarizationMiddleware
C) One ToolMessage containing the subagent's final reply
D) Nothing, until the main agent reads the subagent's output file
Question: A user wants to keep chatting while a 20-minute research job runs. Which setup fits better, and why?
A) A sync subagent, since parallel task calls never block the user
B) A sync subagent with a checkpointer, so the pause can resume later
C) An async subagent, since it shares context and can reply directly
D) An async subagent: the main agent gets a task ID and stays free