OpenAI Agents SDK: How to Hand Off Between Agents
Updated 2026-11-24
Handoffs in the OpenAI Agents SDK let a triage agent delegate to specialists by exposing other agents as tools (for example transfer_to_refund_agent). Use them when billing, technical, and FAQ agents need different instructions and tools but share one customer thread. Install basics first with OpenAI Agents SDK function tools, then add handoffs here. Last tested 2026-11-24.
Prerequisites
- Python 3.10+
pip install openai-agentsin an isolated virtualenv- API keys for your configured model provider (OpenAI by default)
Docs used:
Steps
1. Define specialist agents
from agents import Agent
billing = Agent(
name="Billing agent",
instructions="Answer billing and invoice questions only.",
)
technical = Agent(
name="Technical agent",
instructions="Answer product setup and bug questions only.",
)
Give each specialist only the tools it needs. Keep triage lightweight.
2. Attach handoffs on the triage agent
from agents import handoff
triage = Agent(
name="Triage",
instructions="Route billing issues to Billing and product bugs to Technical.",
handoffs=[billing, handoff(technical)],
)
Plain Agent instances work in handoffs. Wrap with handoff() when you need overrides.
The SDK exposes handoffs to the model as tools named like transfer_to_billing_agent (handoffs overview).
3. Run the workflow with Runner
from agents import Runner
result = Runner.run_sync(triage, "I was charged twice for last month.")
print(result.final_output)
Runner.run_sync executes tool calls and handoffs until a specialist returns a final answer or max_turns stops the run.
4. Customize a handoff with handoff()
def log_handoff(ctx):
print("Handoff invoked")
refund = Agent(name="Refund agent", instructions="Handle refunds with empathy.")
refund_handoff = handoff(
agent=refund,
on_handoff=log_handoff,
tool_name_override="escalate_refund",
tool_description_override="Escalate when the customer requests money back.",
)
triage = Agent(name="Triage", handoffs=[refund_handoff])
on_handoff runs when the model selects the handoff tool—useful for audit logs or prefetching CRM context.
5. Require structured metadata with input_type
from pydantic import BaseModel
class EscalationData(BaseModel):
reason: str
async def on_escalation(ctx, input_data: EscalationData):
print(f"Escalation reason: {input_data.reason}")
escalation = Agent(name="Escalation agent")
escalation_handoff = handoff(
agent=escalation,
on_handoff=on_escalation,
input_type=EscalationData,
)
The model must populate reason in the tool call JSON; the SDK validates it before on_handoff runs.
6. Trim history with an input_filter
Specialists receive full thread history by default. To drop tool noise before FAQ agents answer:
from agents.extensions import handoff_filters
faq = Agent(name="FAQ agent", instructions="Answer from the knowledge base.")
faq_handoff = handoff(
agent=faq,
input_filter=handoff_filters.remove_all_tools,
)
remove_all_tools strips structured tool items; redact sensitive text manually if summaries still include secrets (handoffs security note).
7. Add recommended handoff prompts
from agents.extensions.handoff_prompt import RECOMMENDED_PROMPT_PREFIX
billing = Agent(
name="Billing agent",
instructions=f"{RECOMMENDED_PROMPT_PREFIX}\nAnswer billing questions briefly.",
)
The prefix tells models when handoffs are available (recommended prompts).
8. Pair with support email tooling
Handoffs orchestrate which agent answers; Gmail tools still come from Build an AI agent that answers customer support emails or MCP.
Common errors
Handoff never triggers. Add RECOMMENDED_PROMPT_PREFIX, clarify triage instructions, and give specialists distinct handoff_description text on Agent.
MaxTurnsExceeded during handoff chains. Raise max_turns on Runner.run_sync or shrink the tool set per agent. See Fix: AI agent stuck in a loop.
Specialist sees unrelated tool calls. Apply input_filter=handoff_filters.remove_all_tools or a custom filter; remember filters do not redact text already copied into messages.
Wrong agent receives handoff arguments. input_type only annotates the handoff tool call—it does not change the specialist’s main user message.
FAQ
How is a handoff different from Agent.as_tool?
Handoffs transfer the conversation to another agent. as_tool lets one agent call another as a function without giving up the thread (handoffs vs tools).
Can I disable a handoff at runtime?
Yes. Pass is_enabled=False or a callable to handoff() when a feature flag is off.
Does the function-tools guide already cover handoffs?
OpenAI Agents SDK function tools shows a minimal example. This guide focuses on handoff(), input_type, filters, and production prompt patterns.
Where do MCP servers fit?
Attach MCP with mcp_servers=[...] on the specialist that needs external tools, as in the function-tools guide.
Related guides
- OpenAI Agents SDK function tools (install + Runner basics)
- Build an AI agent that answers customer support emails (Gmail drafts workflow)
- Connect an AI agent to an MCP server (hosted tools)
- Fix: AI agent stuck in a loop
- CrewAI: build an agent (multi-agent alternative)
- What is an AI agent?