β οΈ This blog post was created with the help of AI tools. Yes, I used a bit of magic from language models to organize my thoughts and automate the boring parts, but the geeky fun and the π€ in C# are 100% mine.
Hi!
Have you ever built a chat app where your AI completely forgets what you said five seconds ago? π
Thatβs where persisted threads come in.
In this post, weβll explore how the new Microsoft Agent Framework for .NET lets you create agents that remember β across multiple interactions, sessions, or even app restarts.
And yes, thereβs a demo video too π
π¬ What Are Persisted Threads?
Every agent created with the Microsoft Agent Framework communicates through an Agent Thread β essentially a container for conversation history and state.
When you persist a thread, youβre saving that entire conversation context somewhere (like a file, a database, or cloud storage) so it can be reloaded later.
Think of it as giving your agent memory β it picks up right where you left off.
π‘ Each conversation = one
AgentThread
You can save it, restore it, and continue chatting seamlessly.
βοΈ How It Works
The AgentThread object holds the entire conversation context β both user and AI messages.
You can serialize it to JSON, store it, and deserialize it later to continue.
Hereβs a simple example π
// Create a chat agent
var agent = MyAgentFactory.CreateChatAgent();
// Start a new thread
var thread = await agent.NewThreadAsync();
// Letβs chat!
Console.WriteLine(await agent.RunAsync("Tell me a quick joke!", thread));
// Persist the thread to a file
var serialized = thread.Serialize();
File.WriteAllText("thread.json", serialized);
// Later... reload and continue
var saved = File.ReadAllText("thread.json");
var resumedThread = AgentThread.Deserialize(saved);
// Continue the conversation
Console.WriteLine(await agent.RunAsync("Now tell it as a pirate!", resumedThread));
Thatβs it β your AI now remembers the previous chat.
No extra work. No complex state management. Justβ¦ memory. πΎ
π§© Real Samples to Try
The Microsoft team published official examples in the Generative AI for .NET repository.
Youβll find multiple versions of persisted chat demos:
| Sample | Description | Link |
|---|---|---|
| π§ AgentFx-Persisting-01-Simple | Console app that creates and saves threads | View on GitHub |
| π¬ AgentFx-Persisting-02-Menu | Add a menu to load previous conversations | View on GitHub |
| π AgentFx-AIWebChatApp-Persisting | Blazor web app with persistent chat threads | View on GitHub |
Each one builds upon the same concept:
store the AgentThread β reload it later β continue the chat.
ποΈ Next Step: Use a ChatMessageStore
When you move beyond local testing, you can use a custom ChatMessageStore to persist data in:
- SQL or Cosmos DB
- Azure Blob Storage
- or even your own API
This is the foundation for stateful AI apps β assistants, copilots, and customer bots that donβt reset with every message.
π§ Learn More: Generative AI for .NET
Want to go deeper?
Check out the official learning path π aka.ms/genainet
It includes:
- tutorials
- code samples
- agent building blocks
- and guidance for deploying on Azure
This repo is packed with ready-to-run examples that mix .NET + AI beautifully.
πΎ TL;DR
| Concept | What It Does |
|---|---|
| AgentThread | Holds your chat history and state |
| Serialize() / Deserialize() | Save and restore the chat context |
| ChatMessageStore | Persist chat messages in databases or storage |
| Goal | Agents that remember your previous interactions |
β
Happy coding!
Greetings
El Bruno
More posts in my blog ElBruno.com.
More info in https://beacons.ai/elbruno

Leave a reply to Dew Drop β November 4, 2025 (#4533) β Morning Dew by Alvin Ashcraft Cancel reply