AG-2 in Practice #3 – Creating a Multi-Agent Workflow (Researcher + Writer)
Daniel Azevedo

Daniel Azevedo @dazevedo

About: C# developer passionate about clean code and design patterns. Sharing insights on software architecture, coding best practices, and tech tips. Join me on this journey to better code!

Location:
Portugal
Joined:
Sep 2, 2024

AG-2 in Practice #3 – Creating a Multi-Agent Workflow (Researcher + Writer)

Publish Date: May 25
0 0

Welcome back to the series!

So far, we’ve learned what AG-2 is and created a basic single-agent app. Now we’re moving to the fun part: collaboration between agents.

In this post, we’ll build a simple multi-agent pipeline:

  • A Researcher agent that finds information
  • A Writer agent that turns that info into a summary

This will show you how to chain agents, pass messages between them, and use AG-2's orchestration features.


What We’re Building

We’ll simulate this conversation:

You: “Write a summary about climate change.”
Researcher: “Climate change refers to long-term shifts…”
Writer: “Sure! Here's a concise summary of that…”


Step 1: Install or Update AG-2 (if needed)

Make sure you're using the latest AG-2:

pip install --upgrade ag2
Enter fullscreen mode Exit fullscreen mode

Step 2: Define Two Agents

Create a file called multi_agent_pipeline.py:

from ag2 import Agent, Orchestrator, Conversation
import os

os.environ["OPENAI_API_KEY"] = "your-api-key-here"

# Researcher agent
researcher = Agent(
    name="researcher",
    llm="openai/gpt-4",
    system_message="You are a helpful researcher. Given a topic, find relevant and accurate information.",
)

# Writer agent
writer = Agent(
    name="writer",
    llm="openai/gpt-4",
    system_message="You are a professional writer. Based on research findings, write clear summaries.",
)
Enter fullscreen mode Exit fullscreen mode

Step 3: Orchestrate the Conversation

Now we’ll define the flow between them.

# Define orchestrator logic
orchestrator = Orchestrator(
    agents=[researcher, writer],
    rules=[
        {"from": "user", "to": "researcher"},
        {"from": "researcher", "to": "writer"},
        {"from": "writer", "to": "user"},
    ]
)

# Start the conversation
conv = Conversation(orchestrator=orchestrator)
conv.send("Please write a short summary about climate change.")
Enter fullscreen mode Exit fullscreen mode

This defines a simple linear message flow from the user to Researcher → Writer → back to user.


Step 4: Run It

python multi_agent_pipeline.py
Enter fullscreen mode Exit fullscreen mode

You should see something like:

User: Please write a short summary about climate change.
Researcher: Climate change refers to...
Writer: Sure! Here's a summary: ...

Success! You just built a multi-agent reasoning chain.


What’s Next?

In the next post, we’ll:

  • Explore Patterns in AG-2 (like debate, delegation, human review)
  • Add tool use in multi-agent chains
  • Start designing more dynamic workflows

Keep coding

Comments 0 total

    Add comment