Welcome back! In the last lesson, you learned how to chain multiple OpenAI agents together, passing the output and conversation history from one agent to the next. This approach allowed you to break down complex tasks into smaller, manageable steps, with each agent playing a specific role. You saw how to use the to_input_list()
method to transfer context and how to build collaborative workflows by running agents in sequence.
In this unit, you will take the next step in coordinating agent workflows: delegating tasks dynamically using handoffs. Instead of always following a fixed sequence, you will learn how to set up agents that can route requests to the most appropriate specialist agent based on the user's needs. This lesson will show you how to configure the handoffs
parameter and use prompting strategies to guide agent behavior, making your multi-agent systems more flexible and intelligent.
In the OpenAI Agents SDK, a handoff is a way for one agent to delegate a task to another agent. Think of it as a transfer of responsibility: when an agent receives a request that is better handled by a specialist, it can "hand off" the task to that specialist agent. This is especially useful in situations where different agents have different areas of expertise.
For example, imagine a travel assistant system. You might have one agent that is great at recommending destinations and another that specializes in travel safety. With handoffs, you can create a triage agent that listens to the user's request and decides which specialist should handle it. This makes your system more modular, easier to maintain, and able to provide higher-quality responses by letting each agent focus on what it does best.
Handoffs are not just about splitting up work — they also help create a seamless user experience. The user interacts with a single system, but behind the scenes, their request is routed to the right expert. This delegation happens automatically, making your AI workflows smarter and more adaptable.
Before setting up delegation, start by defining your specialist agents. For example, you might have one agent focused on travel recommendations and another dedicated to travel safety advice:
Each specialist agent has clear instructions that focus on its area of expertise. This ensures that when a request is handed off, the agent can provide the most relevant and high-quality response.
With your specialist agents defined, you can now create a triage agent responsible for delegating incoming requests. The triage agent uses the handoffs
parameter to specify which agents it can delegate to:
The triage agent’s instructions clearly describe when to delegate to each specialist. When a user submits a request, the triage agent analyzes it and automatically routes it to the appropriate expert. This modular approach keeps your workflow organized and makes it easy to add or update specialist agents as your system grows.
When a handoff happens, the original agent (in this case, the triage agent) does not come back in the loop. The specialist agent that receives the handoff takes over and responds to the user. The full conversation history is passed along, so the new agent has all the necessary context and the user does not need to repeat themselves.
You might be wondering how the SDK actually makes it possible for one agent to delegate a task to another. Under the hood, when you specify the handoffs
parameter, the SDK represents each handoff as a special kind of "tool" that the language model can use to transfer control to another agent.
You don’t need to worry about the details of how tools work just yet—we’ll cover tools in depth in the next course. For now, it’s enough to know that for each agent you include in the handoffs
list, the SDK automatically creates a tool with a name like transfer_to_<agent_name>
. The agent’s name is converted to lowercase and spaces are replaced with underscores. For example:
- If you hand off to an agent named
Travel Genie
, the tool will be calledtransfer_to_travel_genie
. - If you hand off to an agent named
Travel Safety Expert
, the tool will be calledtransfer_to_travel_safety_expert
.
When the triage agent decides to delegate a request, it uses the appropriate transfer tool (for example, transfer_to_travel_genie
). This action hands off the conversation to the specialist agent, and the triage agent does not get control back. The specialist agent completes the interaction with the user, using the full conversation history for context.
Again, you don’t need to define these transfer tools yourself—the SDK takes care of it automatically when you use the handoffs
parameter. For now, just focus on how agents can delegate tasks to each other, and know that the SDK is handling the details behind the scenes.
When working with handoffs, it’s important to provide clear and specific instructions to each agent. The OpenAI Agents SDK offers a recommended prompt prefix to help agents understand their role in a multi-agent system and handle handoffs correctly.
The recommended prompt prefix is available as RECOMMENDED_PROMPT_PREFIX
in the SDK. This prefix provides essential system context for the agent. You can inspect its contents like this:
At the time this course is being developed. The recommended prompt prefix look like this:
To use this prefix, you can prepend it to your agent’s specific instructions. For example, when defining a triage agent:
This approach ensures the agent receives both the system context and its specific task instructions, which helps it delegate tasks better.
The SDK also provides a helper function, prompt_with_handoff_instructions
, which automatically combines your agent’s instructions with the recommended handoff context:
Using either approach ensures your agents are properly primed to handle handoffs, resulting in more reliable and context-aware delegation in your multi-agent workflows.
Let’s walk through an example to see how this works in practice. You have three agents: a triage agent, a travel recommendation agent, and a travel safety expert. When a user submits a request, the triage agent analyzes it and decides which specialist agent should handle the response.
When you run this code, the triage agent receives each user request and decides which specialist should handle it. For the first request, "Where should I go for a relaxing beach vacation?", the triage agent delegates to the Travel Genie. For the second request, "Is it safe to travel by airplane?", it delegates to the Travel Safety Expert.
The output might look like this:
Notice how the triage agent does not answer the questions itself. Instead, it routes each request to the appropriate specialist, and the user receives a helpful, context-aware response from the right expert.
In this lesson, you learned how to use the handoffs
parameter to delegate tasks between agents in the OpenAI Agents SDK. You saw how to define specialist agents, configure a triage agent to route requests, and use clear prompting strategies to guide agent behavior. By leveraging handoffs, you can build modular, flexible, and intelligent multi-agent systems that provide a seamless user experience.
You are now ready to practice configuring handoffs and writing effective prompts in your own code. In the next set of exercises, you will get hands-on experience building agent workflows that can dynamically delegate tasks. Keep up the great work — each lesson brings you closer to mastering the art of coordinating OpenAI agents in Python!
