Handoffs are the essential mechanism that controls how agents interact and pass control to each other in a group chat. If tools represent what agents can do and context variables represent what they know, handoffs determine where they go next.Documentation Index
Fetch the complete documentation index at: https://private-04b27de1.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Introduction to Handoffs
Handoffs define the paths a conversation can take through your multi-agent system. They allow you to create sophisticated workflows where the right agent handles each part of a conversation at the right time.Why Handoffs Matter
In any multi-agent system, you need to carefully control which agent speaks when. Handoffs provide this control by:- Directing the conversation to the most appropriate agent for each situation
- Creating predictable workflows with clear transitions
- Enabling complex decision trees based on conversation state
- Allowing dynamic adaptation to user inputs
The Hospital Triage Analogy
Extending our hospital emergency room analogy, when a patient enters the ER, they first see a triage nurse who assesses their condition. The triage nurse then makes a critical decision:- For cardiac symptoms, transfer immediately to the cardiologist
- For respiratory issues, send to the pulmonologist
- For minor injuries, direct to the general practitioner
- For severe trauma, rush to the trauma surgeon
- When they need to transfer a patient
- Who they should transfer to
- What information must be included in the handoff
Core Handoff Concepts
Each agent in AG2 has ahandoffs attribute that manages transitions from that agent. The handoffs attribute is an instance of the Handoffs class, which provides methods for defining when and where control should pass:
Transition Targets
When defining a handoff, you specify a transition target - the destination where control should go. AG2 provides several types of transition targets:AgentNameTarget: Transfer control to an agent by nameAgentTarget: Transfer control to a specific agent instanceAskUserTarget: Ask the user to select the next speakerNestedChatTarget: Target that represents a nested chat configurationGroupManagerTarget: Transfer control to the group manager who will select the next speakerGroupChatTarget: Transfer control to another group chat (essentially a nested group chat)RandomAgentTarget: Randomly select from a list of agentsRevertToUserTarget: Return control to the user agentStayTarget: Keep control with the current agentTerminateTarget: End the conversation
ReplyResults and Transitions
Each tool function can return aReplyResult that specifies a transition target:
Types of Handoffs
AG2 offers four main ways to define handoffs:- LLM-based conditions: Transitions based on the language model’s analysis of messages
- Context-based conditions: Transitions based on values in context variables
- After-work behavior: Default transition when no LLM or context conditions are met and no tools are called
- Explicit handoffs from tools: Direct transitions specified by tool return values
LLM-Based Conditions
LLM-based conditions use the language model to determine when a transition should occur. This is useful when the decision depends on understanding the meaning of messages:triage_agent uses the LLM to evaluate each condition prompt against the messages in the conversation. If it determines that the user query is about a technical issue, control passes to the tech agent.
Context-Based Conditions
Context-based conditions transition based on the values of context variables. This is ideal for decisions that depend on the system’s state rather than message content:issue_severity context variable is 8 or higher.
After-Work Behavior
After-work behavior defines the default transition that occurs after an agent completes its work and no specific condition is met:Explicit Handoffs from Tools
Tools can specify transitions directly in their return values:Configuring Handoffs for Agents
Let’s build a tech support system to demonstrate different types of handoffs and how they work together to create a seamless user experience.Enhancing our Support System with Handoffs
We will enhance our support system with multiple specialized agents that handle different aspects of customer service:- A triage agent that analyzes initial queries
- A general support agent for non-technical questions
- Specialists for different device types (computers and smartphones)
- An advanced troubleshooting agent for persistent issues
Setting Up the Initial Structure
First, let’s create our context variables and agents:Creating Tool Functions
Now let’s define the tool functions that enable our agents to make routing decisions and update the conversation state:Configuring LLM-Based Handoffs
Our first type of handoff uses the LLM to analyze messages and route based on content. The tech agent uses this to route device-specific issues:Configuring Context-Based Handoffs
Context-based handoffs make decisions based on the state of the conversation rather than message content. We use these to detect when a customer returns with the same issue:repeat_issue is set to True in the context variables. When a user says their issue wasn’t solved, the check_repeat_issue tool sets this flag, and the handoff condition triggers a transfer to advanced troubleshooting.
Setting Default After-Work Behavior
Finally, we set the default behavior for when agents complete their work without a specific handoff triggering:Putting It All Together
Now we can set up the conversation pattern and run the chat:- Route based on message content (LLM-based handoffs)
- Route based on conversation state (context-based handoffs)
- Provide fallback behaviors when no conditions are met (after-work)
- Use tools to update context and influence routing
Complete Example
???+ info “Complete Code Example”Example Output
If you run the complete example, you should see a conversation flow similar to this:Next Steps: Putting Everything Together with Patterns
You’ve already seenAutoPattern in action in this example, where it allows the system to dynamically select agents based on conversation context. In the next section, you’ll explore the full range of orchestration patterns available in AG2.
In the upcoming “Patterns” section, you’ll discover:
- How different pattern types (DefaultPattern, RoundRobinPattern, RandomPattern, etc.) offer alternative approaches to agent selection and conversation flow
- When to choose each pattern type based on your specific use case requirements
- How to customize patterns with additional configuration options
- Advanced techniques for combining patterns with handoffs, context variables, and tools