AutoGen Multi Agent Tutorial 2026
AutoGen runs multiple agents as a group chat bounded by termination conditions. Setup, a minimal two-agent writer/critic team, component reference, structured output, and the four ways multi-agent systems fail.
📜 Table of Contents
📖 Keep reading
AutoGen Multi-Agent Tutorial 2026
The point of multiple agents is not more models - it is cross-checking. One writes, one critiques, and that beats a single model revising itself. AutoGen implements this as conversation: agents take turns in a group until a termination condition fires.
The risk is the same as the benefit: conversation is the freest form of collaboration and the hardest to control.
Know which version you are reading
- 0.2 and earlier:
ConversableAgent,UserProxyAgent,initiate_chat. - 0.4+: split into
autogen-core,autogen-agentchatandautogen-ext, async and streaming by default. Microsoft is also consolidating around Agent Framework, so confirm the current API in the docs.
This tutorial uses the new API.
Install
pip install -U autogen-agentchat autogen-ext[openai]
pip install -U autogen-ext[docker] # container code execution
Minimal team
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.conditions import TextMentionTermination, MaxMessageTermination
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
client = OpenAIChatCompletionClient(model="gpt-4o-mini")
writer = AssistantAgent("writer", model_client=client,
system_message="You are a tech editor. Write 300 words, end with TERMINATE.")
team = RoundRobinGroupChat([writer],
termination_condition=TextMentionTermination("TERMINATE") | MaxMessageTermination(10))
await Console(team.run_stream(task="Explain RAG in a short paragraph"))
Always combine a text trigger with a hard cap - a model that forgets the keyword will chat forever.
Writer plus critic
Add a second agent whose system message says: list at most three factual or logical problems, and reply APPROVE when clean. Then terminate on APPROVE or 12 messages.
RoundRobinGroupChat enforces strict rotation in list order - good for fixed produce/review loops. SelectorGroupChat lets a model pick the next speaker, which costs an extra call and adds nondeterminism.
Components
| Component | Role |
|---|---|
| AssistantAgent | Prompted conversational agent |
| CodeExecutorAgent | Runs code in a sandbox |
| RoundRobinGroupChat | Fixed rotation |
| SelectorGroupChat | Model picks next speaker |
| Swarm | Handoff-based routing |
| MagenticOneGroupChat | Manager-driven orchestration |
| MaxMessage / Timeout / TextMention termination | Stop conditions |
Tools and structured output
Pass plain typed functions in tools and set reflect_on_tool_use=True, otherwise the agent may just echo the tool output. For typed results, set output_content_type to a Pydantic model and read the parsed instance from response.content.
Four failure modes
- No hard stop - always add
MaxMessageTerminationand, in production,TimeoutTermination. - Overlapping roles - differentiate what each agent does not do.
- Cost blowup - history is resent every turn; keep teams under three agents.
- No visibility - log every streamed message, not just the final answer.
When not to use multi-agent
A single prompt with two or three tools beats a team for most tasks. Use agents when you need opposing views or code execution; use a workflow engine when order must be deterministic.
FAQ
Where did UserProxyAgent go? CodeExecutorAgent handles execution in the new API; old code will not run.
Does it have to be async? Yes in the new API; wrap with asyncio.run().
Local models? Supported through OpenAI-compatible clients, with stricter termination rules.
Related
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.