Skip to main content

Command Palette

Search for a command to run...

Workflows as Agents - Microsoft Agent Framework

Turn a workflow into an agent and interact with it just like with an agent.

Updated
3 min read
Workflows as Agents - Microsoft Agent Framework

Workflows in Agent Framework

A workflow, simply put, represents a predefined set of operations. Workflows are built to manage complex business processes that can include multiple agents and integrations with external systems. Their flow is explicitly defined, providing greater control over the execution.

There is also the concept of orchestrations which are pre-built workflow patterns. Currently supported orchestrations are:

  • Concurrent

  • Sequential

  • Handoff

  • Magentic


Workflows as Agents

You can easily convert a workflow into an agent. Let’s take a look at a simple example.

I have defined two agents, one is a horror story writer agent, the other is a comedy story writer. The agent definition is the following:

var chatClient = new AzureOpenAIClient(endpoint, new AzureKeyCredential(apiKey))
    .GetChatClient(deploymentName)
    .AsIChatClient();

var horrorInstructions = @"You are a horror writer. Your task is to write EXACTLY one two-sentence horror story using the theme provided. 
After writing your two sentences, your job is complete. Do not write anything else.";
var horrorAgent = new ChatClientAgent(chatClient, horrorInstructions, "horror-writer-agent");

var comedyInstructions = @"You are a comedy writer. Your task is to write EXACTLY one two-sentence comedy story using the theme provided. 
After writing your two sentences, your job is complete. Do not write anything else.";
var comedyAgent = new ChatClientAgent(chatClient, comedyInstructions, "comedy-writer-agent");

Now, to use these agents in a workflow, I will use the sequential orchestration.

var horrorComedyOrchestration = AgentWorkflowBuilder.BuildSequential([horrorAgent, comedyAgent]);

Next, we can easily convert this workflow into an agent, and use it as it were an agent:

var horrorComedyAgent = await horrorComedyOrchestration.AsAgentAsync(
    id: "horror-comedy-agent",
    name: "HorrorComedyAgent"
);

Finally, let’s run the new agent:

var thread = horrorComedyAgent.GetNewThread();
var input = "Write a story about a boy and his dog.";

Dictionary<string, List<AgentRunResponseUpdate>> buffer = [];
await foreach (AgentRunResponseUpdate update in horrorComedyAgent.RunStreamingAsync(input, thread))
{
    if (update.MessageId is null)
    {
        continue;
    }
    Console.Clear();

    if (!buffer.TryGetValue(update.MessageId, out List<AgentRunResponseUpdate>? value))
    {
        value = [];
        buffer[update.MessageId] = value;
    }
    value.Add(update);

    foreach (var (messageId, segments) in buffer)
    {
        string combinedText = string.Concat(segments);
        if (!string.IsNullOrEmpty(combinedText))
        {
            Console.WriteLine($"{segments[0].AuthorName}: {combinedText}");
            Console.WriteLine();
        }        
    }
}

The output of the agents is the following:

horror-writer-agent: As the boy played fetch with his beloved dog in the fading light, he was blissfully unaware that the playful barks were growing fainter, echoing from the depths of an empty, darkened forest. When he turned to call his dog back, he found not his furry friend, but the twisted figure of something wearing its skin, grinning wide with his dog's last memories trapped inside.

comedy-writer-agent: Timmy was excited to finally teach his dog, Rex, how to fetch the newspaper; he figured if a dog could grab a stick, fetching a rolled-up paper should be a piece of cake. After a week of training, Rex proudly returned with a pile of newspapers, but unfortunately, they all belonged to the neighbors-who were now very confused as to why they suddenly had the Daily Bark.

Notice how easy it was to create an agent based on a defined workflow and use it as part of further processing.

The Azure Behind the Madness

Part 8 of 15

Explore the world of Microsoft Azure - from AI and cloud architecture to data and DevOps. The Azure Behind the Madness brings insights, stories, and hands-on guidance for building intelligent, scalable solutions in the cloud.

Up next

Azure AI Search - Data Deletion and Change Detection Policies

Define a data deletion detection policy and add a data change detection policy.