Skip to main content
Agents can maintain context across multiple turns by passing message history between runs. This enables natural, multi-step conversations.

Passing Message History

The simplest approach is to feed the messages from one run into the next:
The agent sees the full conversation history and can reference earlier messages.

Session IDs

Use session_id to logically group runs that belong to the same conversation. This is recorded in traces and makes it easy to correlate runs:

Building a Conversation Loop

A typical interactive conversation loop:

Async Conversation Loop

Streaming Multi-Turn

When streaming, collect the final RunCompleted event to get the messages for the next turn:

Message Structure

Each message in the history is a Message object with:
Messages include system prompts, user inputs, assistant responses, and tool call results. The full history is sent to the model on each turn, so be mindful of token limits in very long conversations.