Master AG2 (AutoGen Evolved) with our step-by-step tutorial, detailed feature walkthrough, and expert tips.
Install AG2 via pip by running 'pip install ag2' in your terminal (Python
9 or higher required). Use 'pip install ag2[openai]' for OpenAI support, 'pip install ag2[retrievechat]' for RAG, or 'pip install ag2[docker]' for sandboxed code execution. Set up your LLM API keys by creating an OAI_CONFIG_LIST JSON file in your project root containing your model name, API key, and optional base URL. Alternatively, configure inline via a Python dict passed to llm_config when creating agents. Create your first two
agent conversation by importing AssistantAgent and UserProxyAgent from ag2, defining each with a name and llm_config, then calling user_proxy.initiate_chat(assistant, message='Your task here') to start the dialogue. Explore working examples at github.com/ag2ai/build
ag2 covering group chats, tool registration, RAG
powered conversations, code execution workflows, and sequential multi
step pipelines to learn common patterns. Join the AG2 Discord community at discord.gg/pAbnFJrkgZ for troubleshooting help and real
time discussion with maintainers, and check the official documentation at docs.ag
ai for API references and tutorials.
💡 Quick Start: Follow these 8 steps in order to get up and running with AG2 (AutoGen Evolved) quickly.
Explore the key features that make AG2 (AutoGen Evolved) powerful for multi-agent builders workflows.
AG2 provides four distinct collaboration patterns — swarms for parallel task distribution, sequential chains for pipeline workflows, group chats with LLM-based automatic speaker selection for multi-party collaboration, and nested conversations for hierarchical agent compositions. The GroupChatManager coordinates turn-taking and routing, while developers can customize speaker selection logic, termination conditions, and maximum turn limits for each pattern.
AG2 uniquely bridges agent ecosystems by connecting agents built in CrewAI, LangChain, LlamaIndex, and other frameworks into unified conversations through standardized A2A and MCP protocols. This cross-framework orchestration means teams are not locked into a single agent framework — they can leverage the best tools from each ecosystem while AG2's AgentOS runtime handles message routing, state synchronization, and lifecycle management across heterogeneous agent populations.
The UserProxyAgent class provides granular control over human involvement in agent conversations. Developers configure human_input_mode to ALWAYS (human approves every message), TERMINATE (human intervenes only at conversation end), or NEVER (fully autonomous). The agent can relay messages to humans, execute code on their behalf, and seamlessly transition between autonomous operation and human oversight at any conversation turn.
Agents can write, execute, and iteratively debug Python code within sandboxed environments. AG2 supports LocalCommandLineCodeExecutor for development, DockerCommandLineCodeExecutor for production-safe isolation, and Jupyter-based execution for notebook-style workflows. Configurable timeouts, allowed languages, and working directories give developers control over the execution environment while preventing runaway processes.
Register any Python function as an agent tool with automatic schema generation using simple decorators. The @register_for_llm decorator exposes a tool to an agent's LLM for function calling, while @register_for_execution designates which agent runs the function. This separation of calling and execution enables secure patterns where one agent decides to use a tool but a different, authorized agent performs the action. LangChain tool adapters provide additional interoperability.
Built-in RAG capabilities allow agents to ingest, index, and reason over external documents during conversations. The RetrieveUserProxyAgent handles document chunking, embedding generation, and vector store queries (with native ChromaDB support and adapters for Pinecone and Weaviate). Agents automatically retrieve relevant passages and inject them as context, enabling document-grounded conversations without requiring separate RAG infrastructure.
Every agent inherits from the ConversableAgent base class, providing a unified interface for sending and receiving messages, maintaining conversation history, and registering reply functions. This architecture means any agent can talk to any other agent, conversations are composable (a two-agent chat can be nested inside a group chat), and new agent types are created by subclassing and overriding reply logic rather than implementing complex interfaces.
AG2 integrates with all major LLM providers — OpenAI GPT-4 and GPT-4o, Anthropic Claude, Google Gemini, Mistral, Azure OpenAI, and local open-weight models via Ollama and LM Studio. Different agents in the same conversation can use different models, enabling cost optimization by routing simple tasks to cheaper models while reserving expensive frontier models for complex reasoning. Model failover chains automatically switch providers if a primary model is unavailable.
Define strict output schemas using Pydantic models, enforce JSON response formats, and set precise termination conditions to maintain control over agent conversations. Developers can specify max_turns limits, custom termination keywords, reply function overrides, and conversation hooks that trigger at specific points in the dialogue flow. This ensures agent conversations produce machine-parseable results and terminate predictably.
The shared state system provides a centralized context store accessible to all agents in a conversation. In sequential chats, the carryover mechanism automatically passes relevant context from one conversation stage to the next. Context variables can be set, read, and updated by any participating agent, enabling coordination without requiring agents to explicitly pass information through conversation messages. AgentOS extends this with persistent state across sessions.
AG2 is the community-governed evolution of Microsoft's original AutoGen project. In late 2024, the original AutoGen creators forked the project as AG2 under the ag2ai organization, continuing the proven conversable-agent architecture from AutoGen 0.2.x. Meanwhile, Microsoft launched a separate AutoGen v0.4 with a completely different event-driven/actor-based architecture that breaks backward compatibility. AG2 preserves API compatibility with AutoGen 0.2.x — most existing code works by simply changing the import — while adding new features like AgentOS, cross-framework interoperability, and swarm orchestration. Both projects are open-source under Apache 2.0, but they have diverged significantly in design philosophy and governance.
Yes. The AG2 framework is released under the Apache 2.0 license, which permits commercial use, modification, and distribution without licensing fees or royalties. You can build and sell products using AG2 without paying AG2 anything. Your costs are limited to the LLM API fees from your chosen provider (OpenAI, Anthropic, etc.) and any infrastructure costs for hosting your agents. The paid AgentOS tier is optional and only needed if you want managed hosting, enterprise SSO, persistent state management, and other production-grade features.
Yes, AG2 is a Python-first framework that requires intermediate programming knowledge. You will write Python code to define agents, configure conversation patterns, register tools, and set up workflows. There is no visual builder, drag-and-drop interface, or low-code option in the open-source framework. AG2 Studio (part of the enterprise AgentOS offering) aims to provide a visual designer, but the core framework is code-only. If you are not comfortable writing Python, consider CrewAI for a slightly simpler API or a no-code platform like Relevance AI.
AG2 offers more orchestration flexibility with four distinct conversation patterns (two-agent, sequential, group chat, nested chat) compared to CrewAI's sequential and hierarchical process model. AG2's conversable-agent architecture lets agents engage in natural back-and-forth dialogue, while CrewAI uses a more structured role-and-task abstraction. AG2 includes built-in Docker-sandboxed code execution and a native UserProxyAgent for human-in-the-loop, whereas CrewAI requires external setup for code execution. However, CrewAI is faster to get started with for straightforward role-based agent teams due to its more opinionated design. AG2 is the better choice when you need complex conversation flows, cross-framework interoperability, or fine-grained control over agent interactions.
Yes. AG2 has a robust tool registration system where any Python function can be registered as an agent-callable tool using decorators. The framework automatically generates the tool schema from the function signature and docstring, which is passed to the LLM for function calling. Tools can be registered to specific agents for calling (via register_for_llm) and specific agents for execution (via register_for_execution), giving you fine-grained control. AG2 also supports LangChain tool adapters for interoperability and MCP integration for connecting to external tool servers.
Multi-agent conversations can generate significant LLM API costs because each agent interaction involves token-consuming API calls. Best practices include: setting max_turns or max_consecutive_auto_reply limits to prevent runaway conversations; using cheaper models (GPT-3.5, Haiku) for simple routing agents while reserving expensive models (GPT-4, Opus) for complex reasoning; implementing clear termination conditions so conversations end when goals are met; monitoring token usage via the built-in usage_summary tracking; using caching to avoid repeated identical LLM calls; and starting with two-agent patterns before scaling to larger group chats to understand cost profiles.
Now that you know how to use AG2 (AutoGen Evolved), it's time to put this knowledge into practice.
Sign up and follow the tutorial steps
Check pros, cons, and user feedback
See how it stacks against alternatives
Follow our tutorial and master this powerful multi-agent builders tool in minutes.
Tutorial updated March 2026