Fixed: EtagException: AgentState 'conversation' could not save key because another turn updated the state first
I sent two orders one right after the other to a Microsoft 365 Agents SDK agent whose order handler takes a few seconds. Both got a confirmation, then the second one also got AgentState 'conversation' could not save key ... because another turn updated the state first, and that order was gone. The SDK runs overlapping turns of one conversation at the same time, and refuses the save that comes second. The fix is to save before confirming, and to tell the person when their change didn't make it.
What I Was Doing
While building Part 4 of the Microsoft 365 Agents SDK Fundamentals, on Agents SDK 1.9.205 and .NET 10, I wanted to know what happens to conversation state when two messages of the same conversation are handled at once. That happens whenever an agent takes a while to answer, for example while it waits for another service, and someone sends a second message in the meantime.
So I built a small lunch-order agent. order <item> adds the item to a list in the conversation's state, and orders shows the list. Before it answers, the order handler waits three seconds, standing in for a slow call:
using System.Text.RegularExpressions;
using Microsoft.Agents.Builder;
using Microsoft.Agents.Builder.App;
using Microsoft.Agents.Builder.State;
namespace ConcurrentTurnsDemo;
public class LunchOrderAgent : AgentApplication
{
private static readonly Regex OrderPattern = new(@"^order\s+(.{1,40})$", RegexOptions.IgnoreCase);
public LunchOrderAgent(AgentApplicationOptions options) : base(options)
{
OnMessage(OrderPattern, OrderAsync);
OnMessage("orders", ShowOrdersAsync);
}
private async Task OrderAsync(ITurnContext turnContext, ITurnState turnState, CancellationToken cancellationToken)
{
string item = OrderPattern.Match(turnContext.Activity.Text).Groups[1].Value.Trim();
List<string> orders = turnState.Conversation.GetValue<List<string>>("orders", () => []);
orders.Add(item);
turnState.Conversation.SetValue("orders", orders);
// Stands in for a slow call that changes nothing elsewhere, such as checking the item against today's menu.
await Task.Delay(TimeSpan.FromSeconds(3), cancellationToken);
await turnContext.SendActivityAsync($"Ordered **{item}**. Orders so far: {orders.Count}.", cancellationToken: cancellationToken);
}
private async Task ShowOrdersAsync(ITurnContext turnContext, ITurnState turnState, CancellationToken cancellationToken)
{
List<string> orders = turnState.Conversation.GetValue<List<string>>("orders", () => []);
string reply = orders.Count == 0
? "No orders yet."
: "Orders:\n\n" + string.Join("\n", orders.Select(order => $"- {order}"));
await turnContext.SendActivityAsync(reply, cancellationToken: cancellationToken);
}
}
It runs with the usual hosting: AddAgent<LunchOrderAgent>(), and MemoryStorage registered as the agent's IStorage. In the Microsoft 365 Agents Playground, I sent order tomato soup and waited for the answer. Then I sent order green salad and, straight after it, order chips.
What I Expected
Three orders in the list, or at least an honest answer for any order that didn't make it.
What I Got
Both new orders got a confirmation. Then chips got a second answer, the exception's message, and orders showed that chips was never kept:

The person ordering chips was told Ordered chips, and then shown an error they can't do anything with. The agent's terminal had the exception:
fail: Microsoft.Agents.Hosting.AspNetCore.CloudAdapter[0]
Unhandled exception during turn processing. AgentState 'conversation' could not save key 'emulator/conversations/f5dfb98d-7abc-4406-958a-030e43d3fedb' because another turn updated the state first (status: ConditionNotMet). This turn's state changes were not saved.
=====================================
ErrorCode: -2146233088
Source: Microsoft.Agents.Builder
Method: MoveNext
TimeStamp: 2026-11-21T12:11:44.5939060Z
Error: AgentState 'conversation' could not save key 'emulator/conversations/f5dfb98d-7abc-4406-958a-030e43d3fedb' because another turn updated the state first (status: ConditionNotMet). This turn's state changes were not saved.
HelpLink Url: Not Provided
=====================================
Microsoft.Agents.Storage.EtagException: AgentState 'conversation' could not save key 'emulator/conversations/f5dfb98d-7abc-4406-958a-030e43d3fedb' because another turn updated the state first (status: ConditionNotMet). This turn's state changes were not saved.
at Microsoft.Agents.Builder.State.AgentState.SaveChangesAsync(ITurnContext turnContext, Boolean force, CancellationToken cancellationToken)
at Microsoft.Agents.Builder.State.TurnState.SaveStateAsync(ITurnContext turnContext, Boolean force, CancellationToken cancellationToken)
at Microsoft.Agents.Builder.App.AgentApplication.OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken)

