Note

Access to this page requires authorization. You can try signing in or .

Access to this page requires authorization. You can try .

Step 5: Workflows

Workflows let you chain multiple steps together — each step processes data and passes it to the next.

Define workflow steps (executors):

using Microsoft.Agents.AI.Workflows;

// Step 1: Convert text to uppercase
Func<string, string> uppercaseFunc = s => s.ToUpperInvariant();
var uppercase = uppercaseFunc.BindAsExecutor("UppercaseExecutor");

// Step 2: Reverse the string and yield output
class ReverseTextExecutor() : Executor<string, string>("ReverseTextExecutor")
{
 public override ValueTask<string> HandleAsync(string message, IWorkflowContext context, CancellationToken cancellationToken = default)
 {
 return ValueTask.FromResult(string.Concat(message.Reverse()));
 }
}
ReverseTextExecutor reverse = new();

Build and run the workflow:

WorkflowBuilder builder = new(uppercase);
builder.AddEdge(uppercase, reverse).WithOutputFrom(reverse);
var workflow = builder.Build();

await using Run run = await InProcessExecution.RunAsync(workflow, "Hello, World!");
foreach (WorkflowEvent evt in run.NewEvents)
{
 if (evt is ExecutorCompletedEvent executorComplete)
 {
 Console.WriteLine($"{executorComplete.ExecutorId}: {executorComplete.Data}");
 }
}

Tip

See here for a full runnable sample application.

Define workflow steps (executors) and connect them with edges:

Build and run the workflow:

Tip

See the full sample for the complete runnable file.

Next steps

Go deeper:


Feedback

Was this page helpful?

Additional resources