When using RunStreamingAsync in the .NET Agent Framework, the AgentThread is not properly updated/saved if the execution exits early due to:
- API errors (e.g. HTTP 400, resource exhausted, etc.)
- A triggered CancellationToken
In these cases, the agent stops execution, but the thread context (memory) is not updated, resulting in lost conversation state.
This makes it difficult to reliably persist conversation history when streaming is interrupted or fails.
Steps to Reproduce
- Restore or create an AgentThread:
AgentThread thread;
if (string.IsNullOrEmpty(threadStateJson))
thread = _agent.GetNewThread();
else
try
{
thread = _agent.DeserializeThread(JsonDocument.Parse(threadStateJson).RootElement);
}
catch
{
thread = _agent.GetNewThread();
}
- Start a streaming run:
await _agent.RunStreamingAsync(
job.UserMessage,
thread,
null,
cancellationToken: ct
);
- Cause one of the following:
- An API error (e.g. invalid input → 400, resource exhaustion)
- Trigger the CancellationToken while streaming
- Attempt to persist the AgentThread after execution.
Expected Behavior
- The AgentThread should be updated with the latest context up to the point of failure.
- Partial assistant responses and/or user messages should still be stored.
- Cancellation should gracefully finalize and persist the thread state.
Am I doing something wrong? Or is this a real issue?
When using RunStreamingAsync in the .NET Agent Framework, the AgentThread is not properly updated/saved if the execution exits early due to:
In these cases, the agent stops execution, but the thread context (memory) is not updated, resulting in lost conversation state.
This makes it difficult to reliably persist conversation history when streaming is interrupted or fails.
Steps to Reproduce
Expected Behavior
Am I doing something wrong? Or is this a real issue?