Agent Handoff System
The handoff system enables seamless task delegation between specialized agents, allowing complex workflows where each agent contributes their expertise.Overview
When an agent encounters a task outside its expertise, it can dynamically hand off the conversation to a more suitable agent, ensuring optimal task completion.Quick Start
How Handoffs Work
1. Handoff Detection
Agents identify when they need to delegate based on:- Task complexity beyond their expertise
- Specific keywords or patterns in user requests
- Explicit handoff instructions in their role
2. Target Selection
The system selects the best agent using:- Predefined handoff targets in agent configuration
- Dynamic selection based on agent capabilities
- Custom selection logic via callbacks
3. Context Transfer
When handing off, the system:- Packages the entire conversation history
- Includes any relevant metadata
- Maintains user context and preferences
4. Seamless Continuation
The receiving agent:- Gets full context of the conversation
- Continues naturally without user intervention
- Can hand back or to another agent if needed
Basic Usage
Handoff Configuration
Parameters
| Parameter | Type | Description | Default |
|---|---|---|---|
agent | Agent | Target agent for handoff | Required |
tool_name_override | str | Custom name for handoff tool | transfer_to_<agent_name> |
tool_description_override | str | Custom description | Auto-generated |
on_handoff | Callable | Callback function | None |
input_filter | Callable | Filter for conversation data | None |
Handoff Filters
Control what conversation data is passed during handoff:Agent-Level Filters
Advanced Usage
Custom Handoff Names
Handoff Callbacks
Structured Input with Pydantic
Complete Example
Common Use Cases
Customer Support System
Multi-Department Routing
Language-Based Routing
Best Practices
Clear Handoff Criteria
Define explicit criteria for when agents should handoff:
- Specific keywords or phrases
- Task complexity thresholds
- User requests for specialists
- Error or uncertainty conditions
Context Preservation
Ensure important context is maintained:
- Include conversation summary in handoffs
- Pass relevant user preferences
- Maintain task state and progress
- Include any gathered information
Prevent Loops
Avoid infinite handoff loops:
- Implement handoff history tracking
- Set maximum handoff limits
- Use handoff filters appropriately
- Design clear agent responsibilities
User Experience
Make handoffs seamless for users:
- Inform users about handoffs when appropriate
- Maintain conversation continuity
- Preserve user context and preferences
- Minimize repetition of information
Troubleshooting
API Reference
See the Handoff API documentation for detailed information about:- The
Handoffclass - The
handoff()function HandoffInputDatastructure- Built-in handoff filters
- Callback signatures

