Chaining Agents for Sequential Workflows

Introduction & Context

Welcome back! In the previous lesson, you learned how to maintain multi-turn conversations with a single OpenAI agent by preserving and passing conversation history. This is a crucial skill for building context-aware applications. Today, we will take the next step: coordinating multiple agents to work together in a sequence, where the output of one agent becomes the input for the next. This approach is called chaining agents.

Chaining agents is a powerful technique for breaking down complex tasks into smaller, manageable steps. Instead of asking one agent to do everything, you can assign each agent a specific role. For example, one agent might recommend a travel destination, while another creates a detailed itinerary based on that recommendation. This step-by-step decomposition not only makes your code more modular and easier to maintain, but it also allows you to build more sophisticated and collaborative AI workflows.

Recap Of Key Concepts

Before we dive into chaining, let’s quickly review what you learned about conversation history and the to_input_list() method. In the last lesson, you saw how important it is for an agent to remember previous messages in a conversation. By using the to_input_list() method, you can collect the entire dialogue so far and pass it back to the agent for each new turn. This ensures the agent always has the full context.

The same idea applies when chaining agents. Instead of just passing conversation history to the same agent, you can pass it to a different agent. This allows multiple agents to collaborate on a task, each building on the work of the previous one. Remember, the to_input_list() method is your tool for capturing and transferring the full context between agents.

Defining Agents For Sequential Tasks

To chain agents effectively, you need to define each agent with a clear and specific role. This means giving each agent a unique name and a set of instructions that describe what it should do. For example, you might have a Travel Genie agent whose job is to suggest exciting destinations, and an Itinerary Writer agent that creates a travel plan based on the chosen destination.

Here’s how you might define these two agents in code:

import asyncio
from agents import Agent, Runner, ModelSettings

# Define the first agent (Travel Genie)
travel_genie = 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",
    model_settings=ModelSettings(reasoning={"effort": "none"}),
)

# Define the second agent (Itinerary Writer)
itinerary_writer = Agent(
    name="Itinerary Writer",
    instructions=(
        "You are an expert travel itinerary writer. "
        "Given a destination and user interests, create a concise itinerary."
    ),
    model="gpt-5.6-luna",
    model_settings=ModelSettings(reasoning={"effort": "medium"}),
)

async def main():
    # Run the agents...

if __name__ == "__main__":
    asyncio.run(main())

Notice how each agent has a clear purpose and a unique name. This makes it easy to understand what each agent is responsible for and helps you organize your workflow.

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