ag2ai/ag2
AG2 (formerly AutoGen): The Open-Source AgentOS.Join us at: https://discord.gg/sNGSwQME3x
AG2: An Open-Source Framework for Building AI Agents
AG2 is a Python framework for creating AI agents that can work together to solve tasks. It's designed to make it easy to build, test, and deploy agentic AI systems — programs that use large language models (LLMs) to reason, call tools, and collaborate.
What AG2 Does
At its core, AG2 provides a Agent class. You give an agent a prompt (its instructions) and a model configuration (which LLM to use). Then you can ask it questions, and it replies. Agents can also be given tools — plain Python functions that the agent can call to get external data or perform actions. This lets agents go beyond just text generation.
AG2 also supports human-in-the-loop workflows: you can pause an agent's run to ask a person for approval or missing information. And for multi-agent scenarios, AG2 uses a Network — a hub that coordinates agents over typed channels (like a conversation channel or a workflow channel) — so multiple agents can collaborate systematically.
Key Concepts
- Agent – The basic building block. It talks to a model, can call tools, and returns replies.
- Tools – Regular Python functions decorated with
@tool. The agent decides when to call them; AG2 runs the function and feeds the result back. - Human in the Loop – A way to pause a run and get input from a person, via a configurable hook (e.g., a CLI prompt or a web UI).
- Network – A hub that manages multiple agents and typed channels (conversation, consulting, discussion, workflow) for orchestration.
- Agent Harness – Optional add-ons like persistent memory (knowledge store) and history compaction (summarizing old turns instead of discarding them).
Installation and Setup
AG2 requires Python 3.11 or later. Install it from PyPI with the extra for your model provider:
pip install ag2[openai] # for OpenAI
pip install 'ag2[anthropic]' # for Anthropic
pip install 'ag2[gemini]' # for Gemini
Set your API key as an environment variable (e.g., OPENAI_API_KEY), or pass it explicitly in the config.
Quick Example
import asyncio
from ag2 import Agent
from ag2.config import OpenAICompletionsConfig
agent = Agent(
"assistant",
prompt="You are a helpful assistant.",
config=OpenAICompletionsConfig(model="gpt-4o-mini"),
)
async def main():
reply = await agent.ask("Summarize the main differences between Python lists and tuples.")
print(reply.body)
asyncio.run(main())
AG2 is fully async: agent.ask() returns a reply, and you can call ask() on that reply to continue the same conversation.
AG2 Classic (the autogen namespace)
If you've used AutoGen or the older AG2, you might know ConversableAgent, GroupChat, or import autogen. That code now lives in a separate package called AG2 Classic (pip install ag2-classic). It's still maintained and your existing code keeps working. The current ag2 package (v1.0+) uses a new protocol-driven design with import ag2 and a different agent model. If you're starting fresh, use the new AG2; if you have existing Classic code, stay on Classic.
More Resources
AG2 is open-source and maintained by a group of volunteers. You can join the community on Discord.
Related
- Project
- Project
- Project
- Project
- Project