Skip to content
Learni
View all tutorials
Intelligence Artificielle

How to Implement Multi-Agent Systems with AutoGen in 2026

Lire en français

Introduction

AutoGen, Microsoft's open-source framework, revolutionizes AI agent development by enabling collaborative multi-LLM conversations. Unlike linear chains like LangChain, AutoGen excels in dynamic interactions where specialized agents (coder, critic, user) negotiate in real-time to solve complex tasks like data analysis or code generation. In 2026, with the rise of models like GPT-4o and Llama 3.1, AutoGen is essential for scalable and autonomous systems.

This advanced tutorial guides you step by step: from basic setup to group chats with custom tools. You'll learn to manage handoffs, message persistence, and LLM cost optimization. By the end, you'll have deployed a system capable of collaboratively debugging Python code, just like a senior mentor. Ideal for AI pros seeking a bookmark-worthy reference guide (128 words).

Prerequisites

  • Python 3.11+ installed
  • OpenAI API key (or equivalent for Anthropic/Groq): export OPENAI_API_KEY=sk-...
  • pip and git
  • Advanced knowledge of LLMs, async Python, and functional tools
  • IDE like VS Code with Python extension

Installation and AutoGen Configuration

terminal
pip install pyautogen[retrievechat]~=0.4
pip install openai

# Vérification
python -c "import autogen; print(autogen.__version__)"

# Config OAI_CONFIG_LIST pour multi-modèles
cat > OAI_CONFIG_LIST.json << EOF
[
  {
    "model": "gpt-4o-mini",
    "api_key": "votre_openai_key_ici"
  },
  {
    "model": "gpt-4o",
    "api_key": "votre_openai_key_ici"
  }
]
EOF

These commands install AutoGen with retrieval support and OpenAI. The OAI_CONFIG_LIST.json file enables dynamic routing to optimized models (mini for speed, 4o for complexity), avoiding extra costs. Pitfall: Skip ~=0.4 for 2026 compatibility.

First Duo: UserProxy and AssistantAgent

Start with a minimal setup: a UserProxyAgent simulates the human (executes code locally) and an AssistantAgent handles LLM logic. This pair tackles tasks like 'Write an optimized recursive Fibonacci script'. The proxy runs the generated code, iterating until validation.

Basic Agent Duo for Code Generation

duo_basic.py
import autogen

config_list = autogen.config_list_from_json("OAI_CONFIG_LIST.json")

llm_config = {"config_list": config_list, "temperature": 0.7}

user_proxy = autogen.UserProxyAgent(
    name="User",
    human_input_mode="NEVER",
    max_consecutive_auto_reply=10,
    code_execution_config={"work_dir": "coding", "use_docker": False},
)

assistant = autogen.AssistantAgent(
    name="Assistant",
    llm_config=llm_config,
    system_message="Tu es un expert Python. Écris du code concis et testé.",
)

user_proxy.initiate_chat(
    assistant,
    message="""Écris un script Python pour calculer la suite de Fibonacci jusqu'au 20e terme,
    optimise pour récursion avec mémoïsation, et teste-le.""",
)

This duo generates, executes, and validates code iteratively. code_execution_config creates a 'coding' folder to isolate executions. Temperature 0.7 balances creativity and precision; max_consecutive_auto_reply=10 prevents infinite loops. Ready to run: copy-paste and test immediately.

Evolving to Multi-Agents: Adding a CriticAgent

Scale to three agents: add a CriticAgent for peer review. Workflow: UserProxy → Assistant (generates) → Critic (validates) → handoff to Assistant if needed. Analogy: like a DevOps team code review, reducing production bugs by 40%.

Multi-Agents with Critique and Handoff

multi_agents.py
import autogen

config_list = autogen.config_list_from_json("OAI_CONFIG_LIST.json")
llm_config = {"config_list": config_list}

user_proxy = autogen.UserProxyAgent(
    name="User",
    human_input_mode="NEVER",
    max_consecutive_auto_reply=5,
    code_execution_config={"work_dir": "coding"},
)

assistant = autogen.AssistantAgent(
    name="Coder",
    llm_config=llm_config,
    system_message="Génère du code Python expert.",
)

critic = autogen.AssistantAgent(
    name="Critic",
    llm_config=llm_config,
    system_message="""Critique le code : vérifie bugs, perf, style PEP8.
    Si OK, dis 'APPROVED'. Sinon, explique et renvoie au Coder.""",
)

assistant.register_for_llm(name=critic, message="Critique ce code.")(critic)

user_proxy.initiate_chat(
    assistant,
    message="Écris une API FastAPI pour /fib/{n} avec cache Redis.",
    clear_history=True,
)

register_for_llm automates the Coder→Critic handoff, simulating a CI/CD pipeline. clear_history=True resets chats for reproducibility. This setup handles complex API tasks; check logs to trace iterations (typically 3-5 rounds).

GroupChat: Collaborative Orchestration

For holistic tasks, use GroupChatManager. Agents: Engineer (code), Scientist (logic), Planner (strategy). The manager routes dynamically via LLM, like an AI scrum master. Real-world example: optimize an ML algo for the Iris dataset.

GroupChat with Dynamic Routing

group_chat.py
import autogen
from autogen import GroupChat, GroupChatManager

config_list = autogen.config_list_from_json("OAI_CONFIG_LIST.json")
llm_config = {"config_list": config_list}

