Skip to main content

Overview

The handoff module provides functionality for agents to delegate tasks to other agents dynamically. It includes the Handoff 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

name
str
required
The name of the handoff tool
agent
Union[str, Agent]
required
Target agent name or Agent instance
template
str
default:"DEFAULT_TEMPLATE"
Template for handoff instructions
description
str
default:"DEFAULT_DESCRIPTION"
Description of the handoff tool
input_model
Optional[Type[BaseModel]]
Pydantic model for structured input
callback
Optional[Callable]
Callback function called during handoff

Methods

run()
Executes the handoff operation. Parameters:
  • message (str): Message to pass to the target agent
Returns:
  • str: Handoff prompt with instructions
Example:

HandoffInputData

Pydantic model for structured handoff input when using input_model.

Functions

handoff()

Creates a handoff tool or executes a handoff operation.

Parameters

agent
Optional[Union[str, Agent]]
Target agent for handoff
name
str
default:"handoff"
Name of the handoff tool
description
str
Description of the handoff functionality
template
str
Template for handoff instructions
input_model
Optional[Type[BaseModel]]
Model for structured input
callback
Optional[Callable]
Callback function for handoff events
message
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:
Direct handoff execution:

prompt_with_handoff_instructions()

Generates a handoff prompt with specific instructions and context.

Parameters

agent
str
required
Name of the target agent
template
str
required
Instruction template
message
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

  1. Use descriptive names for custom handoff tools
  2. Implement callbacks for logging and monitoring
  3. Set appropriate filters to prevent handoff loops
  4. Provide context in handoff messages
  5. Test handoff chains to ensure smooth transitions

Complete Example