Skip to main content

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 to
  • tool_name_override: Optional[str] = None - Custom tool name (defaults to transfer_to_<agent_name>)
  • tool_description_override: Optional[str] = None - Custom tool description for the handoff
  • on_handoff: Optional[Callable] = None - Callback function executed during handoff
  • input_type: Optional[type] = None - Type annotation for structured input data
  • input_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 history
  • context: 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

The on_handoff callback supports three patterns:
  1. No parameters - Simple notification
  2. One parameter - Receives source agent
  3. Two parameters - Receives source agent and input data

Built-in Input Filters

The handoff_filters module provides common filtering functions:
  • remove_all_tools - Removes all tool calls from message history
  • keep_last_n_messages(n) - Keeps only the last n messages
  • remove_system_messages - Removes system messages from history

How It Works

  1. 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
  2. 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
  3. Context Preservation:
    • Full conversation history is maintained
    • Source agent identification is preserved
    • Custom context can be passed via structured inputs

Best Practices

  1. Design Clear Agent Boundaries - Each agent should have a specific expertise
  2. Use Descriptive Names - Tool names should clearly indicate the target agent’s role
  3. Implement Callbacks - Use callbacks for logging, monitoring, or custom logic
  4. Filter Appropriately - Remove unnecessary context to stay within token limits
  5. Test Handoff Chains - Ensure agents don’t create infinite delegation loops