Subagents
A subagent is an agent that a parent agent can invoke. The parent delegates work via a tool, and the subagent executes autonomously before returning a result.How It Works
- Define a subagent with its own model, instructions, and tools
- Create a tool that calls it for the main agent to use
- Subagent runs independently with its own context window
- Return a result (optionally streaming progress to the UI)
- Control what the model sees using
toModelOutputto summarize
When to Use Subagents
Subagents add latency and complexity. Use them when the benefits outweigh the costs:| Use Subagents When | Avoid Subagents When |
|---|---|
| Tasks require exploring large amounts of tokens | Tasks are simple and focused |
| You need to parallelize independent research | Sequential processing suffices |
| Context would grow beyond model limits | Context stays manageable |
| You want to isolate tool access by capability | All tools can safely coexist |
Why Use Subagents?
Offloading Context-Heavy Tasks
Some tasks require exploring large amounts of information—reading files, searching codebases, or researching topics. Running these in the main agent consumes context quickly, making the agent less coherent over time. With subagents, you can:- Spin up a dedicated agent that uses hundreds of thousands of tokens
- Have it return only a focused summary (perhaps 1,000 tokens)
- Keep your main agent’s context clean and coherent
Parallelizing Independent Work
For tasks like exploring a codebase, you can spawn multiple subagents to research different areas simultaneously. Each returns a summary, and the main agent synthesizes the findings—without paying the context cost of all that exploration.Specialized Orchestration
A less common but valid pattern is using a main agent purely for orchestration, delegating to specialized subagents for different types of work. For example:- An exploration subagent with read-only tools for researching codebases
- A coding subagent with file editing tools
- An integration subagent with tools for a specific platform or API
Basic Subagent Without Streaming
The simplest subagent pattern requires no special machinery. Your main agent has a tool that calls another agent in itsexecute function:
Handling Cancellation
When the user cancels a request, theabortSignal propagates to the subagent. Always pass it through to ensure cleanup:
AbortError. The main agent’s tool execution fails, which stops the main loop.
To avoid errors about incomplete tool calls in subsequent messages, use convertToModelMessages with ignoreIncompleteToolCalls:
Streaming Subagent Progress
When you want to show incremental progress as the subagent works, use preliminary tool results. This pattern uses a generator function that yields partial updates to the UI.How Preliminary Tool Results Work
Change yourexecute function from a regular function to an async generator (async function*). Each yield sends a preliminary result to the frontend:
Building the Complete Message
Eachyield replaces the previous output entirely (it does not append). This means you need a way to accumulate the subagent’s response into a complete message that grows over time.
The readUIMessageStream utility handles this. It reads each chunk from the stream and builds an ever-growing UIMessage containing all parts received so far:
message is a complete UIMessage containing all the subagent’s parts up to that point (text, tool calls, and tool results). The frontend simply replaces its display with each new message.
Controlling What the Model Sees
Here’s where subagents become powerful for context management. The fullUIMessage with all the subagent’s work is stored in the message history and displayed in the UI. But you can control what the main agent’s model actually sees using toModelOutput.
How It Works
ThetoModelOutput function maps the tool’s output to the tokens sent to the model:
- Users see: The full subagent execution—every tool call, every intermediate step
- The model sees: Just the final summary text
Write Subagent Instructions for Summarization
FortoModelOutput to extract a useful summary, your subagent must produce one. Add explicit instructions like this:
toModelOutput with nothing useful to extract.
Rendering Subagents in the UI (with useChat)
To display streaming progress, check the tool part’sstate and preliminary flag.
Tool Part States
| State | Description |
|---|---|
input-streaming | Tool input being generated |
input-available | Tool ready to execute |
output-available | Tool produced output (check preliminary) |
output-error | Tool execution failed |
Detecting Streaming vs Complete
Type Safety for Subagent Output
Export types alongside your agents for use in UI components:Render Messages and Subagent Output
This example uses the types defined above to render both the main agent’s messages and the subagent’s streamed output:Caveats
No Tool Approvals in Subagents
Subagent tools cannot useneedsApproval. All tools must execute automatically without user confirmation.
Subagent Context is Isolated
Each subagent invocation starts with a fresh context window. This is one of the key benefits of subagents: they don’t inherit the accumulated context from the main agent, which is exactly what allows them to do heavy exploration without bloating the main conversation. If you need to give a subagent access to the conversation history, themessages are available in the tool’s execute function alongside abortSignal: