This sample demonstrates how to maintain conversation state across multiple turns using the Agent Framework with OpenAI's Conversation API.
- Conversation State Management: Shows how to use
ConversationClientandAgentSessionto maintain conversation context across multiple agent invocations - Multi-turn Conversations: Demonstrates follow-up questions that rely on context from previous messages in the conversation
- Server-Side Storage: Uses OpenAI's Conversation API to manage conversation history server-side, allowing the model to access previous messages without resending them
- Conversation Lifecycle: Demonstrates creating, retrieving, and deleting conversations
The ConversationClient manages conversations on OpenAI's servers:
// Create a ConversationClient from OpenAIClient
OpenAIClient openAIClient = new(apiKey);
ConversationClient conversationClient = openAIClient.GetConversationClient();
// Create a new conversation
ClientResult createConversationResult = await conversationClient.CreateConversationAsync(BinaryContent.Create(BinaryData.FromString("{}")));The AgentSession works with ChatClientAgentRunOptions to link the agent to a server-side conversation:
// Set up agent run options with the conversation ID
ChatClientAgentRunOptions agentRunOptions = new() { ChatOptions = new ChatOptions() { ConversationId = conversationId } };
// Create a session for the conversation
AgentSession session = await agent.CreateSessionAsync();
// First call links the session to the conversation
ChatCompletion firstResponse = await agent.RunAsync([firstMessage], session, agentRunOptions);
// Subsequent calls use the session without needing to pass options again
ChatCompletion secondResponse = await agent.RunAsync([secondMessage], session);You can retrieve the full conversation history from the server:
CollectionResult getConversationItemsResults = conversationClient.GetConversationItems(conversationId);
foreach (ClientResult result in getConversationItemsResults.GetRawPages())
{
// Process conversation items
}- Create an OpenAI Client: Initialize an
OpenAIClientwith your API key - Create a Conversation: Use
ConversationClientto create a server-side conversation - Create an Agent: Initialize an
OpenAIResponseClientAgentwith the desired model and instructions - Create a Session: Call
agent.CreateSessionAsync()to create a new conversation session - Link Session to Conversation: Pass
ChatClientAgentRunOptionswith theConversationIdon the first call - Send Messages: Subsequent calls to
agent.RunAsync()only need the session - context is maintained - Cleanup: Delete the conversation when done using
conversationClient.DeleteConversation()
-
Set the required environment variables:
$env:OPENAI_API_KEY = "your_api_key_here" $env:OPENAI_CHAT_MODEL_NAME = "gpt-5.4-mini"
-
Run the sample:
dotnet run
The sample demonstrates a three-turn conversation where each follow-up question relies on context from previous messages:
- First question asks about the capital of France
- Second question asks about landmarks "there" - requiring understanding of the previous answer
- Third question asks about "the most famous one" - requiring context from both previous turns
After the conversation, the sample retrieves and displays the full conversation history from the server, then cleans up by deleting the conversation.
This demonstrates that the conversation state is properly maintained across multiple agent invocations using OpenAI's server-side conversation storage.