Introduction & Context

Welcome back! In the previous lesson, you learned how to maintain multi-turn conversations with a single OpenAI agent by managing and passing conversation history using the history property. This is a crucial skill for building context-aware applications. Today, we’ll 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 how to manage it in JavaScript. In the last lesson, you saw how important it is for an agent to remember previous messages in a conversation. By using the history property returned from the run() function, 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.

When chaining agents, the same idea applies. 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. In JavaScript, you can use the history array from the result of run(), and add new user messages using the user() function. This combination lets you capture and transfer 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 JavaScript:

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 run() function 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.finalOutput property gives you the agent’s answer, while the result.history array contains the full conversation history.

Extracting Data With history And Adding More Context

To pass the context from the first agent to the next, you use the result.history property. This property is an array 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 user message using the user() function.

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

This itineraryInput 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 array 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 using JavaScript. You saw how to define agents with clear roles, run them in sequence, use the history property to pass context from one agent to the next, and add new instructions for downstream agents with the user() function. 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 JavaScript code. In the next set of exercises, you’ll 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 JavaScript!

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