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.
SubAgentMiddleware(
self,
*,
backend: BackendProtocol | BackendFactory,
subagents: Sequence[SubAgent | CompiledSubAgent],
system_prompt: str | None = TASK_SYSTEM_PROMPT,
task_description: str | None = None
)Example:
from deepagents.middleware import SubAgentMiddleware
from langchain.agents import create_agent
agent = create_agent(
"openai:gpt-4o",
middleware=[
SubAgentMiddleware(
backend=my_backend,
subagents=[
{
"name": "researcher",
"description": "Research agent",
"system_prompt": "You are a researcher.",
"model": "openai:gpt-4o",
"tools": [search_tool],
}
],
)
],
)| 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 |
system_prompt | str | None | Default: TASK_SYSTEM_PROMPTInstructions appended to main agent's system prompt about how to use the task tool. |
task_description | str | None | Default: NoneCustom description for the task tool. |
| Name | Type |
|---|---|
| backend | BackendProtocol | BackendFactory |
| subagents | Sequence[SubAgent | CompiledSubAgent] |
| system_prompt | str | None |
| task_description | str | None |