Synchronous vs. Streaming Execution in Microsoft Agent Framework
This lesson explains two ways to execute an AI agent in a .NET application:
-
Non-streaming execution – Wait until the agent generates its complete response, then return it.
-
Streaming execution – Receive the response incrementally while the model is still generating it.
The main architectural decision is whether the application needs the complete response at once or needs to display the response as it is generated.
1. Non-streaming execution – RunAsync()
In this approach, the application waits until the AI agent completes its entire response.
Execution flow:
.NET Application
|
| Send user prompt
v
Microsoft Agent Framework
|
v
LLM (Azure OpenAI / OpenAI)
|
| Generate tokens internally
| Token 1, Token 2, Token 3 ...
|
v
Complete Response
|
v
AgentResponse Object
|
v
.NET Application receives response
C# implementation
var response = await agent.RunAsync(userIssue);
Console.WriteLine(response.Text);
Understanding the code:
|
Code |
Explanation |
|---|---|
|
|
Executes the agent and returns the complete response. |
|
|
Waits asynchronously until the agent finishes execution. |
|
|
Holds the complete |
|
|
Gets the generated text from the response. |
For example, suppose the agent generates a 500-word technical report.
The LLM generates the response internally, but the application receives the complete report only after generation finishes.
When should RunAsync() be used?
-
Background processing.
-
Windows services and scheduled jobs.
-
Batch processing.
-
Queue-based message processing.
-
Generating reports and saving them into SQL Server.
-
Passing complete responses to another microservice.
In these scenarios, the application generally needs the final output rather than incremental text updates.
2. Streaming execution – RunStreamingAsync()
Streaming works differently.
Instead of waiting for the complete response, the application receives response updates as the LLM generates them.
Execution flow:
.NET Application
|
| Send user prompt
v
Microsoft Agent Framework
|
v
LLM
|
| Generate response
|
+---- Token 1 ------> .NET Application
|
+---- Token 2 ------> .NET Application
|
+---- Token 3 ------> .NET Application
|
+---- Token 4 ------> .NET Application
|
v
Completed
The application can display each incoming update immediately.
C# implementation
await foreach (var update in agent.RunStreamingAsync(userIssue))
{
Console.Write(update.Text);
}
Understanding the code:
|
Code |
Explanation |
|---|---|
|
|
Executes the agent and streams response updates. |
|
|
Represents a sequence of asynchronously available items. |
|
|
Iterates through the asynchronous sequence as updates arrive. |
|
|
Contains an individual |
|
|
Contains the text available in that particular update. |
|
|
Prints each update without adding a new line. |
Notice the difference between Console.Write() and Console.WriteLine().
Console.Write() keeps printing incoming text on the same line, producing the typing effect.
The official Microsoft documentation also demonstrates this streaming approach using await foreach.
3. Understanding IAsyncEnumerable
This is an important C# concept in the lesson.
You already know IEnumerable, which represents a sequence of items that can be iterated using foreach.
For example:
IEnumerable<string> names = new[]
{
"Syed",
"John",
"David"
};
foreach (var name in names)
{
Console.WriteLine(name);
}
IAsyncEnumerable is similar, but it supports asynchronous iteration.
await foreach (var update in agent.RunStreamingAsync(userIssue))
{
Console.Write(update.Text);
}
Why is this important?
The LLM does not necessarily produce all response updates at once. The application can asynchronously receive each update without waiting for the complete response.
The return type of RunStreamingAsync() is:
IAsyncEnumerable<AgentResponseUpdate>
The AgentResponseUpdate represents an individual response update, rather than the final aggregated response.
4. RunAsync vs. RunStreamingAsync
|
Feature |
RunAsync |
RunStreamingAsync |
|---|---|---|
|
Execution |
Non-streaming |
Streaming |
|
Response |
Complete response |
Incremental updates |
|
Return type |
|
|
|
Iteration |
Not required |
|
|
User experience |
Wait for complete response |
Progressive response display |
|
Complexity |
Simpler |
Requires stream handling |
|
Typical usage |
Background jobs |
Interactive applications |
Real-world enterprise examples
Background job – RunAsync()
An application receives 1,000 customer support tickets from Azure Service Bus. An AI agent analyses each ticket and stores the complete analysis in SQL Server.
Streaming the response to a human is unnecessary.
AI chatbot – RunStreamingAsync()
A customer asks a technical question through a Blazor application. The AI agent starts displaying its answer while the model is still generating the remaining content.
Report generation – Depends on requirements
For a report that must be saved as a complete document, use RunAsync(). For a report that should appear progressively on screen, streaming can be useful.
5. OpenTelemetry and Observability
This is another important enterprise concept covered in the lesson.
When an AI agent executes, we need to monitor its behaviour.
For example:
-
How long did the agent take to respond?
-
Which model was used?
-
How many tokens were consumed?
-
How many times did the agent call external tools?
-
Were there any execution errors?
OpenTelemetry provides a standard way to collect observability information.
Microsoft Agent Framework supports OpenTelemetry integration and emits traces, logs and metrics following GenAI semantic conventions.
.NET Application
|
v
Microsoft Agent Framework
|
v
Agent Execution
|
+------ Traces
|
+------ Metrics
|
+------ Logs
|
v
OpenTelemetry
|
v
Monitoring Platform
(Application Insights /
Aspire Dashboard)
One important implementation detail: OpenTelemetry instrumentation and exporters must be configured. Merely calling RunAsync() or RunStreamingAsync() does not automatically send telemetry to Application Insights.
Also, capturing prompts and responses requires sensitive-data instrumentation to be enabled. Microsoft advises enabling this only in development or testing because prompts and responses can contain confidential user information.
6. Important clarification: Async vs. Streaming
There is one technical distinction worth remembering for .NET development.
The transcript sometimes calls RunAsync() synchronous execution. Technically, that terminology is not accurate.
RunAsync() is asynchronous .NET execution, but it is non-streaming.
When using:
var response = await agent.RunAsync(userIssue);
The application asynchronously waits for the complete response. It does not necessarily block the underlying application thread.
Similarly, a properly awaited RunAsync() call does not automatically freeze a Blazor UI.
Streaming is useful because it allows the UI to display partial results before the complete response is available.
Also, streaming does not make the model generate tokens faster. It reduces perceived waiting time by displaying the available output earlier.
7. Final takeaway
Remember these two methods:
RunAsync()
Complete response
Wait for the agent to finish and then receive the result.
RunStreamingAsync()
Incremental response
Receive updates progressively as the agent generates the result.