Master agent-to-agent task delegation! This tutorial teaches you how to use the OpenAI Agents SDK's handoff system to create specialized agents that can intelligently delegate tasks to each other, building powerful multi-agent workflows.
Sign in to save downloads to your library and vote.
Preview
๐ค Tutorial 8: Handoffs & Delegation
Master agent-to-agent task delegation! This tutorial teaches you how to use the OpenAI Agents SDK's handoff system to create specialized agents that can intelligently delegate tasks to each other, building powerful multi-agent workflows.
๐ฏ What You'll Learn
- Agent Handoffs: Delegating tasks between specialized agents
- Handoff Configuration: Custom tool names, descriptions, and callbacks
- Input Filtering: Controlling what context gets passed between agents
- Triage Patterns: Building intelligent routing and delegation systems
๐ง Core Concept: What Are Handoffs?
Handoffs enable agent specialization and delegation where agents can transfer tasks to other agents with specific expertise. Think of handoffs as a smart routing system that:
- Creates specialized agents for different domains (support, billing, technical)
- Allows intelligent task delegation based on user needs
- Maintains conversation context across agent transfers
- Provides custom routing logic and input filtering
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ HANDOFF WORKFLOW โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ USER REQUEST โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโ 1. ANALYZE REQUEST โ
โ โ TRIAGE โ โ
โ โ AGENT โ 2. DECIDE DELEGATION โ
โ โโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโ 3. CALL HANDOFF TOOL โ
โ โ HANDOFF โ "transfer_to_billing_agent" โ
โ โ TOOL โ โ
โ โโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโ 4. TRANSFER CONTEXT โ
โ โ BILLING โ (with optional filtering) โ
โ โ AGENT โ โ
โ โโโโโโโโโโโโโโโ 5. PROCESS REQUEST โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโ 6. RETURN RESPONSE โ
โ โ RESPONSE โ โ
โ โ TO USER โ โ
โ โโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ Tutorial Overview
This tutorial demonstrates key handoff patterns:
1. Basic Handoffs (basic_handoffs.py)
- Simple agent-to-agent delegation
- Customer support triage example
- Automatic tool creation from handoff definitions
2. Advanced Handoffs (advanced_handoffs.py)
- Custom handoff configuration with callbacks
- Input filtering and context management
- Handoff with structured input data
๐ Project Structure
8_handoffs_delegation/
โโโ README.md # This file - concept explanation
โโโ requirements.txt # Dependencies
โโโ basic_handoffs.py # Simple agent handoffs (40 lines)
โโโ advanced_handoffs.py # Advanced handoff patterns (50 lines)
โโโ app.py # Streamlit handoff demo (optional)
โโโ env.example # Environment variables template
๐ฏ Learning Objectives
By the end of this tutorial, you'll understand:
- โ How to create agent handoffs for task delegation
- โ Configuring handoff tools with custom names and descriptions
- โ Using input filters to control context transfer
- โ Building intelligent triage systems with multiple agents
- โ When and how to use handoffs vs direct agent orchestration
๐ Getting Started
-
Install OpenAI Agents SDK:
pip install openai-agents -
Install dependencies:
pip install -r requirements.txt -
Set up environment variables:
cp env.example .env # Edit .env and add your OpenAI API key -
Test basic handoffs:
python basic_handoffs.py -
Try advanced patterns:
python advanced_handoffs.py
๐ง Key Handoff Patterns
1. Basic Handoff Setup
from agents import Agent, handoff
billing_agent = Agent(name="Billing Agent")
support_agent = Agent(name="Support Agent")
triage_agent = Agent(
name="Triage Agent",
handoffs=[billing_agent, support_agent] # Creates tools automatically
)
2. Custom Handoff Configuration
from agents import Agent, handoff
def on_handoff_callback(ctx):
print(f"Handoff to {ctx.agent.name} initiated")
custom_handoff = handoff(
agent=billing_agent,
tool_name_override="escalate_to_billing",
tool_description_override="Transfer complex billing issues",
on_handoff=on_handoff_callback
)
3. Input Filtering
from agents.extensions import handoff_filters
filtered_handoff = handoff(
agent=support_agent,
input_filter=handoff_filters.remove_all_tools # Clean context
)
๐ก Handoff Design Best Practices
- Clear Specialization: Each agent should have a distinct area of expertise
- Intelligent Routing: Use descriptive tool names and instructions for LLM
- Context Management: Consider what context should transfer between agents
- Callback Integration: Use callbacks for logging, metrics, and workflows
- Input Validation: Structure inputs when passing specific data
๐ Next Steps
After completing this tutorial, you'll be ready for:
- Tutorial 9: Multi-Agent Orchestration - Complex multi-agent workflows with parallel execution
- Tutorial 10: Tracing & Observability - Monitoring and debugging
- Tutorial 11: Production Patterns - Real-world deployment strategies
๐ก Pro Tips
- Start Simple: Begin with basic handoffs, add complexity gradually
- Clear Instructions: Make agent roles and handoff triggers obvious
- Test Routing: Verify LLM chooses correct agents for different scenarios
- Monitor Handoffs: Use callbacks and tracing to track delegation patterns
- Context Strategy: Plan what information should transfer between agents
Ingestion metadata
- Source catalog
- awesome-llm-apps
- Repository
- Shubhamsaboo/awesome-llm-apps ยท main
- File path
- ai_agent_framework_crash_course/openai_sdk_crash_course/8_handoffs_delegation/README.md
- Last refreshed
- 7/24/2026, 3:00:13 AM (53m ago)
- Refresh schedule
- Daily ยท 03:00 UTC
- Dedupe status
- Unique ยท deduped by (source, url)