AutoGen's core concept
AutoGen (AG2) frames agentic AI as a multi-party conversation. Every participant — whether an LLMLLMLarge Language Model — a neural network trained on vast amounts of text data that can understand and generate human-quality language across a wide range of tasks.Learn more →, a code executor, or a human — is a 'ConversableAgent'. Agents send messages to each other, and the conversation continues until a termination condition is met.
The most common pattern is a two-agentAgentAn LLM-powered system that can take actions, use tools, and pursue multi-step goals autonomously without human input at each step.Learn more → setup: an AssistantAgent (backed by an LLM) that writes code or analyses problems, and a UserProxyAgent that executes the code and reports results back. This loop — plan, execute, observe, revise — handles complex programming tasks autonomously.
Install AutoGen
Install the package: `pip install pyautogen`. For the full feature set including code execution: `pip install pyautogen[docker]`. AutoGen uses Docker to sandbox code execution safely — install Docker Desktop if you don't have it.
Configure your LLM: create a `config_list` as a Python list of dicts: `[{"model": "gptGPTGenerative Pre-trained Transformer — the model architecture and family name behind OpenAI's most famous models, from GPT-2 to GPT-5.Learn more →-4o", "api_key": os.environ["OPENAI_API_KEY"]}]`. You can include multiple models in the list and AutoGen will try them in order if one fails.
Create a two-agent code workflow
Create the assistant: `assistant = AssistantAgent(name='assistant', llm_config={"config_list": config_list})`. Create the user proxy with code execution enabled: `user_proxy = UserProxyAgent(name='user_proxy', human_input_mode='NEVER', code_execution_config={"work_dir": 'coding', "use_docker": False}, max_consecutive_auto_reply=10)`.
Start a conversation: `user_proxy.initiate_chat(assistant, message='Write a Python script that fetches the top 10 Hacker News stories and saves them to a JSON file.')`. AutoGen will have the assistant write code, the proxy execute it, feed back any errors, and iterate until the task succeeds.
GroupChat for multi-agent conversations
For more than two agents, use GroupChat: `groupchat = GroupChat(agents=[planner, researcher, coder, reviewer], messages=[], max_round=12)`. A GroupChatManager LLM decides which agent speaks next based on the conversation history.
Define agent roles clearly in their system messages. The planner decomposes the task, the researcher gathers information, the coder writes the solution, and the reviewer checks for bugs. Each agent's system message should specify when it should and should not speak.
Using local models with AutoGen
AutoGen supports any OpenAI-compatible API. To use Ollama: `config_list = [{"model": "llama3.3:70b", "base_url": "http://localhost:11434/v1", "api_key": "ollama"}]`. Some features (function callingFunction CallingA capability that allows language models to return structured calls to developer-defined functions or tools, enabling AI systems to interact with external APIs, databases, and services.Learn more →, JSON modeJSON ModeA model setting that constrains LLM output to syntactically valid JSON format, ensuring parseable responses without full schema validation.Learn more →) require models that support them — use a capable model like Llama 3.3 70B or Qwen 3 32B for code tasks.
Local models work well for the UserProxyAgent's simple tasks (executing code, summarising output) but may struggle with complex planningPlanningAn agent's decomposition of a goal into an ordered set of steps or subgoals before or while acting to achieve complex objectives systematically.Learn more →. A hybrid setup — local model for simple agents, cloud model for the planner — balances cost and capability.