Agents·7 min read

How to build conversational agents with AutoGen

Microsoft AutoGen enables multi-agent conversations where AI agents and human proxies collaborate to solve tasks. This guide covers setting up AutoGen, creating conversable agents, and running code-generation workflows.

AutoGen's core concept

AutoGen (AG2) frames agentic AI as a multi-party conversation. Every participant — whether an LLM, 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-agent 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": "gpt-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 calling, JSON mode) 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 planning. A hybrid setup — local model for simple agents, cloud model for the planner — balances cost and capability.