Skip to main content

Fixed: Microsoft Agent Framework Session Store Loses Conversations After a Restart

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

My Microsoft Agent Framework API saved every conversation in a durable session store, and still forgot all of them whenever it restarted. The store keyed each conversation by agent.Id, as Microsoft's documentation advises, and an agent created with AsAIAgent gets a new random ID every time the app starts. A fixed Id in ChatClientAgentOptions fixed it.

What I Was Doing​

I was moving an agent's conversations from memory to Azure Cosmos DB (Part 15: Persistent Conversations). The API hosts the agent with Agent Framework's AG-UI integration, which loads a conversation from an AgentSessionStore before each run and saves it after. My store wrote each session as one Cosmos DB item. Its ID came from the parts Microsoft's self-hosting documentation names: Key records by both agent.Id and the opaque sessionStoreId, plus the key's partitions, which hold the signed-in user.

The agent itself came from a Foundry project, as in the rest of the series:

ServiceDeskAgentExtensions.cs (before)
return projectClient
.AsAIAgent(new ChatClientAgentOptions
{
Name = name,
ChatOptions = new ChatOptions
{
ModelId = foundry.ModelDeployment,
Instructions = ServiceDeskInstructions.Text,
Tools = tools.AsAITools(),
},
AIContextProviders = [microsoftLearnTools],
})
.AsBuilder()
.UseLogging()
.Build(services);

To test the store, I sent two messages on the same thread, with a restart of the API between them:

  1. Hi! This week I'm working from the Pune office. Which devices do I have? The agent listed my devices.
  2. I stopped the API and started it again.
  3. Which office did I say I'm working from this week?

What I Expected​

Pune. The first run had saved the conversation in Cosmos DB, and the second request named the same thread.

What I Got​

The agent didn't know:

The agent's answer
I don't have access to your messages outside this chat, and you haven't mentioned an office here.

In Data Explorer, the container that should have held one conversation held two items:

Data Explorer with two items in the sessions container, 8448a3bb… and 1fa3fd7a…, and the second one open: an id of 64 hex characters and a session with a conversationId that starts with resp_ and an empty InMemoryChatHistoryProvider

The first item came from the run before the restart, the second from the run after it. So the store had saved the conversation, and after the restart it looked under a different ID, found nothing, and started a new conversation.

What I Checked​

What the item's ID is made of. Three things: the agent's ID, the session ID and the partitions. The session ID was the thread's ID, the same in both requests. The partition was my user, the same in both. So the agent's ID had changed.

Microsoft's reference for AIAgent.Id. A unique string identifier for the agent. For in-memory agents, this defaults to a randomly-generated ID, while service-backed agents typically use the identifier assigned by the backing service. And: They should remain stable for the lifetime of the agent instance. The lifetime of the instance, not of the app's data. The reference for IdCore, which an agent overrides to give itself an ID, adds: When null is returned, the Id property will use the default randomly-generated identifier.

Whether my agent is an in-memory agent. AsAIAgent builds the agent in the app: nothing is created in Foundry, and the options above set no Id.

The ID itself, in two runs. I wrote a small file-based app that creates the same kind of agent twice, once without an ID and once with one, and wraps the second in the same logging wrapper as my API:

ids.cs
#:package Microsoft.Agents.AI.Foundry@1.28.0-preview.261106.1
#:package Azure.Identity@1.21.0
#:property PublishAot=false
using Azure.AI.Projects;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;

AIProjectClient project = new(new Uri("https://your-resource.services.ai.azure.com/api/projects/your-project"), new AzureCliCredential());

AIAgent withoutId = project.AsAIAgent(new ChatClientAgentOptions
{
Name = "helper",
ChatOptions = new ChatOptions { ModelId = "gpt-5.6-luna" },
});
AIAgent withId = project.AsAIAgent(new ChatClientAgentOptions
{
Id = "helper",
Name = "helper",
ChatOptions = new ChatOptions { ModelId = "gpt-5.6-luna" },
});
AIAgent wrapped = withId.AsBuilder().UseLogging(Microsoft.Extensions.Logging.Abstractions.NullLoggerFactory.Instance).Build();

