Introduction & Lesson Overview

Welcome back! In the last lesson, you explored the details of the agent result object and learned how to chain agents together. You saw how to extract important information from an agent’s run and how to use the history property to pass context between agents. This knowledge is essential for building more advanced workflows.

In this lesson, we’ll focus on a key feature of interactive agents: multi-turn conversations. Many real-world applications — such as chatbots, virtual assistants, and customer support agents — require the ability to remember and build upon previous exchanges. This is what makes interactions feel natural and coherent. By the end of this lesson, you’ll know how to manage and update conversation history using the history property, enabling your agents to handle seamless, multi-turn interactions in TypeScript.

Recap of Agent Results and Conversation History

Let’s briefly review what you learned about the agent result object. When you run an agent, the SDK returns a result object that contains several useful properties: the original input, a list of new items (such as messages or tool calls), the final output, and the conversation history.

The history property is designed to make managing conversation history easy. When you access this property on the result object, it returns an array of message objects. Each object has a role (either "user" or "assistant") and a content field containing the text of the message. This format matches what the agent expects as input for the next turn. The history property gives you the full conversation history in a format that the agent can understand for the next turn.

Preserving conversation history is crucial in multi-turn interactions. Without it, the agent would treat each message as a brand-new conversation, forgetting everything that happened before. By keeping track of both user and assistant messages, you ensure that the agent can respond in a way that makes sense, referencing earlier parts of the conversation as needed.

Let’s walk through a practical example that demonstrates how to manage a multi-turn conversation using the history property in TypeScript.

Step 1: Set Up the Agent and Imports

To begin, import the necessary modules and create your agent. In this example, we’ll make a comedian agent that tells jokes on a given topic.

Step 2: Initialize Conversation History and Add the First User Message

Start the conversation by initializing an empty conversation history as an array of AgentInputItem. Add the first user message using the user() helper.

Step 3: Run the Agent and Display the First Exchange

Pass the conversation history to the agent using the run function, then print both the user’s message and the agent’s response.

When you run this code, you'll see the user's message and the assistant's reply printed out:

Step 4: Update Conversation History Using the history Property

After the agent responds, update the conversation history using the history property from the result object. This ensures the history now includes both the user’s message and the assistant’s reply.

Step 5: Add a Second User Message and Continue the Conversation

Now, add a second user message to the conversation history and repeat the process to keep the conversation going.

If you try this next bit, you'll get the second user message and the assistant's new response — notice how the agent keeps up with the conversation and already knows the topic:

Step 6: Update and Review the Full Conversation History

Update the conversation history again with the latest result, then print the entire conversation to see how it has evolved.

Once you update and print the conversation history, you'll see the full back-and-forth so far:

By following these steps, you can build a natural, back-and-forth conversation with your agent, where each turn builds on the previous ones and the context is preserved throughout.

Summary & Preparation for Practice Exercises

In this lesson, you learned how to manage and update conversation history using the history property, enabling your agents to handle seamless multi-turn interactions in TypeScript. You saw how to initialize a conversation, update it with each new message, and pass the evolving history back to the agent. This approach is essential for building interactive applications where context matters.

By following best practices — such as managing token limits and keeping your conversation history well-structured — you can create agents that feel natural and engaging. You are now ready to practice these skills in the upcoming exercises. Keep up the great work, and get ready to build even more powerful and interactive agent experiences!

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