Overview
The handoff module provides functionality for agents to delegate tasks to other agents dynamically. It includes theHandoff class for creating custom handoff tools and the handoff function for standard handoff operations.
Classes
Handoff
A subclass of Tool that provides handoff-specific functionality.
Parameters
The name of the handoff tool
Target agent name or Agent instance
Template for handoff instructions
Description of the handoff tool
Pydantic model for structured input
Callback function called during handoff
Methods
run()
message(str): Message to pass to the target agent
str: Handoff prompt with instructions
HandoffInputData
Pydantic model for structured handoff input when using input_model.
Functions
handoff()
Creates a handoff tool or executes a handoff operation.
Parameters
Target agent for handoff
Name of the handoff tool
Description of the handoff functionality
Template for handoff instructions
Model for structured input
Callback function for handoff events
Message for direct handoff execution
Returns
Handoff: When creating a tool (no message provided)str: When executing directly (message provided)
Usage Examples
Creating a handoff tool:prompt_with_handoff_instructions()
Generates a handoff prompt with specific instructions and context.
Parameters
Name of the target agent
Instruction template
Additional context or message
Returns
str: Formatted handoff prompt
Built-in Filters
handoff_filters()
Returns available handoff filter options.
Returns
List of available filters:"all"- Accept handoffs from any agent"none"- Reject all handoffs"self"- Only accept from same agent"other"- Accept from any agent except self"team:<name>"- Only accept from agents in specified team
Example
Constants
DEFAULT_TEMPLATE
Default template for handoff instructions:
DEFAULT_DESCRIPTION
Default description for handoff tools:
Integration with Agents
Using Handoffs in Agent Configuration
Callback Function Signature
Error Handling
The handoff system handles various error conditions:- Invalid agent name: Returns error message if target agent not found
- Circular handoffs: Can be prevented using handoff filters
- Callback errors: Logged and handoff continues if callback fails
Best Practices
- Use descriptive names for custom handoff tools
- Implement callbacks for logging and monitoring
- Set appropriate filters to prevent handoff loops
- Provide context in handoff messages
- Test handoff chains to ensure smooth transitions

