AutoGen Tutorial: Build a Multi-Agent System Step by Step
Build a multi-agent system with AutoGen step by step: install, first agent, two-agent loop, group chat and tools, plus the honest note on Microsoft Agent Framework.
By Galactic News Editorial · Published 2026-11-17T19:55:33+00:00 · Updated 2026-11-17T19:55:33+00:00
AutoGen Tutorial: Build a Multi-Agent System Step by Step
This AutoGen tutorial shows you how to build a multi-agent system in Python, from install to a working team of agents that talk to each other. AutoGen is Microsoft's open-source framework for building applications where multiple agents collaborate through structured conversation. You will set up the framework, run a single agent, build a two-agent loop, and then run a group chat where agents take turns. One warning up front: Microsoft now directs new users to Microsoft Agent Framework, and the AutoGen project is in maintenance mode. This tutorial is still the right place to learn the multi-agent pattern, and to work with AutoGen code you already have. If you are starting a brand-new production project, read the migration guide first.
What AutoGen is
AutoGen is a Python framework for building agentic applications with a strong focus on multi-agent conversations and orchestration. An agent is a Python object with a role, a system message, and a connection to a language model. Agents pass messages to each other, call tools, run code, and ask a human for input when needed.
The current version splits the project into layers. AgentChat is the programming framework for building conversational single and multi-agent applications. Core is the event-driven framework underneath it, for workflows and distributed agent systems. Extensions add model providers, tooling and code execution. AutoGen Studio is a web app for prototyping and managing agents without writing code. Magentic-One is a console-based multi-agent assistant for web and file-based tasks, built on AgentChat.
AutoGen works with OpenAI, Azure OpenAI, Anthropic Claude, Google Gemini and local models through Ollama. It is free and open source. Model calls are billed by your model provider, not by AutoGen.
Step 1: Set up Python and a virtual environment
AutoGen requires Python 3.10 or newer. Check your version first, then create a fresh virtual environment so the install stays clean.
python3 --version
python3 -m venv .venv
source .venv/bin/activateOn Windows, activate with .venv\Scripts\activate. Use a fresh environment for every project. Mixing agent frameworks in one environment causes version conflicts that are hard to debug.
Step 2: Install AutoGen
Install the AgentChat package and the OpenAI extension. The current install command from the official docs is:
pip install -U "autogen-agentchat" "autogen-ext[openai]"Older guides use pip install pyautogen. That installs the v0.2 line, which is still widely used in existing codebases. The v0.4 and later lines split the package, so use the command above for new work. If you want the no-code studio as well, install it separately with pip install -U autogenstudio and start it with autogenstudio ui --port 8080 --appdir ./myapp.
Step 3: Set your model API key
AutoGen needs a model to run. If you use OpenAI, set your key as an environment variable. Never paste the key into your code.
export OPENAI_API_KEY="your-key-here"On Windows, use set OPENAI_API_KEY=your-key-here in the same terminal session. The client reads the key from the environment. Keep the key out of version control. A leaked key in a public repo gets used within minutes.
Step 4: Run your first agent
This is the hello-world example from the official docs. One AssistantAgent runs a single task and prints the result.
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient
async def main() -> None:
agent = AssistantAgent("assistant", OpenAIChatCompletionClient(model="gpt-4o"))
print(await agent.run(task="Say 'Hello World!'"))
asyncio.run(main())Save this as first_agent.py and run it with python first_agent.py. The agent has a name, "assistant", and a model client pointed at gpt-4o. The run method takes a task string and returns the agent's response. If it prints a greeting, your install and key both work.
Step 5: Build a two-agent loop
One agent is a chatbot. Two agents are a system. The classic AutoGen pattern pairs an assistant that writes code with a user proxy that runs it and feeds the output back. The assistant proposes a solution, the proxy executes it, and the loop repeats until the task is done or the turn limit is hit.
In the AgentChat layer, the equivalent pattern uses a team. Create two agents with different roles and system messages, then run them as a group. Give the writer agent a system message like "You write Python code to solve the task. Return only code." Give the reviewer agent a system message like "You check code for errors and suggest fixes." Each agent only sees its own role, and the conversation carries the work forward.
Two settings control the loop. The turn limit caps how many turns the team can take before the framework stops it, so a stuck loop cannot run forever. Human input mode decides whether a person must approve each step, approve only when asked, or never get involved. Start with a person in the loop. Move to full automation only after the team behaves well on your task.
Step 6: Run a group chat with a team
A group chat is where AutoGen earns its name. Three or more agents take turns on a shared task: a planner breaks the work down, a researcher gathers facts, a writer drafts the result, and a critic checks it. The team decides who speaks next, either in a fixed round-robin order or by letting the conversation pick the next speaker.
Build the team in this order.
- Define one agent per job. Keep each system message short and specific. A planner plans. A writer writes. Do not give one agent three jobs.
- Give the team a clear task. One sentence, one deliverable, one format. Vague tasks produce vague conversations.
- Set a turn limit. Twenty turns is a good starting point for a three-agent team. Raise it only when the team keeps stopping early.
- Watch the first run. Read the whole conversation. Check who repeated themselves, who went off task, and who never spoke.
- Tighten the system messages. Fix the worst failure first, then run again. One change per run, so you know what helped.
AutoGen Studio is useful here. It gives you a web interface for designing agent flows and watching the conversation, without writing all the Python by hand. Prototype the team in Studio, then move the working design into code.
Step 7: Give agents tools
Agents become useful when they can act. Tools are Python functions the agent can call: search the web, read a file, query a database, or call an API. Register a function as a tool, describe what it does in plain language, and the model decides when to call it.
Keep tools small and safe. A tool that reads files should only read one folder. A tool that runs code should run it in a sandbox or a container, not on your main machine. Log every tool call during development, so you can see what the agent actually did. The current docs include MCP support through McpWorkbench, which lets you expose tools through a standard interface instead of wiring up function calling by hand for each one.
Common mistakes to avoid
Most AutoGen failures come from the same few mistakes. Avoid them and the framework is straightforward.
- Vague system messages. "You are a helpful assistant" produces a generic agent. Name the job, the input, the output format, and what not to do.
- No turn limit. Without a cap, two agents can argue forever and burn your model budget. Always set one.
- Too many agents. Three focused agents beat six overlapping ones. Add an agent only when a job has no owner.
- Running generated code unsandboxed. Code the agent writes is untrusted input. Run it in a container or a sandbox, never directly on your machine.
- Skipping the human. Keep a person in the loop until the team is reliable. Full automation is the last step, not the first.
AutoGen vs Microsoft Agent Framework
Microsoft now points new users toward Microsoft Agent Framework, and has published an official migration guide from AutoGen. The Agent Framework is the successor to both AutoGen and Semantic Kernel, with graph-based workflows, checkpointing, human-in-the-loop flows, and a unified Python and .NET story. The AutoGen docs note that the project is in maintenance mode, with bug fixes and security patches rather than new features.
Use AutoGen if you are inheriting an AutoGen codebase, you want its AgentChat patterns specifically, or you need to move fast on a prototype. Use Microsoft Agent Framework if you are starting a new production project and want the actively developed path. The concepts transfer: agents, tools, teams and turn-taking work the same way in both.
Bottom line
AutoGen gives you a practical way to build multi-agent systems in Python: install two packages, set a key, run one agent, then grow to a team with clear roles and a turn limit. Start with AgentChat, prototype in AutoGen Studio when a visual flow helps, and keep a human in the loop until the team is reliable. Learn it if you keep seeing it in codebases or client projects. For a new production system, follow Microsoft's direction and evaluate Microsoft Agent Framework first, using the official migration guide to carry your AutoGen patterns across.