Welcome back! In the previous lesson, you learned how to define and run your first OpenAI agent using the Agents SDK in JavaScript. You also saw how to extract the agent's final output from the result object. In this lesson, we'll take a closer look at the result object that is returned after running an agent. Understanding the structure and properties of this object is key to building more advanced applications, debugging your agent's behavior, and making the most of the SDK's features.
By the end of this lesson, you'll know how to inspect and interpret the different properties of the result object, including the final output, the original input, the last agent that ran, and new items generated during the run. You'll also see how these properties can help you understand what happened during the agent's run and how to use this information in your own projects.
When you run an agent, you receive a RunResult object. This object provides detailed information about the agent's execution.
Some of the most relevant properties include:
finalOutput: The final output produced by the last agent that ran. Its type can vary depending on the agent's configuration — it may be a string or a more complex object if the agent specifies an output type.input: The original input or prompt provided to the agent at the start of the run.lastAgent: The agent instance that produced the final output. This is especially useful in workflows involving multiple agents or handoffs.newItems: An array of items generated during the run, such as messages, tool calls, or handoffs. These items provide a step-by-step record of the agent's reasoning and actions.history: The conversation history, including all messages exchanged during the run.
By inspecting these properties, you gain a comprehensive view of the agent's execution, including the input, output, and intermediate steps. This structure is designed to support both simple and advanced use cases, from basic logging to complex multi-agent workflows.
For the examples in this lesson, we'll use a simple travel assistant agent. Here's how you can define and run this agent in JavaScript:
This code creates an agent named "Travel Genie" and runs it with a sample query. The result of the run is stored in the result object, which we'll explore in detail in the following sections.
As a reminder from the previous lesson, the most important property in the result object is usually finalOutput. This is the agent's final answer after completing its reasoning, tool use, or any handoffs to other agents. You can access it directly with result.finalOutput.
For example, after running our Travel Genie agent:
You might see output like this:
The finalOutput property contains the complete, formatted response from the agent. In more complex scenarios, the result object may also include additional metadata, such as a finish reason, which explains why the agent stopped (for example, because it reached a final answer, hit a token limit, or encountered a content filter). This information can be useful for debugging or for handling special cases in your application.
Beyond the final output, the result object provides several other useful attributes. The input property of the result object preserves the exact prompt or question you provided to the agent at the start of the run. This is especially useful for logging, debugging, or tracing how the agent responded to specific user queries. For instance, after running your agent, you might want to confirm what input triggered the response:
If you had asked, "What's your top recommendation for adventure seekers?", the output would reflect that original prompt:
This makes it easy to correlate the agent's output with the user's request, which is essential for both transparency and troubleshooting.
In scenarios where multiple agents collaborate or hand off tasks, it's important to know which agent produced the final output. The lastAgent property provides this information, allowing you to identify the agent responsible for the answer. Since lastAgent is an instance of the Agent class, you can access its properties and methods directly. For example, you can access the agent's name:
Suppose your workflow involves a travel assistant agent named "Travel Genie." If this agent produced the final response, you would see:
This detail is particularly valuable in multi-agent systems, where understanding the flow of responsibility helps with both debugging and auditability.
As the agent processes your input, it may generate a sequence of intermediate items — such as messages, tool calls, or handoffs to other agents. These are collected in the newItems property, which provides a step-by-step record of the agent's reasoning and actions during the run. To review this sequence, you can iterate through the array:
You might observe output like the following, which shows a message output item generated by the "Travel Genie" agent:
By examining these items, you gain insight into the agent's internal process, making it easier to understand how the final answer was constructed and to diagnose any unexpected behavior.
The history property in the result object contains the conversation history, including all messages exchanged during the run. This can be useful for logging, debugging, or building conversational interfaces.
A sample output might look like:
Reviewing the conversation history helps you trace the flow of the interaction and understand how the agent arrived at its final output.
In this lesson, you learned how to inspect and interpret the different properties of the result object returned by the OpenAI Agents SDK in JavaScript. You saw how to access the final output, review the original input and last agent, examine the sequence of new items, and inspect the conversation history. Understanding these properties will help you debug your agents, optimize their behavior, and build more advanced applications.
In the next part of the course, you'll get hands-on practice with these concepts through interactive exercises. You'll have the chance to experiment with different agent configurations, inspect result properties, and deepen your understanding of how agents work under the hood. When you're ready, move on to the practice exercises to apply what you've learned!
