Overview
The Handoff Pattern creates clean boundaries between agent responsibilities. When one agent completes its part of a task, it explicitly transfers control—along with all relevant context—to the next agent.
Core Mechanics
Handoff Message
{
"from_agent": "research_agent",
"to_agent": "synthesis_agent",
"task_id": "task_123",
"context": {
"findings": [...],
"sources_consulted": [...],
"open_questions": [...]
},
"instructions": "Synthesize these findings into a summary"
}
Handoff vs. Agent-as-Tool
| Aspect | Handoff | Agent-as-Tool |
|---|---|---|
| Control | Full transfer | Temporary delegation |
| Return | May not return | Always returns to caller |
| Context | Explicit transfer | Passed as parameters |
| Ownership | New agent owns task | Original agent retains ownership |
Implementation
OpenAI Agents SDK
from openai_agents import Agent, handoff
research_agent = Agent(name="researcher")
writer_agent = Agent(name="writer")
@research_agent.on_complete
async def research_complete(result):
await handoff(
to=writer_agent,
context=result,
message="Write an article based on this research"
)
LangGraph
Uses Command(goto="next_agent") to transfer control between nodes in the state graph.
Context Transfer Strategies
Full Context
Pass entire conversation history. Safe but can hit token limits.
Summarized Context
Compress history into summary. Efficient but may lose details.
Structured Context
Extract key fields into schema. Explicit but requires schema design.
Reference Context
Pass IDs; receiving agent fetches what it needs. Efficient for large context.
Common Pitfalls
Context Drift
Each handoff risks losing important details. Mitigate with:
- Explicit context schemas
- Validation at handoff boundaries
- Summary verification
Infinite Loops
Agent A hands to B, B hands back to A. Prevent with:
- Maximum handoff depth limits
- Cycle detection
- Explicit termination conditions
Orphaned Tasks
Handoff fails and no agent owns the task. Require:
- Acknowledgment from receiving agent
- Timeout and escalation paths
- Dead letter queues
When to Use
Good fit:
- Clear sequential workflows
- Specialized agents with distinct responsibilities
- Audit trail requirements
Poor fit:
- Highly dynamic, unpredictable workflows
- Real-time collaborative tasks
- Simple single-agent scenarios