What I Checked
Whether the two turns really overlapped. The Playground's Log Panel has the times: green salad arrived at 12:11:39 and chips at 12:11:41, and their answers went out at 12:11:42 and 12:11:44. The second turn started before the first had finished, so the SDK handled both at the same time. Each had loaded the same saved list, with tomato soup in it, and added its own item: that's why both said Orders so far: 2.
Where the exception comes from. The stack trace starts in AgentState.SaveChangesAsync, called by AgentApplication at the end of the turn. In the SDK's source at version 1.9.205, that method saves with the version of the state it loaded, and turns a refused save into this exception (AgentState.cs, lines 268 to 284):
if (cachedState.Version != null)
{
writeResults = await _storage.WriteAsync(
readOnlyChanges,
new StorageWriteOptions { ExpectedVersion = cachedState.Version },
cancellationToken).ConfigureAwait(false);
}
else
{
writeResults = await _storage.WriteAsync(readOnlyChanges, new StorageWriteOptions(), cancellationToken).ConfigureAwait(false);
}
var writeResult = writeResults[key];
if (writeResult.Status != StorageOperationStatus.Succeeded)
{
throw new EtagException($"AgentState '{Name}' could not save key '{key}' because another turn updated the state first (status: {writeResult.Status}). This turn's state changes were not saved.");
}
Green salad's turn saved first, which gave the stored state a new version. Chips' turn then tried to save with the version it had loaded, the storage refused it with ConditionNotMet, and the SDK threw. That check was added on purpose: the SDK's issue #1012 asked for AgentState to pass the loaded version so that a stale save fails with ConditionNotMet instead of winning, rather than silently overwriting the later turn's changes.
Why the person saw it. The save happens after the route has already answered, and an exception at that point goes to the CloudAdapter's default error handler, which sends the exception's message to the user. Fixed: The Microsoft 365 Agents SDK Sends the Exception Message to the User After OnTurnError covers that handler.
What Microsoft recommends. Learn's Prepare an agent for production guide has a section on exactly this, Preserve state and handle concurrent writes: Reject stale writes and retry only bounded, idempotent domain operations. Don't replay an entire turn after sending a reply or performing an external action. About its own reference agent, it adds: The reference saves once per changed turn before acknowledging success.
Why It Happens
Turns of one conversation can run at the same time, and each loads the state as it was when it started. When two of them change it, the SDK keeps the first save and refuses the second, which protects the first turn's changes. But by default the SDK saves at the very end of the turn, after your route has answered, so the refused turn has already told the person their change went through.
The Fix
Do what the production guide describes: save before you confirm anything, and when the save is refused, say so. In OrderAsync, after the slow call:
private async Task OrderAsync(ITurnContext turnContext, ITurnState turnState, CancellationToken cancellationToken)
{
string item = OrderPattern.Match(turnContext.Activity.Text).Groups[1].Value.Trim();
List<string> orders = turnState.Conversation.GetValue<List<string>>("orders", () => []);
orders.Add(item);
turnState.Conversation.SetValue("orders", orders);
// Stands in for a slow call that changes nothing elsewhere, such as checking the item against today's menu.
await Task.Delay(TimeSpan.FromSeconds(3), cancellationToken);
try
{
// Save now, before telling anyone the order went through.
await turnState.SaveStateAsync(turnContext, cancellationToken: cancellationToken);
}
catch (EtagException)
{
// Another turn saved the orders while this one was running, so this order wasn't saved.
// Load the saved state again, so the end of the turn doesn't try to save this change a second time.
await turnState.LoadStateAsync(turnContext, force: true, cancellationToken: cancellationToken);
await turnContext.SendActivityAsync($"Another order arrived at the same moment, so I didn't add **{item}**. Please send it again.", cancellationToken: cancellationToken);
return;
}
await turnContext.SendActivityAsync($"Ordered **{item}**. Orders so far: {orders.Count}.", cancellationToken: cancellationToken);
}
EtagException is in the Microsoft.Agents.Storage namespace, so add using Microsoft.Agents.Storage; at the top of the file.

