โš ๏ธ 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


4 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

  3. Sadly does not seem to apply to the Ollama extensions?

    Like

    1. Not working on ollama models?
      Weird, I was assuming that it should work.

      Thanks for the heads-up!

      Like

Leave a comment

Discover more from El Bruno

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

Continue reading