Method StreamAsync
StreamAsync(Agent, string, AgentExecutionOptions, CancellationToken)
Streams agent output as an async enumerable.
public static IAsyncEnumerable<AgentStreamToken> StreamAsync(this Agent agent, string input, AgentExecutionOptions options = null, CancellationToken cancellationToken = default)
Parameters
agentAgentThe agent to execute.
inputstringThe input prompt.
optionsAgentExecutionOptionsOptional execution options.
cancellationTokenCancellationTokenCancellation token.
Returns
- IAsyncEnumerable<AgentStreamToken>
An async enumerable of streaming tokens.
Examples
await foreach (var token in agent.StreamAsync("Hello"))
{
if (token.Type == AgentStreamTokenType.Content)
Console.Write(token.Text);
}
StreamAsync(Agent, string, AgentStreamResult, AgentExecutionOptions, CancellationToken)
Streams agent output with result accumulation.
public static IAsyncEnumerable<AgentStreamToken> StreamAsync(this Agent agent, string input, AgentStreamResult result, AgentExecutionOptions options = null, CancellationToken cancellationToken = default)
Parameters
agentAgentThe agent to execute.
inputstringThe input prompt.
resultAgentStreamResultAccumulator for the streaming result.
optionsAgentExecutionOptionsOptional execution options.
cancellationTokenCancellationTokenCancellation token.
Returns
- IAsyncEnumerable<AgentStreamToken>
An async enumerable of streaming tokens.
Examples
var result = new AgentStreamResult();
await foreach (var token in agent.StreamAsync("Hello", result))
{
Console.Write(token.Text);
}
Console.WriteLine($"\n\nTotal tokens: {result.TokenCount}");