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
str
required
The name of the handoff tool
Union[str, Agent]
required
Target agent name or Agent instance
str
default:"DEFAULT_TEMPLATE"
Template for handoff instructions
str
default:"DEFAULT_DESCRIPTION"
Description of the handoff tool
Optional[Type[BaseModel]]
Pydantic model for structured input
Optional[Callable]
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
Optional[Union[str, Agent]]
Target agent for handoff
str
default:"handoff"
Name of the handoff tool
str
Description of the handoff functionality
str
Template for handoff instructions
Optional[Type[BaseModel]]
Model for structured input
Optional[Callable]
Callback function for handoff events
str
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
str
required
Name of the target agent
str
required
Instruction template
str
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

