⚠️ 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:

SampleDescriptionLink
🧠 AgentFx-Persisting-01-SimpleConsole app that creates and saves threadsView on GitHub
πŸ’¬ AgentFx-Persisting-02-MenuAdd a menu to load previous conversationsView on GitHub
🌐 AgentFx-AIWebChatApp-PersistingBlazor web app with persistent chat threadsView 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

ConceptWhat It Does
AgentThreadHolds your chat history and state
Serialize() / Deserialize()Save and restore the chat context
ChatMessageStorePersist chat messages in databases or storage
GoalAgents that remember your previous interactions

β€”

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.

More info in https://beacons.ai/elbruno


2 responses to “🧠 Build an Agent Chat that Remembers β€” Persisting Conversations with Microsoft Agent Framework”

  1. […] 🧠 Build an Agent Chat that Remembers — Persisting Conversations with Microsoft Agent Framew… (Bruno Capuano) […]

    Like

  2. […] 🧠 Build an Agent Chat that Remembers β€” Persisting Conversations with Microsoft Agent Framework (3 Nov 2025) – Discusses persisted conversation threads. Bruno shows how to build a chat app in C# where the agent remembers past messages across sessions by saving the AgentThread state (includes code snippets for serialization). Link: https://elbruno.com/2025/11/03/%F0%9F%A7%A0-build-an-agent-chat-that-remembers-persisting-conversati… […]

    Like

Leave a reply to Introducing the Microsoft Agent Framework – A Dev-Friendly Recap – El Bruno Cancel reply

Discover more from El Bruno

Subscribe now to keep reading and get access to the full archive.

Continue reading