turnState.SaveStateAsyncsaves every scope that changed, there and then, with the same version check as the save at the end of the turn. When it succeeds, the save at the end finds nothing new to write.catch (EtagException)is the refused save. The order isn't confirmed, and the person gets a message they can act on instead of an exception.turnState.LoadStateAsync(turnContext, force: true, ...)replaces this turn's unsaved changes with the state as it's stored now, so the save at the end of the turn has nothing to write and doesn't fail a second time.- No automatic retry. Sending it again is up to the person, as the guide advises. The wait here changes nothing outside the agent; if your slow call does, such as placing an order in another system, that system needs its own protection against the same order arriving twice.
After the Fix
I restarted the agent and repeated the test: order tomato soup, then order green salad and order chips one right after the other, then orders, then order chips once more:

Chips was refused with a clear message instead of a false confirmation, sending it again worked, and the terminal had no exception.
The First Save Isn't Checked
The check needs a version to compare, and a conversation that has never been saved doesn't have one. Before the test above, on a fresh agent, I sent order tomato soup and, straight after it, order green salad, so that the conversation's very first two turns overlapped:

Both orders were confirmed, there was no error, and tomato soup was gone. The source above shows why: when a turn found no saved state, cachedState.Version is null, and the SDK saves with new StorageWriteOptions(), an unconditional write that replaces whatever another turn saved in the meantime. The storage interface does have a create-only mode, StorageWriteMode.CreateOnly, but AgentState doesn't use it in 1.9.205, and it doesn't on the SDK's main branch either. The fix in this post can't catch that case, because the save it makes succeeds.
AgentState has no setting that makes the first save conditional. Microsoft's production reference gets it by wrapping its storage: the guide says its wrapper performs conditional creates and updates. I've reported the gap to the SDK's maintainers as issue #1102.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Confirming a change before it's saved | People are told about changes that are then refused | Call SaveStateAsync before the confirmation |
Catching EtagException without reloading the state | The save at the end of the turn fails again with the same exception | Call LoadStateAsync with force: true in the catch |
| Running the whole turn again automatically | Replies and outside actions happen twice | Ask the person to send it again, or retry only steps that are safe to repeat |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| AgentState 'conversation' could not save key ... (status: ConditionNotMet) | Two turns changed the conversation's state at the same time | Save before confirming and handle EtagException, as above |
The same message, but for AgentState 'user' | Two turns from one person changed their user state at the same time | The same fix: SaveStateAsync saves every changed scope |
| A change disappears without any error | It was lost on the conversation's first save | See The First Save Isn't Checked |
The exception is thrown a second time after the catch | The state wasn't reloaded | Call LoadStateAsync(turnContext, force: true, ...) |
FAQ
Can this happen in Teams?
Any channel that sends two activities of one conversation close together can trigger it, because the agent handles activities as they arrive. I reproduced it in the Agents Playground. Group chats, where several people write at once, and agents that take seconds to answer, such as ones that call an AI model, make it more likely.
Why not use a lock so the turns run one after another?
A lock in the agent's memory only covers one copy of the agent. As soon as the agent runs on two servers, two turns of one conversation can run on different servers, and only the storage's version check sees both.
Does Azure Blob Storage or Cosmos DB storage behave the same way?
In 1.9.205, BlobsStorage and CosmosDbPartitionedStorage implement the same versioned storage interface as MemoryStorage, IStorageV2, so AgentState saves through them with the same check. I ran this test with MemoryStorage only.
Conclusion
AgentState 'conversation' could not save key because another turn updated the state first means two turns of one conversation changed its state at the same time, and the SDK refused the later save to protect the earlier one. The harm comes from the default order of things: the route answers first, and the refused save comes after. Save with SaveStateAsync before you confirm, catch EtagException, reload the state and ask the person to send it again. And remember the one case the check doesn't cover yet: a conversation's very first save.
Additional Resources
- Prepare an agent for production: Preserve state and handle concurrent writes — Microsoft's guidance on stale writes and retries.
- EtagException class — the exception's API reference.
- Manage state — state scopes and storage in the Agents SDK.
- Agents-for-net issue #1012 — why
AgentStatepasses the loaded version when it saves. - Agents-for-net issue #1102 — the report about the unconditional first save.
