Tutorials

How to Use AG2 (Formerly AutoGen) for Multi-Agent AI Workflows

Build multi-agent AI workflows with AG2 — run your first agent, give it a tool, add a human approval step, and coordinate agents on a Network.

Business team collaborating around a laptop, illustrating multi-agent teamwork in the AG2 tutorial
Image: Pixabay / Pixabay Content License
Time Required
About 1 Hour
Difficulty
Intermediate (Python / Async)
Tools Covered
AG2 + OpenAI / Anthropic / Ollama

What You'll Learn (Key Takeaways)

  • AG2 is the successor to AutoGen: a free, open-source Python framework for building AI agents and letting multiple agents cooperate (import ag2; the original AutoGen code lives on as AG2 Classic).
  • Install with pip install ag2[openai] (plus extras for anthropic, gemini, ollama) and set your provider API key in an environment variable.
  • AG2 is async throughout: run a first agent with Agent.ask(...), give it tools with @tool, and add a human approval step with context.input(...) and a hitl_hook.
  • Coordinate multiple agents with a Network and Hub (registry, write-ahead log, audit trail) — the replacement for the Classic GroupChat pattern.
  • Bottom line: a strong pick if your small business has a developer and a workflow where two or three specialised agents can work together.

What it is

AG2 is an open-source programming framework for building AI agents and letting multiple agents cooperate to solve tasks. It is the project formerly known as AutoGen: the repository ag2ai/ag2 (5,000 stars on GitHub, checked 2026-10-17) now ships the new AG2 v1.0 framework under the import ag2 name, while the original AutoGen-style code lives on as AG2 Classic in a separate repository. In plain terms, AG2 gives a small business a way to build small teams of AI agents — one that researches, one that reviews, one that drafts — instead of relying on a single chatbot prompt. Agents can talk to each other, call tools such as plain Python functions, use various large language models (LLMs), and pause for human approval. If you already know the idea of AI agents for small business, AG2 is one of the open-source frameworks you can use to build them yourself.

One important warning from the README: AG2 v1.0 is not a drop-in upgrade from Classic. The agent model, orchestration, and imports all changed. If your code uses import autogen, ConversableAgent, GroupChat, or UserProxyAgent, you are on Classic — stay on it and use the Classic docs. The rest of this tutorial covers AG2 v1.0 (import ag2).

Who it is for / SMB use case

AG2 is for small businesses with at least one person comfortable with Python and a repeatable workflow worth automating. It is a developer tool, not a no-code app. Practical SMB use cases include:

  • Lead triage: one agent reads incoming enquiries, a second checks them against your service criteria, and a third drafts a reply for a human to approve.
  • Content production: a researcher agent gathers facts, a writer agent drafts, and a reviewer agent checks the draft against your style rules before a human publishes.
  • Internal Q&A: an agent with access to your own documents answers staff questions, with a human-in-the-loop step before anything is sent to a customer.

The human-in-the-loop pattern matters most: a tool can pause and ask a person for confirmation — before publishing or sending a quote — so automation speeds up work without removing the owner from risky decisions.

Installation

AG2 requires Python 3.10 or newer and works on Windows, macOS, and Linux. Install it from PyPI with the extra that matches your model provider:

pip install ag2[openai]

On macOS, quote the extra because of how the shell handles brackets:

pip install 'ag2[openai]'

Other provider extras follow the same pattern, for example ag2[anthropic], ag2[gemini], and ag2[ollama]. Minimal dependencies are installed by default, so install only the extra you need. Next, set your API key as an environment variable so it is never hardcoded or checked into your code:

export OPENAI_API_KEY="YOUR_KEY_HERE"

Each provider config reads its standard environment variable. You can also pass a key explicitly when you create a config — see the AG2 documentation for the full provider list.

How to use

Step 1: Run your first agent

AG2 is async throughout. Agent.ask(...) starts a turn and returns a reply whose text is in reply.body. Save this as first_agent.py and run it with python first_agent.py:

import asyncio
from ag2 import Agent
from ag2.config import OpenAIConfig

agent = Agent(
    "assistant",
    prompt="You are a helpful assistant.",
    config=OpenAIConfig(model="gpt-4o-mini"),
)

