Building multi-agent workflows with AutoGen

Coordinate multiple AI agents to tackle complex tasks


Building multi-agent workflows with AutoGen

Single agents are great for straightforward tasks but when you need to break a complex problem into specialised steps, you want multiple agents working together. AutoGen from Microsoft Research is one of the better frameworks I have used for building these kinds of workflows. With version 0.4 they rewrote the core to use an async event driven architecture which makes it a lot more robust for production scenarios.

Install the required packages:

pip install autogen-agentchat autogen-ext[azure] python-dotenv

Set up your Azure OpenAI credentials in your .env file:

AZURE_OPENAI_ENDPOINT=https://yourresource.openai.azure.com/
AZURE_OPENAI_API_KEY=<your api key>
AZURE_OPENAI_API_VERSION=2024-12-01-preview
AZURE_OPENAI_DEPLOYMENT=gpt-4o

The pattern I find most useful is to have a team of agents where each one has a specific role. Here is an example where I set up a researcher agent and a writer agent to produce a short report:

import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.conditions import TextMentionTermination
from autogen_ext.models.openai import AzureOpenAIChatCompletionClient
from dotenv import load_dotenv
import os
 
load_dotenv()
 
model_client = AzureOpenAIChatCompletionClient(
    azure_deployment=os.getenv("AZURE_OPENAI_DEPLOYMENT"),
    api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
    azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
    api_key=os.getenv("AZURE_OPENAI_API_KEY"),
)
 
researcher = AssistantAgent(
    name="Researcher",
    model_client=model_client,
    system_message="""You are a research analyst.
    Your job is to gather key facts and insights on the topic given to you.
    Be concise and focus on the most important points.""",
)
 
writer = AssistantAgent(
    name="Writer",
    model_client=model_client,
    system_message="""You are a business writer.
    Take the research provided and turn it into a clear, professional summary.
    When the summary is complete and approved, end your message with COMPLETE.""",
)
 
termination = TextMentionTermination("COMPLETE")
 
team = RoundRobinGroupChat(
    participants=[researcher, writer],
    termination_condition=termination,
    max_turns=6,
)
 
async def main():
    result = await team.run(
        task="Research and summarise the business impact of AI agents in the financial services industry"
    )
    for message in result.messages:
        print(f"\n{message.source}: {message.content}")
 
asyncio.run(main())

The RoundRobinGroupChat passes the conversation between agents in order. The termination condition watches for the word COMPLETE so the workflow stops cleanly once the writer is satisfied.

Where this gets interesting

The real power comes when you add tools to your agents. You can give the researcher agent access to a web search tool or a database query tool and now it can pull in real data rather than relying on what the model already knows.

from autogen_agentchat.agents import AssistantAgent
 
async def search_internal_docs(query: str) -> str:
    # Replace this with your actual search implementation
    return f"Search results for: {query}"
 
researcher = AssistantAgent(
    name="Researcher",
    model_client=model_client,
    tools=[search_internal_docs],
    system_message="You are a research analyst with access to internal documentation. Use the search tool to find relevant information.",
)

I have been using AutoGen on projects where the workflow has several steps that each require different expertise. Breaking it down into agents makes each part easier to tune and test independently. Give it a try and see how it simplifies your more complex AI workflows.