Files
Chris 904a5b843e Python / .NET Samples - Restructure and Improve Samples (Feature Branc… (#4092)
* Python: .NET Samples - Restructure and Improve Samples (Feature Branch) (#4091)

* Moved by agent (#4094)

* Fix readme links

* .NET Samples - Create `04-hosting` learning path step (#4098)

* Agent move

* Agent reorderd

* Remove A2A section from README 

Removed A2A section from the Getting Started README.

* Agent fixed links

* Fix broken sample links in durable-agents README (#4101)

* Initial plan

* Fix broken internal links in documentation

Co-authored-by: crickman <66376200+crickman@users.noreply.github.com>

* Revert template link changes; keep only durable-agents README fix

Co-authored-by: crickman <66376200+crickman@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: crickman <66376200+crickman@users.noreply.github.com>

* .NET Samples - Create `03-workflows` learning path step (#4102)

* Fix solution project path

* Python: Fix broken markdown links to repo resources (outside /docs) (#4105)

* Initial plan

* Fix broken markdown links to repo resources

Co-authored-by: crickman <66376200+crickman@users.noreply.github.com>

* Update README to rename .NET Workflows Samples section

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: crickman <66376200+crickman@users.noreply.github.com>

* .NET Samples - Create `02-agents` learning path step (#4107)

* .NET: Fix broken relative link in GroupChatToolApproval README (#4108)

* Initial plan

* Fix broken link in GroupChatToolApproval README

Co-authored-by: crickman <66376200+crickman@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: crickman <66376200+crickman@users.noreply.github.com>

* Update labeler configuration for workflow samples

* .NET - Reorder Agents samples to start from Step01 instead of Step04 (#4110)

* Fix solution

* Resolve new sample paths

* Move new AgentSkills and AgentWithMemory_Step04 samples

* Fix link

* Fix readme path

* fix: update stale dotnet/samples/Durable path reference in AGENTS.md

Co-authored-by: crickman <66376200+crickman@users.noreply.github.com>

* Moved new sample

* Update solution

* Resolve merge (new sample)

* Sync to new sample - FoundryAgents_Step21_BingCustomSearch

* Updated README

* .NET Samples - Configuration Naming Update (#4149)

* .NET: Restore AzureFunctions index parity with ConsoleApps under DurableAgents samples (#4221)

* Clean-up `05_host_your_agent`

* Config setting consistency

* Refine samples

* AGENTS.md

* Move new samples

* Re-order samples

* Move new project and fixup solution

* Fixup model config

* Fix up new UT project

---------

Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
904a5b843e · 2026-02-26 00:56:10 +00:00
History
..

Managing Conversation State with OpenAI

This sample demonstrates how to maintain conversation state across multiple turns using the Agent Framework with OpenAI's Conversation API.

What This Sample Shows

  • Conversation State Management: Shows how to use ConversationClient and AgentSession to 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

Key Concepts

ConversationClient for Server-Side Storage

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("{}")));

AgentSession for Conversation State

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);

Retrieving Conversation History

You can retrieve the full conversation history from the server:

CollectionResult getConversationItemsResults = conversationClient.GetConversationItems(conversationId);
foreach (ClientResult result in getConversationItemsResults.GetRawPages())
{
    // Process conversation items
}

How It Works

  1. Create an OpenAI Client: Initialize an OpenAIClient with your API key
  2. Create a Conversation: Use ConversationClient to create a server-side conversation
  3. Create an Agent: Initialize an OpenAIResponseClientAgent with the desired model and instructions
  4. Create a Session: Call agent.CreateSessionAsync() to create a new conversation session
  5. Link Session to Conversation: Pass ChatClientAgentRunOptions with the ConversationId on the first call
  6. Send Messages: Subsequent calls to agent.RunAsync() only need the session - context is maintained
  7. Cleanup: Delete the conversation when done using conversationClient.DeleteConversation()

Running the Sample

  1. Set the required environment variables:

    $env:OPENAI_API_KEY = "your_api_key_here"
    $env:OPENAI_CHAT_MODEL_NAME = "gpt-4o-mini"
    
  2. Run the sample:

    dotnet run
    

Expected Output

The sample demonstrates a three-turn conversation where each follow-up question relies on context from previous messages:

  1. First question asks about the capital of France
  2. Second question asks about landmarks "there" - requiring understanding of the previous answer
  3. 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.