Delegating Tasks Between Agents Using Handoffs

Introduction & Context

Welcome back! In the last lesson, you learned how to chain multiple OpenAI agents together in JavaScript, 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.

In this unit, you’ll take the next step in coordinating agent workflows: delegating tasks dynamically using handoffs. Instead of always following a fixed sequence, you’ll 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.

Understanding Handoffs

In the OpenAI Agents SDK for JavaScript, 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.

Defining Specialist Agents

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:

JavaScript
import { Agent } from '@openai/agents';

// Define the travel genie agent
const travelGenie = new Agent({
  name: 'Travel Genie',
  instructions:
    'You are Travel Genie, a friendly and knowledgeable travel assistant. ' +
    'Recommend exciting destinations and offer helpful travel tips.',
  model: 'gpt-5.6-terra',
  modelSettings: { reasoning: { effort: 'none' } }
});

// Define the travel safety expert agent
const safetyExpert = new Agent({
  name: 'Travel Safety Expert',
  instructions:
    'You are a travel safety expert. ' +
    'Provide safety advice and important precautions for travelers.',
  model: 'gpt-5.6-terra',
  modelSettings: { reasoning: { effort: 'none' } }
});

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.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal