โ ๏ธ 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 dailyesblog Cancel reply