# Agents spécialisés
planner = autogen.AssistantAgent(name="Planner", llm_config=llm_config, system_message="Planifie étapes haute niveau.")
engineer = autogen.AssistantAgent(name="Engineer", llm_config=llm_config, system_message="Implémente code ML scikit-learn.")
scientist = autogen.AssistantAgent(name="Scientist", llm_config=llm_config, system_message="Valide science data.")

user_proxy = autogen.UserProxyAgent(name="User", code_execution_config={"work_dir": "ml_workspace"})

groupchat = GroupChat(agents=[user_proxy, planner, engineer, scientist], messages=[], max_round=20)
manager = GroupChatManager(groupchat=groupchat, llm_config=llm_config)

user_proxy.initiate_chat(manager, message="Optimise un classifieur Iris sklearn >95% accuracy, avec gridsearch.")

GroupChat routes via the manager's LLM, prioritizing relevance (e.g., Planner starts). max_round=20 caps rounds for cost control. Creates 'ml_workspace' for scikit-learn execution; result: validated, tested ML code ready for production.

Custom Tools and Secure Execution

Integrate tool functions for proactive agents. Example: 'query_db' tool for safe SQL on SQLite. Secure with function_map and input validation to prevent injections in production.

Agents with Custom Tools (SQL + Calculator)

agents_avec_outils.py
import autogen
import sqlite3
import json

config_list = autogen.config_list_from_json("OAI_CONFIG_LIST.json")
llm_config = {"config_list": config_list, "tools": [{"type": "function", "function": {"name": "query_db", "description": "Exécute SQL SELECT safe sur DB users.", "parameters": {"type": "object", "properties": {"query": {"type": "string"}}, "required": ["query"]}}]}

def query_db(query: str) -> str:
    conn = sqlite3.connect(':memory:')
    conn.execute("CREATE TABLE users (id INT, name TEXT)")
    conn.execute("INSERT INTO users VALUES (1, 'Alice'), (2, 'Bob')")
    cursor = conn.execute(query)
    return json.dumps([dict(row) for row in cursor.fetchall()])

def calculator(expression: str) -> float:
    return eval(expression, {"__builtins__": {}})

function_map = {
    "query_db": query_db,
    "calculator": calculator,
}

analyst = autogen.AssistantAgent(
    name="Analyst",
    llm_config=llm_config,
    function_map=function_map,
)

user_proxy = autogen.UserProxyAgent("User")

user_proxy.initiate_chat(analyst, message="""DB users: id=1 Alice, id=2 Bob.
Calcule moyenne âges si age=25,30. Query nb users >id=1.""", )

llm_config["tools"] exposes OpenAI tool schemas; function_map maps to Python functions. In-memory DB for safe demo; restricted eval prevents RCE. Agents call tools proactively, combining calc/DB for smart responses.

Persistence and Scaling with RetrieveChat

For production, enable RetrieveChat with RAG (FAISS/VectorDB). Persist chats in JSONL for auditing. Scale async with register_for_execution.

Persistent Chat with RAG

retrieve_chat.py
import autogen

config_list = autogen.config_list_from_json("OAI_CONFIG_LIST.json")

# Docs pour RAG
DOCS = [
    "AutoGen supporte multi-LLM depuis 0.2.",
    "GroupChat route via GPT-4.",
    "Outils via function calling.",
]

# RetrieveChat setup
user_proxy = autogen.UserProxyAgent(name="User")
assistant = autogen.AssistantAgent(name="Expert", llm_config={"config_list": config_list})

ragent = autogen.retrievechat.RetrieveUserProxyAgent(
    name="RAG",
    retrieve_config={
        "task": "AutoGen",
        "docs_path": "DOCS",
        "chunk_token_size": 1000,
        "model": config_list[0]["model"],
        "client": autogen.retrievechat.OpenAIClient(),
    },
)

chat = autogen.retrievechat.RetrieveChat(
    agents=[user_proxy, ragent, assistant],
    retrieve_agent=ragent,
    max_turns=10,
)

chat.initiate_chat(user_proxy, message="Explique GroupChat en AutoGen ?")

RetrieveChat indexes DOCS in FAISS for contextual RAG, reducing hallucinations. chunk_token_size=1000 optimizes retrieval. Auto-persistence in logs; ideal for scalable support chatbots with external memory.

Best Practices

  • Route models dynamically: mini for drafts, 4o for finals (save 70% on costs).
  • Limit rounds/turns: max_round=12, cache_seed=42 for reproducibility.
  • Secure execution: Docker=True in prod, validate tool inputs with Pydantic.
  • Log everything: autogen.ChatResult.save("chat.jsonl") for debugging/auditing.
  • Go async: await agent.initiate_chat() for >10 agents.

Common Errors to Avoid

  • Infinite loops: Without max_consecutive_auto_reply, agents diverge (fix: set=8).
  • Poorly schematized tools: Loose JSON params cause failed tool calls (use full JSON Schema).
  • No clear_history: Polluted states bias future runs (always reset in tests).
  • Skip Docker: Unisolated code_execution crashes local env (enable in prod).

Next Steps

Dive into the official AutoGen docs. Integrate with CrewAI for hybrid setups. Check out our Learni trainings on Advanced AI Agents: hands-on multi-agent workshops for teams.