AutoGen Fundamentals
From the course AutoGen: Multi-Agent Conversations
What Is AutoGen?
AutoGen is Microsoft's framework for building multi-agent systems where AI agents converse with each other and humans to solve complex tasks.
Core Concepts
- ConversableAgent: Base agent that can send and receive messages
- AssistantAgent: AI-powered agent with LLM capabilities
- UserProxyAgent: Represents a human user, can execute code
- GroupChat: Multi-agent conversation with turn management
Installation
pip install autogen-agentchat
Two-Agent Chat
from autogen import AssistantAgent, UserProxyAgent
assistant = AssistantAgent(
name="assistant",
llm_config={"model": "gpt-4o", "api_key": os.environ["OPENAI_API_KEY"]},
)
user_proxy = UserProxyAgent(
name="user",
human_input_mode="NEVER", # Automated
code_execution_config={"work_dir": "output", "use_docker": False},
)
user_proxy.initiate_chat(
assistant,
message="Create a bar chart of the top 5 programming languages by popularity.",
)
How It Works
- User proxy sends the initial message
- Assistant generates a response (possibly with code)
- User proxy executes any code and returns the result
- Assistant refines based on execution output
- Loop continues until task is complete or max rounds reached
Common Pitfall: Setting human_input_mode="NEVER" in Production Without Safeguards
Running a fully automated agent (human_input_mode="NEVER") without termination conditions is dangerous. The agent can loop indefinitely, consuming API tokens until it hits a spending limit. Always set both max_consecutive_auto_reply and an is_termination_msg function:
user_proxy = UserProxyAgent(
name="user",
human_input_mode="NEVER",
max_consecutive_auto_reply=8, # Hard stop
is_termination_msg=lambda msg: "TASK_COMPLETE" in msg.get("content", ""),
code_execution_config={"work_dir": "output", "use_docker": True},
)
Instruct the assistant to output "TASK_COMPLETE" in its final message so the termination condition fires reliably.
Best Practice: Use Docker for Code Execution in All Non-Dev Environments
The default use_docker=False setting runs code in the host Python environment. This is convenient for development but dangerous in staging or production — a generated script could delete files or make unintended network calls. Enable Docker isolation for any deployment where the generated code is not fully trusted:
code_execution_config={
"work_dir": "agent_workspace",
"use_docker": "python:3.11-slim", # specific image for reproducibility
"timeout": 60,
}
Named Product Reference: Microsoft AutoGen Studio
Microsoft ships AutoGen Studio, a no-code visual interface for building and testing AutoGen workflows. It runs as a local web app (pip install autogenstudio, then autogenstudio ui) and lets you drag-and-drop agents, define tools, and observe multi-agent conversations in a browser. It's an excellent tool for prototyping workflows before writing the production Python code.
Key Takeaways
- •AutoGen enables agent-to-agent and agent-to-human conversations
- •AssistantAgent provides LLM-powered reasoning
- •UserProxyAgent can execute code and represent human users
- •Agents iterate through conversation until the task is complete
Progress is kept in this browser only.