SubAgentMiddleware(
self,
*,
backend: BackendProtocol | BackendFactory,
subagents: Sequence[SubAgent| Name | Type | Description |
|---|---|---|
backend* | BackendProtocol | BackendFactory | Backend for file operations and execution. |
subagents* | Sequence[SubAgent | CompiledSubAgent] | List of fully-specified subagent configs. Each SubAgent must specify Optional |
system_prompt | str | None | Default: TASK_SYSTEM_PROMPT |
task_description | str | None | Default: None |
state_schema | type | None | Default: None |
| Name | Type |
|---|---|
| backend | BackendProtocol | BackendFactory |
| subagents | Sequence[SubAgent | CompiledSubAgent] |
| system_prompt | str | None |
| task_description | str | None |
| state_schema | type | None |
Middleware for providing subagents to an agent via a task tool.
This middleware adds a task tool to the agent that can be used
to invoke subagents.
Subagents are useful for handling complex tasks that require multiple steps, or tasks that require a lot of context to resolve.
A chief benefit of subagents is that they can handle multi-step tasks, and then return a clean, concise response to the main agent.
Subagents are also great for different domains of expertise that require a narrower subset of tools and focus.
Example:
from deepagents.middleware import SubAgentMiddleware
from langchain.agents import create_agent
agent = create_agent(
"openai:gpt-5.5",
middleware=[
SubAgentMiddleware(
backend=my_backend,
subagents=[
{
"name": "researcher",
"description": "Research agent",
"system_prompt": "You are a researcher.",
"model": "openai:gpt-5.5",
"tools": [search_tool],
}
],
)
],
)Instructions appended to main agent's system prompt about how to use the task tool.
Custom description for the task tool.
Base graph state schema forwarded to declarative
SubAgent specs when their runnables are compiled.
Leave unset to use create_agent's default. CompiledSubAgent
entries are unaffected — callers own those runnables' schemas.