- A single agent has too many tools and makes poor decisions about which to use.
- Context or memory grows too large for one agent to track effectively.
- Tasks require specialization (e.g., a planner, researcher, math expert).
Multi-agent patterns
Multi-agent systems enable you to build sophisticated applications by combining specialized agents. There are two main patterns:| Pattern | How it works | Control flow | Example use case |
|---|---|---|---|
| Tool calling | A supervisor agent calls other agents as tools. The “tool” agents don’t talk to the user directly — they just run their task and return results. | Centralized: all routing passes through the calling agent. | Task orchestration, structured workflows |
| Handoffs | The current agent decides to transfer control to another agent. The active agent changes, and the user may continue interacting directly with the new agent. | Decentralized: agents can change who is active. | Multi-domain conversations, specialist takeover |
Tool calling
In tool calling, one agent (the controller) treats other agents as tools to be invoked when needed. The controller manages orchestration, while tool agents perform specific tasks and return results. For example:- The controller receives input and decides which tool (sub-agent) to call.
- The tool agent runs its task based on the controller’s instructions.
- The tool agent returns results to the controller.
- The controller decides the next step or finishes.
When to use tool calling
Use tool calling (supervisor pattern) when:- You have multiple distinct domains (e.g., calendar, email, CRM, database).
- Each domain has multiple tools or complex logic.
- You want centralized workflow control.
- Sub-agents don’t need to converse directly with users.
-
Personal assistant: A main agent coordinates calendar, email, and task management sub-agents
- Calendar sub-agent handles scheduling and availability checks
- Email sub-agent drafts, sends, and searches messages
- Task sub-agent manages to-do lists and reminders
-
Research assistant: A supervisor orchestrates research across multiple domains
- Web search sub-agent finds relevant sources
- Document analysis sub-agent extracts key information
- Synthesis sub-agent combines findings into coherent reports
-
Data analysis platform: One agent routes queries to specialized analyzers
- SQL sub-agent queries databases
- Python sub-agent runs statistical analysis
- Visualization sub-agent creates charts and graphs
Tutorial: Build a supervisor agent
Learn how to build a personal assistant using the supervisor pattern, where a central supervisor agent coordinates specialized worker agents.
Example
The example below shows how a main agent is given access to a single sub-agent via a tool definition:- The main agent invokes
call_subagent1when it decides the task matches the sub-agent’s description. - The sub-agent runs independently and returns its result.
- The main agent receives the result and continues orchestration.
Where to customize
There are several points where you can control how context is passed between the main agent and its subagents:- Sub-agent name (
"subagent1_name"): This is how the main agent refers to the sub-agent. Since it influences prompting, choose it carefully. - Sub-agent description (
"subagent1_description"): This is what the main agent knows about the sub-agent. It directly shapes how the main agent decides when to call it. - Input to the sub-agent: You can customize this input to better shape how the sub-agent interprets tasks. In the example above, we pass the agent-generated
querydirectly. - Output from the sub-agent: This is the response passed back to the main agent. You can adjust what is returned to control how the main agent interprets results. In the example above, we return the final message text, but you could return additional state or metadata.
Control the input to the sub-agent
There are two main levers to control the input that the main agent passes to a sub-agent:- Modify the prompt: Adjust the main agent’s prompt or the tool metadata (i.e., sub-agent’s name and description) to better guide when and how it calls the sub-agent.
- Context injection: Add input that isn’t practical to capture in a static prompt (e.g., full message history, prior results, task metadata) by adjusting the tool call to pull from the agent’s state.
Control the output from the sub-agent
Two common strategies for shaping what the main agent receives back from a sub-agent:- Modify the prompt: Refine the sub-agent’s prompt to specify exactly what should be returned.
- Useful when outputs are incomplete, too verbose, or missing key details.
- A common failure mode is that the sub-agent performs tool calls or reasoning but does not include the results in its final message. Remind it that the controller (and user) only see the final output, so all relevant info must be included there.
- Custom output formatting: Adjust or enrich the sub-agent’s response in code before handing it back to the main agent.
- Example: pass specific state keys back to the main agent in addition to the final text.
- This requires wrapping the result in a
Command(or equivalent structure) so you can merge custom state with the sub-agent’s response.
Handoffs
In handoffs, agents pass control to each other through state transitions. Think of it as a state machine where the system tracks which agent is currently active, and that agent’s configuration (system prompt, tools, etc.) determines the behavior. For example:- The current agent decides it needs to transfer control to another agent.
- It updates a state variable (e.g.,
active_agent) using a tool that modifies the graph state. - On the next conversational turn, the graph reads this state and configures itself with the new agent’s system prompt, tools, and behavior.
- The new agent interacts directly with the user until it decides to hand off again or finish.
When to use handoffs
Handoffs are particularly valuable for “online” customer support scenarios where you need to collect information from users in a specific sequence. For example:- Collecting a warranty ID before processing a refund.
- Gathering account details before escalating to a specialist.
- Verifying identity before accessing sensitive information.
-
Customer support chatbot: Agents pass control based on conversation stage
- Triage agent collects initial information (warranty status, product details)
- Classification agent identifies issue type (billing, technical, returns)
- Resolution agent provides solutions or escalates to human support
-
Multi-domain conversational AI: Agents hand off when topic changes
- General conversation agent handles small talk
- Technical support agent answers product questions
- Sales agent discusses pricing and purchases
-
Onboarding assistant: Sequential information collection with branching paths
- Account setup agent creates credentials
- Preference collection agent gathers user preferences
- Tour agent walks through features based on user role
Tutorial: Build a customer support agent with handoffs
Learn how to build a customer support workflow using the handoffs pattern, where agents pass control through state transitions.
How handoffs work
At the core, handoffs rely on persistent state that survives across conversation turns:- State variable: A field in your state schema (e.g.,
active_agent: str) tracks which agent is currently active. - State update tool: The agent uses a tool to change the value of
active_agentwhen handing off control. - Dynamic configuration: On each turn, the graph entry point reads
active_agentfrom the persisted state and dynamically configures the appropriate system prompt, tools, and behavior for that agent.
Example
Here’s an example showing how agents hand off control:Command to update state fields that control which agent is active. Middleware can then read this state and dynamically configure the agent’s behavior.
Best practices
When implementing handoffs:- Bound message histories: Use
trim_messagesor@before_modelmiddleware to prevent unbounded growth. - State for cross-agent memory: Store structured data (warranty status, issue type) in
AgentStatecustom state, not message history. - Inject context summaries: Add state information to system prompts so agents know what previous agents learned.
- Validate state transitions: Check required state exists before allowing transitions to later stages.
- Allow backward navigation: Provide
@tools that returnCommandobjects for users to correct mistakes and return to previous stages. - Ensure valid message sequences: Make sure message histories follow provider requirements (user → AI → tool alternation).
- Use checkpointers: Always include a
InMemorySavercheckpointer to persist state across turns.
Customize agent context
At the center of multi-agent design is context engineering - deciding what information each agent sees. LangChain gives you fine-grained control over:- Which parts of the conversation or state are passed to each agent.
- Specialized prompts tailored to sub-agents.
- Inclusion/exclusion of intermediate reasoning.
- Customizing input/output formats per agent.