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:

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.

Running The First Agent And Retrieving Its Output

Once your agents are defined, you can start the workflow by running the first agent. You use the Runner.run() method to execute the agent with an initial input. The result object returned contains both the agent’s final output and the full conversation history.

Here’s how you might run the Travel Genie agent:

When you run this code, you might see output like:

The result.final_output property gives you the agent’s answer, while the result object itself contains the full conversation history.

Extracting Data With to_input_list() And Adding More Context

To pass the context from the first agent to the next, you use the to_input_list() method. This method returns a list of all messages exchanged so far, including the original input and the agent’s response. You can also add additional instructions for the next agent by appending a new message to this list.

For example, after running the Travel Genie agent, you can prepare the input for the next agent like this:

This itinerary_input variable now contains the full conversation so far, plus a new user request for a 3-day itinerary, ready to be used as input for the next agent.

Running The Second Agent With Chained Input

Now you can run the second agent, passing in the input list you just prepared. This allows the Itinerary Writer agent to see both the original user request, the recommendation from the Travel Genie, and the new instruction to write a 3-day itinerary. As a result, the itinerary it creates will be relevant and well-informed.

Here’s how you run the second agent:

The output might look like this:

By chaining the agents in this way, you create a smooth workflow where each agent builds on the work of the previous one and any additional user instructions.

Summary & Next Steps

In this lesson, you learned how to chain multiple OpenAI agents together to solve tasks step by step. You saw how to define agents with clear roles, run them in sequence, use the to_input_list() method to pass context from one agent to the next, and add new instructions for downstream agents. This approach allows you to break down complex problems into manageable parts and build collaborative, modular AI workflows.

You are now ready to practice chaining agents in your own code. In the next set of exercises, you will get hands-on experience building and running multi-agent workflows. Keep up the great work — each lesson brings you closer to mastering the art of coordinating OpenAI agents in Python!

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