Module praisonaiagents.handoff
The handoff module enables seamless task delegation between AI agents, allowing specialized agents to transfer control to other agents based on expertise or task requirements.Classes
Handoff
The main class that represents a handoff configuration for delegating tasks between agents.Parameters
agent: 'Agent'
- The target agent to hand off totool_name_override: Optional[str] = None
- Custom tool name (defaults totransfer_to_<agent_name>
)tool_description_override: Optional[str] = None
- Custom tool description for the handoffon_handoff: Optional[Callable] = None
- Callback function executed during handoffinput_type: Optional[type] = None
- Type annotation for structured input datainput_filter: Optional[Callable[[HandoffInputData], HandoffInputData]] = None
- Function to transform input before passing to target
Methods
to_tool_function(self, source_agent: 'Agent') → Callable
- Converts the handoff configuration into a callable tool function
HandoffInputData
A dataclass that standardises the data passed between agents during handoff.Fields
messages: List[Message]
- List of conversation historycontext: Dict[str, Any]
- Dictionary containing additional context (e.g., source agent name)
Functions
handoff
Factory function for creating Handoff instances.Usage Examples
Basic Handoff
Advanced Handoff with Callbacks
Handoff with Input Filtering
Structured Input Handoff
Callback Patterns
Theon_handoff
callback supports three patterns:
-
No parameters - Simple notification
-
One parameter - Receives source agent
-
Two parameters - Receives source agent and input data
Built-in Input Filters
Thehandoff_filters
module provides common filtering functions:
remove_all_tools
- Removes all tool calls from message historykeep_last_n_messages(n)
- Keeps only the last n messagesremove_system_messages
- Removes system messages from history
How It Works
-
Configuration Phase:
- Agents are created with a
handoffs
parameter containing target agents - The handoff module converts these into callable tool functions
- Tools are registered with the agent’s LLM for function calling
- Agents are created with a
-
Runtime Delegation:
- When an agent determines delegation is needed, it calls the handoff tool
- The handoff executes any configured callbacks
- Conversation history is collected and filtered
- The target agent receives the context and continues the conversation
- The response is returned to the original caller
-
Context Preservation:
- Full conversation history is maintained
- Source agent identification is preserved
- Custom context can be passed via structured inputs
Best Practices
- Design Clear Agent Boundaries - Each agent should have a specific expertise
- Use Descriptive Names - Tool names should clearly indicate the target agent’s role
- Implement Callbacks - Use callbacks for logging, monitoring, or custom logic
- Filter Appropriately - Remove unnecessary context to stay within token limits
- Test Handoff Chains - Ensure agents don’t create infinite delegation loops