AI Tools & Reviews
AutoGen Tutorial: Building Multi-Agent Conversations with Microsoft's Framework
Contents
The Core ConceptsInstalling AutoGenA Working Two-Agent Conversationhuman_input_mode Controls How Autonomous It IsCoordinating More Than Two AgentsWhen AutoGen Is the Right ChoiceFrequently Asked QuestionsWhere CrewAI structures multi-agent work around roles and sequential tasks, Microsoft’s AutoGen takes a different angle: agents that talk to each other in a conversation, the same way a Slack thread between coworkers unfolds — one message at a time, with the flexibility to loop in a human whenever needed. That conversational model is the whole design philosophy, and it changes how you build with it.
The Core Concepts
- ConversableAgent — the base building block: an agent that can send and receive messages, optionally backed by an LLM, a human, or both.
- AssistantAgent — a ConversableAgent preset for LLM-driven responses, typically the “does the actual work” agent.
- UserProxyAgent — represents a human (or automated code-execution) in the conversation, able to approve, reject, or directly execute code the assistant proposes.
- GroupChat — coordinates a conversation among more than two agents, deciding who speaks next.
Installing AutoGen
pip install pyautogen
A Working Two-Agent Conversation
The simplest real AutoGen pattern: an assistant agent and a user proxy agent, conversing until the task is done.
from autogen import AssistantAgent, UserProxyAgent
config_list = [{"model": "gpt-4o", "api_key": "YOUR_API_KEY"}]
assistant = AssistantAgent(
name="assistant",
llm_config={"config_list": config_list}
)
user_proxy = UserProxyAgent(
name="user_proxy",
human_input_mode="TERMINATE",
code_execution_config={"work_dir": "coding", "use_docker": False}
)
user_proxy.initiate_chat(
assistant,
message="Write a Python function that checks whether a number is prime, and test it on the numbers 17 and 18."
)
Here’s what actually happens: the assistant writes the function, the user proxy agent executes the code (because code_execution_config is set), and if it fails, the result goes back to the assistant to fix and retry — a real iterate-until-it-works loop, not a single one-shot generation.
human_input_mode Controls How Autonomous It Is
This single setting is the most important dial in AutoGen:
| Value | Behavior |
|---|---|
ALWAYS | Asks a human before every single message — maximum oversight, slowest. |
TERMINATE | Runs autonomously, only asking a human when the conversation seems to be ending — a reasonable default for most tasks. |
NEVER | Fully autonomous, no human checkpoints at all — use only for low-stakes, well-scoped tasks. |
Coordinating More Than Two Agents
For tasks needing several specialized agents, GroupChat manages the conversation flow among them:
from autogen import GroupChat, GroupChatManager
groupchat = GroupChat(
agents=[user_proxy, assistant, critic_agent],
messages=[],
max_round=10
)
manager = GroupChatManager(groupchat=groupchat, llm_config={"config_list": config_list})
user_proxy.initiate_chat(manager, message="Draft and critique a project proposal for X.")
The GroupChatManager decides which agent speaks next based on the conversation so far — useful for patterns like “one agent drafts, another critiques, a third finalizes,” without hand-coding the handoff logic yourself.
When AutoGen Is the Right Choice
AutoGen’s conversational model fits naturally when a task genuinely resembles back-and-forth discussion — drafting and critique, negotiation between agents with different objectives, or debugging loops that benefit from a code-execution agent trying, failing, and retrying. For a fixed, role-based pipeline (research, then write, then edit, in that order every time), CrewAI’s more structured task model is usually simpler to reason about; see our CrewAI tutorial for that comparison in practice.
Frequently Asked Questions
Is AutoGen safe to let execute code automatically?
Only with real guardrails — sandbox code execution (Docker is the standard recommendation), never point it at production systems, and keep human_input_mode away from NEVER for anything beyond low-stakes, well-understood tasks.
How many agents can realistically be in one GroupChat?
Technically many, but coordination quality degrades as the group grows — most real GroupChat setups stay in the 3-5 agent range, where the manager can still make sensible next-speaker decisions.
Is AutoGen only for coding tasks?
No — the code-execution example is common because it’s easy to verify correctness objectively, but the same conversational agent pattern works for research, writing, planning, or any task that benefits from multiple perspectives talking through a problem.
Conclusion
AutoGen’s conversational structure is its real differentiator — agents that message each other and a human moderator dialed in or out via human_input_mode, rather than a fixed pipeline of tasks. Start with the two-agent assistant/user-proxy pattern before reaching for GroupChat — it’s the fastest way to see the iterate-and-retry loop actually work before adding coordination complexity on top.
Keep reading
📑 About the author: I also build Digital Bizz Card — hosted digital business cards you can share with a QR code, no app required.


