- implementing handoffs between agents
- using handoffs and the prebuilt agent to build a custom multi-agent system
Handoffs
To set up communication between the agents in a multi-agent system you can use handoffs — a pattern where one agent hands off control to another. Handoffs allow you to specify:- destination: target agent to navigate to (e.g., name of the LangGraph node to go to)
- payload: information to pass to that agent (e.g., state update)
Create handoffs
To implement handoffs, you can returnCommand
objects from your agent nodes or tools:
- Access the state of the agent that is calling the handoff tool using the InjectedState annotation.
- The
Command
primitive allows specifying a state update and a node transition as a single operation, making it useful for implementing handoffs. - Name of the agent or node to hand off to.
- Take the agent’s messages and add them to the parent’s state as part of the handoff. The next agent will see the parent state.
- Indicate to LangGraph that we need to navigate to agent node in a parent multi-agent graph.
If you want to use tools that return
Command
, you can either use prebuilt create_react_agent
/ ToolNode
components, or implement your own tool-executing node that collects Command
objects returned by the tools and returns a list of them, e.g.:This handoff implementation assumes that:
- each agent receives overall message history (across all agents) in the multi-agent system as its input. If you want more control over agent inputs, see this section
- each agent outputs its internal messages history to the overall message history of the multi-agent system. If you want more control over how agent outputs are added, wrap the agent in a separate node function:
Control agent inputs
You can use theSend()
primitive to directly send data to the worker agents during the handoff. For example, you can request that the calling agent populate a task description for the next agent:
Send()
in handoffs.
Build a multi-agent system
You can use handoffs in any agents built with LangGraph. We recommend using the prebuilt agent orToolNode
, as they natively support handoffs tools returning Command
. Below is an example of how you can implement a multi-agent system for booking travel using handoffs:
Full example: Multi-agent system for booking travel
Full example: Multi-agent system for booking travel
- Access agent’s state
- The
Command
primitive allows specifying a state update and a node transition as a single operation, making it useful for implementing handoffs. - Name of the agent or node to hand off to.
- Take the agent’s messages and add them to the parent’s state as part of the handoff. The next agent will see the parent state.
- Indicate to LangGraph that we need to navigate to agent node in a parent multi-agent graph.
Multi-turn conversation
Users might want to engage in a multi-turn conversation with one or more agents. To build a system that can handle this, you can create a node that uses aninterrupt
to collect user input and routes back to the active agent.
The agents can then be implemented as nodes in a graph that executes agent steps and determines the next action:
- Wait for user input to continue the conversation, or
- Route to another agent (or back to itself, such as in a loop) via a handoff
Full example: multi-agent system for travel recommendations
Full example: multi-agent system for travel recommendations
In this example, we will build a team of travel assistant agents that can communicate with each other via handoffs.We will create 2 agents:Let’s test a multi turn conversation with this application.
- travel_advisor: can help with travel destination recommendations. Can ask hotel_advisor for help.
- hotel_advisor: can help with hotel recommendations. Can ask travel_advisor for help.
Prebuilt implementations
LangGraph comes with prebuilt implementations of two of the most popular multi-agent architectures:- supervisor — individual agents are coordinated by a central supervisor agent. The supervisor controls all communication flow and task delegation, making decisions about which agent to invoke based on the current context and task requirements. You can use
langgraph-supervisor
library to create a supervisor multi-agent systems. - swarm — agents dynamically hand off control to one another based on their specializations. The system remembers which agent was last active, ensuring that on subsequent interactions, the conversation resumes with that agent. You can use
langgraph-swarm
library to create a swarm multi-agent systems.