Console.WriteLine($"Without Id: {withoutId.Id}");
Console.WriteLine($"With Id: {withId.Id}");
Console.WriteLine($"Wrapped: {wrapped.Id}");

The #:package lines make it a .NET 10 file-based app, which dotnet run ids.cs runs without a project. Creating an agent doesn't call Foundry, so the endpoint and model only have to be valid values. I ran it twice:

Output
$ dotnet run ids.cs
Without Id: 7127c655af384a1486d0d854bd93b053
With Id: helper
Wrapped: helper

$ dotnet run ids.cs
Without Id: 1e5aa671f26b43d093fc0664439c62c0
With Id: helper
Wrapped: helper

macOS-style terminal window titled Terminal — AgentIdCheck showing two runs of dotnet run ids.cs: without an Id the agent's ID is 7127c655af384a1486d0d854bd93b053 in the first run and 1e5aa671f26b43d093fc0664439c62c0 in the second, while with Id set to helper both the agent and its logging wrapper report helper in both runs

Without an Id, every run gets a new GUID. With one, the agent keeps it, and the logging wrapper passes it through.

Why It Happens​

An agent built in your app, such as one from AsAIAgent, gets a random ID when you don't give it one. The ID only needs to stay the same while the app runs. A session store that keys conversations by agent.Id therefore saves each conversation under a key that the next run of the app can't produce, and the saved conversations become unreachable after every restart. They stay in the store until they expire, so the store also fills with orphans.

The Fix​

Give the agent a fixed ID. I used the name the agent is registered under, which is the same in every run and on every copy of the API:

ServiceDeskAgentExtensions.cs (after)
return projectClient
.AsAIAgent(new ChatClientAgentOptions
{
// A fixed ID. Without one, every start gets a new random ID, and the session store,
// which keeps each conversation under the agent's ID, can't find yesterday's conversations.
Id = name,
Name = name,
ChatOptions = new ChatOptions
{
ModelId = foundry.ModelDeployment,
Instructions = ServiceDeskInstructions.Text,
Tools = tools.AsAITools(),
},
AIContextProviders = [microsoftLearnTools],
})
.AsBuilder()
.UseLogging()
.Build(services);

Keep the ID once you've chosen it: changing it later orphans the conversations saved under the old one, the same way the random ID did.

After the Fix​

I ran the same test on a new thread. After the restart, the agent answered:

The agent's answer
You said you're working from the Pune office this week.

The new thread had one item in Cosmos DB, written by the run before the restart and replaced by the run after it.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving Id unset on an agent whose sessions are storedEvery restart loses every conversationSet Id in ChatClientAgentOptions
Dropping the agent from the key to make it stableTwo agents that share a store can read each other's conversationsKeep the agent in the key and give it a fixed ID
Changing the agent's ID in a later versionThe conversations saved under the old ID can't be foundChoose the ID once, as you would a database key

Troubleshooting​

SymptomLikely causeFix
The agent forgets a conversation after every restart, though the store saved itThe agent's ID is randomSet Id
The store has a new item for the same conversation after each restartIts key includes a value that changes per runCheck every part of the key; with the agent's ID fixed, they stay the same

FAQ​

Does the in-memory session store have this problem?​

You can't see it there: an in-memory store loses everything at a restart anyway. It shows up the first time a store outlives the app.

What about agents that live in Foundry?​

The reference says service-backed agents typically use the identifier assigned by the backing service, so their ID comes from the service. AsAIAgent doesn't create one; it builds the agent in your app.

Conclusion​

A durable AgentSessionStore that keys conversations by agent.Id only works if that ID survives a restart. An agent built in the app, such as one from AsAIAgent, gets a random ID unless you give it one, so every restart orphans every saved conversation. Set Id in ChatClientAgentOptions, for example to the agent's registered name, and keep it.

Additional Resources​