async def main() -> None:
    reply = await agent.ask("Summarize the main differences between Python lists and tuples.")
    print(reply.body)

asyncio.run(main())

Step 2: Give the agent a tool

Tools are plain Python functions decorated with @tool. The model decides when to call the tool, AG2 runs it, and the result is fed back into the conversation:

import asyncio
from datetime import datetime
from ag2 import Agent, tool
from ag2.config import OpenAIConfig

@tool
async def get_weekday(date_string: str) -> str:
    """Get the day of the week for a given date, formatted as YYYY-MM-DD."""
    return datetime.strptime(date_string, "%Y-%m-%d").strftime("%A")

date_agent = Agent(
    "date_agent",
    prompt="You find the day of the week for a given date.",
    config=OpenAIConfig(model="gpt-4o-mini"),
    tools=[get_weekday],
)

async def main() -> None:
    reply = await date_agent.ask("I was born on 1995-03-25, what day was it?")
    print(reply.body)

asyncio.run(main())

For a business, replace the toy function with a real one: look up a price list, check stock, or fetch a booking record. Keep each tool narrow.

Step 3: Add a human approval step

Call context.input(...) inside a tool to pause the run and ask a person. Your hitl_hook decides how that question is answered — a CLI prompt, a web UI, or a queue. Use this pattern before any action that is hard to undo: sending an email, publishing a page, or charging a customer.

Step 4: Coordinate multiple agents with a Network

When two or more agents need to work together, AG2 uses the Network: a Hub that owns the registry, the write-ahead log, and the audit trail, with agents talking over typed channels. This replaces the Classic GroupChat pattern. Start with two agents — a planner and a reviewer — on one channel, and check the docs for the full Network example.

Step 5: Test with a real business task

Pick one small, measurable workflow, run it with a human reviewing every output, and only then widen the scope.

Pricing / Limitations

AG2 itself is free open-source software under the Apache 2.0 license — there is no per-seat fee for the framework. What you pay for is the LLM behind it: API usage billed by providers such as OpenAI, Anthropic, or Google. Check your provider’s pricing page for current rates; the README does not state pricing. Limitations to plan around:

  • Developer tool: you need Python skills to build and maintain it. Non-technical teams should pair with a developer or freelancer.
  • Breaking change: v1.0 is not a drop-in upgrade from Classic. Do not mix import autogen and import ag2 code.
  • Model dependence: output quality, speed, and cost all depend on the model you choose, and on how carefully you write agent prompts and tools.
  • Review still matters: agents can be wrong with confidence. Keep human approval on anything customer-facing.

Bottom line for SMB

AG2 is a strong pick if your small business has a developer and a workflow where two or three specialized agents plus one human approval step would save real hours every week. Start with one agent and one tool, prove it on a real task, and grow from there. If you need a no-code tool instead, look at the prompt templates and automation tools covered elsewhere on this site before committing to code. Full API reference and migration guides: docs.ag2.ai.

FAQ

Is AG2 the same as AutoGen?

AG2 is the successor to AutoGen. The new framework is imported as ag2, and the original AutoGen-style framework continues as AG2 Classic (import autogen, installed with pip install ag2-classic).

Do I need to pay for AG2?

No. AG2 is open source under the Apache 2.0 license. You pay your LLM provider for API usage, not for the framework itself.

Can AG2 work without a human in the loop?

Technically yes, but for a small business it is risky. Use the human-in-the-loop pattern for anything that reaches customers, publishes content, or moves money.

Frequently Asked Questions

Is AG2 the same as AutoGen?

AG2 is the successor to AutoGen. The new framework is imported as ag2, and the original AutoGen-style framework continues as AG2 Classic (installed with pip install ag2-classic).

Do I need to pay for AG2?

No. AG2 is open source under the Apache 2.0 license. You pay your LLM provider for API usage, not for the framework.

Can AG2 work without a human in the loop?

Technically yes, but for a small business it is risky. Use the human-in-the-loop pattern for anything customer-facing.

WebGool Tech Senior AI & Small Business Editor
Verified Contributor

Keep Reading

More in Tutorials

Explore all

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top