|
| 1 | +# Agent Handoffs |
| 2 | + |
| 3 | +Agent handoffs allow agents to delegate tasks to other specialized agents. This is useful when you have agents with different areas of expertise that need to collaborate. |
| 4 | + |
| 5 | +## Basic Usage |
| 6 | + |
| 7 | +```python |
| 8 | +from praisonaiagents import Agent, handoff |
| 9 | + |
| 10 | +# Create specialized agents |
| 11 | +billing_agent = Agent(name="Billing Agent", role="Billing Specialist") |
| 12 | +refund_agent = Agent(name="Refund Agent", role="Refund Specialist") |
| 13 | + |
| 14 | +# Create main agent with handoffs |
| 15 | +triage_agent = Agent( |
| 16 | + name="Triage Agent", |
| 17 | + role="Customer Service", |
| 18 | + handoffs=[billing_agent, refund_agent] # Can hand off to these agents |
| 19 | +) |
| 20 | +``` |
| 21 | + |
| 22 | +## How Handoffs Work |
| 23 | + |
| 24 | +1. Handoffs are automatically converted to tools that the agent can use |
| 25 | +2. The agent decides when to hand off based on the conversation context |
| 26 | +3. When a handoff occurs, the target agent receives the conversation history |
| 27 | +4. The target agent's response is returned to the user |
| 28 | + |
| 29 | +## Advanced Features |
| 30 | + |
| 31 | +### Custom Handoff Configuration |
| 32 | + |
| 33 | +Use the `handoff()` function for more control: |
| 34 | + |
| 35 | +```python |
| 36 | +from praisonaiagents import Agent, handoff |
| 37 | + |
| 38 | +agent = Agent(name="Target Agent") |
| 39 | + |
| 40 | +custom_handoff = handoff( |
| 41 | + agent=agent, |
| 42 | + tool_name_override="escalate_to_specialist", |
| 43 | + tool_description_override="Escalate complex issues to a specialist", |
| 44 | + on_handoff=lambda ctx: print(f"Handoff from {ctx.name}"), |
| 45 | + input_filter=handoff_filters.remove_all_tools |
| 46 | +) |
| 47 | + |
| 48 | +main_agent = Agent( |
| 49 | + name="Main Agent", |
| 50 | + handoffs=[custom_handoff] |
| 51 | +) |
| 52 | +``` |
| 53 | + |
| 54 | +### Handoff Callbacks |
| 55 | + |
| 56 | +Execute custom logic when a handoff occurs: |
| 57 | + |
| 58 | +```python |
| 59 | +# Create target agent |
| 60 | +target_agent = Agent(name="Target Agent", role="Specialist") |
| 61 | + |
| 62 | +def log_handoff(source_agent): |
| 63 | + print(f"Handoff initiated from {source_agent.name}") |
| 64 | + |
| 65 | +handoff_with_callback = handoff( |
| 66 | + target_agent, |
| 67 | + on_handoff=log_handoff |
| 68 | +) |
| 69 | +``` |
| 70 | + |
| 71 | +### Structured Input |
| 72 | + |
| 73 | +Require specific data when handing off: |
| 74 | + |
| 75 | +```python |
| 76 | +from pydantic import BaseModel |
| 77 | + |
| 78 | +class EscalationData(BaseModel): |
| 79 | + reason: str |
| 80 | + priority: str |
| 81 | + |
| 82 | +# Create escalation agent |
| 83 | +escalation_agent = Agent(name="Escalation Agent", role="Senior Manager") |
| 84 | + |
| 85 | +def handle_escalation(source_agent, data: EscalationData): |
| 86 | + print(f"Escalation: {data.reason} (Priority: {data.priority})") |
| 87 | + |
| 88 | +escalation_handoff = handoff( |
| 89 | + escalation_agent, |
| 90 | + on_handoff=handle_escalation, |
| 91 | + input_type=EscalationData |
| 92 | +) |
| 93 | +``` |
| 94 | + |
| 95 | +### Input Filters |
| 96 | + |
| 97 | +Control what conversation history is passed to the target agent: |
| 98 | + |
| 99 | +```python |
| 100 | +from praisonaiagents import handoff_filters |
| 101 | + |
| 102 | +# Create target agent for filtering examples |
| 103 | +agent = Agent(name="Target Agent", role="Specialist") |
| 104 | + |
| 105 | +# Remove all tool calls from history |
| 106 | +filtered_handoff = handoff( |
| 107 | + agent, |
| 108 | + input_filter=handoff_filters.remove_all_tools |
| 109 | +) |
| 110 | + |
| 111 | +# Keep only last N messages |
| 112 | +limited_handoff = handoff( |
| 113 | + agent, |
| 114 | + input_filter=handoff_filters.keep_last_n_messages(5) |
| 115 | +) |
| 116 | + |
| 117 | +# Remove system messages |
| 118 | +clean_handoff = handoff( |
| 119 | + agent, |
| 120 | + input_filter=handoff_filters.remove_system_messages |
| 121 | +) |
| 122 | +``` |
| 123 | + |
| 124 | +## Recommended Prompts |
| 125 | + |
| 126 | +Include handoff instructions in your agent prompts: |
| 127 | + |
| 128 | +```python |
| 129 | +from praisonaiagents import RECOMMENDED_PROMPT_PREFIX, prompt_with_handoff_instructions |
| 130 | + |
| 131 | +# Create specialized agents |
| 132 | +billing_agent = Agent(name="Billing Agent", role="Billing Specialist") |
| 133 | +technical_agent = Agent(name="Technical Agent", role="Technical Support") |
| 134 | + |
| 135 | +agent = Agent( |
| 136 | + name="Support Agent", |
| 137 | + handoffs=[billing_agent, technical_agent] |
| 138 | +) |
| 139 | + |
| 140 | +# After creating the agent, update its instructions |
| 141 | +agent.instructions = prompt_with_handoff_instructions( |
| 142 | + "Help customers and transfer to specialists when needed.", |
| 143 | + agent # Pass the agent to auto-generate handoff info |
| 144 | +) |
| 145 | +``` |
| 146 | + |
| 147 | +## Complete Example |
| 148 | + |
| 149 | +See the examples directory for complete working examples: |
| 150 | +- `examples/handoff_basic.py` - Simple handoff demonstration |
| 151 | +- `examples/handoff_advanced.py` - Advanced features with callbacks and filters |
| 152 | +- `examples/handoff_customer_service.py` - Real-world customer service workflow |
| 153 | + |
| 154 | +## Best Practices |
| 155 | + |
| 156 | +1. **Clear Role Definition**: Give each agent a clear role and area of expertise |
| 157 | +2. **Handoff Instructions**: Include when to hand off in agent instructions |
| 158 | +3. **Context Preservation**: Use input filters carefully to maintain necessary context |
| 159 | +4. **Logging**: Use callbacks to track handoffs for debugging and analytics |
| 160 | +5. **Testing**: Test handoff paths to ensure smooth transitions |
| 161 | + |
| 162 | +## Backward Compatibility |
| 163 | + |
| 164 | +The handoff feature is fully backward compatible: |
| 165 | +- Existing agents work without modification |
| 166 | +- The `handoffs` parameter is optional |
| 167 | +- All existing agent functionality is preserved |
0 commit comments