Introduction & Lesson Overview

Welcome back! In the previous lesson, you learned the basics of the OpenAI Agents SDK in TypeScript. You discovered how agents differ from simple chat models, explored the agent loop, and practiced running agents. This foundation is essential, as it allows you to build agents that can reason, plan, and act in multiple steps using modern TypeScript code.

In this lesson, we'll take a closer look at what happens after you run an agent. Specifically, you'll learn how to interpret the results returned by the agent, understand the structure of the result object, and see how to use these results to chain agents together. By the end of this lesson, you'll be able to extract key information from an agent's run and use it to build more complex, multi-step workflows — such as having one agent generate a recipe and another agent write a blog post about it. This knowledge will prepare you for the hands-on practice exercises that follow.

Understanding The Result Object

When you run an agent using the OpenAI Agents SDK for TypeScript, the result is not just a simple string or message. Instead, the SDK returns a result object that contains detailed information about the agent's execution. This object is your window into what happened during the agent's run.

The most important properties of the result object are:

  • input: The original input you provided to the agent.
  • newItems: An array of events or actions that occurred during the run, such as tool calls, reasoning steps, or messages.
  • finalOutput: The last response produced by the agent. This is usually what you want to show to the user.
  • lastAgent: The last agent that was executed, which is useful if your workflow involves multiple agents.
  • history: An array representing the full conversation history, including all messages exchanged. This is especially useful for chaining agents or continuing a conversation, as it provides all the necessary context in the correct format.

These properties help you understand not just the answer, but also how the agent arrived at it. This is especially important when you want to chain agents together or keep track of the conversation's context.

Example: Creating a Recipe Agent

Before we look at the different properties of the result object, let's quickly revisit how you might define and run an agent that generates recipes. Suppose you want an agent that acts as a creative chef and provides detailed smoothie recipes. You can set this up as follows:

In this example, the recipeAgent is set up with clear instructions and a model. When you run the agent with a prompt like "Give me a recipe for a healthy smoothie.", you receive a result object. Next, let's look at each part of the result object in more detail, using simple code snippets to illustrate how they work.

The input Property

The input property holds the original prompt or message you sent to the agent. This is the starting point for the agent's reasoning and response. For example, if you ask for a smoothie recipe, the input will simply be your request:

When you print this property, you'll see exactly what was provided to the agent at the beginning of its run:

This is useful for logging, debugging, or when you want to confirm exactly what was asked of the agent.

The newItems Property

The newItems property is an array of all the events or actions that occurred during the agent's run. This includes messages generated by the agent, tool calls, and any intermediate reasoning steps. By inspecting newItems, you can see the agent's process and how it arrived at its answer.

The output reveals the internal steps and messages produced by the agent. For example:

This property is especially helpful for understanding the agent's reasoning, debugging, or building features that need to explain the agent's steps.

The finalOutput Property

The finalOutput property contains the agent's final answer or response. This is typically what you want to display to the user or use as the result of the agent's work. For a recipe agent, this would be the complete recipe text:

When you print this property, you get the main output from the agent, ready to be shown to the user or passed to another agent:

This is the main output you'll use in your application or pass to another agent.

The history Property for Chaining Agents

The history property provides the full conversation history as an array of messages. This is especially useful for chaining agents or continuing a conversation, as it provides all the necessary context in the correct format.

For example, you can inspect the history like this:

The output will look like:

When you want to pass context to another agent, you can use the history array and append new user messages as needed. This ensures that the next agent has access to the full context, leading to more accurate and relevant responses.

Chaining Agents Using Results

A powerful feature of the OpenAI Agents SDK is the ability to chain agents together, passing the output and context from one agent directly into another. This allows you to build multi-step workflows where each agent builds on the work of the previous one.

Let's look at a practical example. Suppose you have a recipeAgent that generates a smoothie recipe and a blogAgent that writes a blog post about that recipe. After running the first agent, you can use its history to provide full context to the second agent. Here's how you can do this in TypeScript:

In this workflow, after the recipeAgent generates a recipe, we use the history property to capture the full conversation context. We then append a new user message asking for a blog post about the recipe using the user() helper, which creates a properly formatted user message object for the agent. This combined input is passed to the blogAgent, ensuring it has all the information it needs to write a relevant and accurate blog post.

Viewing the Final Output (Blog Post)

After running the blogAgent, you can access the final blog post text using the finalOutput property:

This will output something like:

Viewing the Last Agent Executed

The lastAgent property contains information about the last agent that was executed during the run. This is particularly useful when working with multi-agent workflows or when you need to track which agent produced the final output.

This will show you the details of the agent that generated the blog post:

This is useful for debugging or for tracking which agent handled the final step in a multi-agent workflow.

Summary & Preparation For Practice

In this lesson, you learned how to interpret the results of an agent run using the result object in the OpenAI Agents SDK for TypeScript. You explored its key properties — input, newItems, finalOutput, lastAgent, and history — and saw how to use them to chain agents together in a multi-step workflow. By understanding how to extract and use these results, you are now ready to build more advanced applications that combine the strengths of multiple agents.

In the next set of practice exercises, you'll get hands-on experience working with agent results and chaining agents together. This will help you solidify your understanding and prepare you for even more powerful agentic workflows. Well done on reaching this point — let's continue building your skills!

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