← Back to tutorials

AutoGen 2.0 Multi-Agent in Action: From Single Agent to Collaborative Systems

A Complete Tutorial on Microsoft's Latest AutoGen Framework with Real Business Cases

AutoGen 2.0 Multi-Agent in Action

Why Multi-Agent?

A single agent faces two core problems with complex tasks:

  • Role Confusion: The same agent writes code and reviews it, leading to quality degradation.
  • Context Bloat: Long tasks fill up the context window, causing early information to be forgotten.
  • The multi-agent solution: a team of specialized experts, each doing one thing well.


    AutoGen Core Concepts

    Agent Types

    python
    from autogen import AssistantAgent, UserProxyAgent, GroupChat, GroupChatManager

    AssistantAgent: LLM-driven, speaks proactively

    UserProxyAgent: Represents the human, executes code, controls termination conditions

    Conversation Modes

    ModeDescriptionUse Case

    Two-AgentTwo agents talk directlySimple tasks GroupChatMultiple agents share a conversationMulti-role collaboration Nested ChatAgent spawns sub-dialogues internallyComplex subtask decomposition


    Installation & Configuration

    bash
    pip install pyautogen
    

    python
    config_list = [
        {"model": "gpt-4o", "api_key": "sk-..."},
        {"model": "claude-3-5-sonnet-20241022", "api_key": "sk-ant-...", "api_type": "anthropic"},
    ]

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


    Hands-On Case: Data Analysis Collaboration System

    Define Three Agents

    python
    

    1. Data Analyst: Understands data, plans analysis approach

    analyst = AssistantAgent( name="DataAnalyst", system_message="""You are a senior data analyst. Your responsibilities: analyze data structure, discover key trends, plan visualization strategies. Do not write code yourself; tell the Coder what code is needed.""", llm_config=llm_config )

    2. Coder: Writes Python code

    coder = AssistantAgent( name="Coder", system_message="""You are a Python data engineer. Your responsibilities: write pandas/matplotlib code based on the Analyst's instructions. Code must be runnable directly, including complete import statements.""", llm_config=llm_config )

    3. Reviewer: Checks code and conclusions for quality

    reviewer = AssistantAgent( name="Reviewer", system_message="""You are a QA reviewer. Check for: bugs in code, conclusions backed by data, clarity of visualizations. If you find issues, point them out clearly and request changes.""", llm_config=llm_config )

    4. Executor (represents the user, runs code)

    executor = UserProxyAgent( name="Executor", human_input_mode="NEVER", code_execution_config={"work_dir": "output", "use_docker": False}, max_consecutive_auto_reply=10 )

    Start GroupChat

    python
    groupchat = GroupChat(
        agents=[analyst, coder, reviewer, executor],
        messages=[],
        max_round=20,
        speaker_selection_method="auto"
    )

    manager = GroupChatManager(groupchat=groupchat, llm_config=llm_config)

    executor.initiate_chat( manager, message=""" Analyze sales_data.csv (contains: date, product category, sales amount, region) Output required:

  • Sales trend chart for the past 12 months
  • Pie chart of product category shares
  • Comparison of best/worst performing regions
  • 3 key business insights
  • """ )


    Common Issues & Solutions

    Issue 1: Agent gets stuck in infinite loop

    python
    def is_termination_msg(msg):
        return "ANALYSIS_COMPLETE" in msg.get("content", "")

    executor = UserProxyAgent( ... is_termination_msg=is_termination_msg )

    Issue 2: Code execution fails without handling

    In the system_message, explicitly require: if code execution errors, analyze the error cause and rewrite, retry up to 3 times.


    AutoGen vs LangGraph vs CrewAI

    FrameworkLearning CurveFlexibilityProduction ReadinessBest For

    AutoGenMediumHighHighCode execution scenarios LangGraphHigherHighestHighComplex state management CrewAILowMediumMediumRapid prototyping


    Further Reading

  • Multi-Agent Collaboration Patterns
  • LangGraph Stateful Agent
  • MCP Standardized Tool Integration
  • Also available in 中文.