Multi-Agent Frameworks
Introduction
Multi-agent frameworks provide the infrastructure for building multi-agent systems, encapsulating the underlying logic of communication, collaboration, and task allocation. This section introduces the most popular LLM multi-agent frameworks.
AutoGen (Microsoft)
Overview
AutoGen is Microsoft's multi-agent conversation framework, with "Conversable Agent" as its core concept.
Core Concepts
- ConversableAgent: Base class for agents that can converse with other agents
- AssistantAgent: Agent that uses an LLM to generate replies
- UserProxyAgent: Agent representing the user, capable of executing code
- GroupChat: Group chat among multiple agents
Code Example
from autogen import AssistantAgent, UserProxyAgent, GroupChat, GroupChatManager
# Define agents
coder = AssistantAgent(
name="Coder",
system_message="You are a Python expert. Write clean, efficient code.",
llm_config={"model": "gpt-4o"},
)
reviewer = AssistantAgent(
name="Reviewer",
system_message="You are a code review expert. Review code quality, security, and performance.",
llm_config={"model": "gpt-4o"},
)
tester = AssistantAgent(
name="Tester",
system_message="You are a test engineer. Write comprehensive test cases.",
llm_config={"model": "gpt-4o"},
)
user_proxy = UserProxyAgent(
name="User",
human_input_mode="NEVER",
code_execution_config={"work_dir": "coding_output"},
)
# Create group chat
group_chat = GroupChat(
agents=[user_proxy, coder, reviewer, tester],
messages=[],
max_round=20,
)
manager = GroupChatManager(groupchat=group_chat, llm_config={"model": "gpt-4o"})
# Start conversation
user_proxy.initiate_chat(
manager,
message="Please implement an LRU cache with comprehensive tests."
)
AutoGen Features
| Feature | Description |
|---|---|
| Conversation-driven | Agents collaborate through natural language dialogue |
| Code execution | Built-in secure code execution environment |
| Flexible dialogue flow | Supports two-person dialogue, group chat, nested conversations |
| Human involvement | Supports human-in-the-loop |
| Tool calling | Agents can register and use tools |
CrewAI
Overview
CrewAI adopts a role-playing paradigm, organizing multi-agent collaboration as a "Crew."
Core Concepts
- Agent: An intelligent agent with a role, goal, and backstory
- Task: A specific task to be completed
- Crew: A combination of Agents and Tasks
- Process: Execution mode (sequential / hierarchical)
Code Example
from crewai import Agent, Task, Crew, Process
# Define agents
researcher = Agent(
role="Research Analyst",
goal="Conduct in-depth research on a given topic and provide comprehensive analysis",
backstory="You are an experienced research analyst skilled in information gathering and deep analysis.",
tools=[search_tool, scrape_tool],
llm="gpt-4o",
verbose=True,
)
writer = Agent(
role="Technical Writer",
goal="Transform research findings into clear, engaging articles",
backstory="You are a professional technical writer skilled at expressing complex concepts in accessible language.",
llm="gpt-4o",
)
editor = Agent(
role="Editor",
goal="Ensure article quality, check factual accuracy and readability",
backstory="You are a rigorous editor with extremely high standards for quality.",
llm="gpt-4o",
)
# Define tasks
research_task = Task(
description="Research the latest developments, key technologies, and market trends in {topic}.",
expected_output="A comprehensive research report including key findings and supporting data.",
agent=researcher,
)
writing_task = Task(
description="Write a technical article based on the research report.",
expected_output="A well-structured, content-rich technical article of about 2000 words.",
agent=writer,
context=[research_task], # Depends on research task output
)
editing_task = Task(
description="Review and edit the article to ensure quality standards are met.",
expected_output="A reviewed and finalized article.",
agent=editor,
context=[writing_task],
)
# Create crew
crew = Crew(
agents=[researcher, writer, editor],
tasks=[research_task, writing_task, editing_task],
process=Process.sequential, # Sequential execution
verbose=True,
)
# Execute
result = crew.kickoff(inputs={"topic": "LLM Agent Technology"})
print(result)
MetaGPT
Overview
MetaGPT (Hong et al., 2023) introduces Standard Operating Procedures (SOPs) into multi-agent collaboration, simulating a software company's development process.
Core Philosophy
- Agents are constrained by SOPs rather than free-form conversation
- Structured output formats (PRD, design documents, code, etc.)
- Clear role definitions and division of responsibilities
Role Definitions
# MetaGPT predefined roles
roles = {
"ProductManager": {
"goal": "Create a successful product",
"constraints": "Use the same language as user requirements",
"actions": ["WritePRD"],
},
"Architect": {
"goal": "Design a concise and usable system",
"constraints": "Use existing APIs/libraries",
"actions": ["WriteDesign"],
},
"ProjectManager": {
"goal": "Improve team efficiency",
"actions": ["WriteTasks"],
},
"Engineer": {
"goal": "Write elegant, modular code",
"actions": ["WriteCode"],
},
"QAEngineer": {
"goal": "Ensure quality",
"actions": ["WriteTest"],
},
}
Workflow
graph LR
U[User Requirements] --> PM[Product Manager<br/>Write PRD]
PM --> ARCH[Architect<br/>System Design]
ARCH --> PJM[Project Manager<br/>Task Breakdown]
PJM --> ENG[Engineer<br/>Write Code]
ENG --> QA[QA Engineer<br/>Write Tests]
QA --> OUTPUT[Deliverables]
OpenAI Swarm
Overview
OpenAI Swarm is a lightweight multi-agent orchestration framework whose core concept is "Handoff" between agents.
Core Concepts
- Agent: A conversational entity with instructions and tools
- Handoff: An agent transfers conversation control to another agent
- Context Variables: Context shared across agents
Code Example
from swarm import Swarm, Agent
client = Swarm()
# Define agents
triage_agent = Agent(
name="Triage Agent",
instructions="You are a customer service triage agent. Route users to the appropriate specialist based on their question.",
)
sales_agent = Agent(
name="Sales Agent",
instructions="You are a sales specialist. Help users learn about products and make purchases.",
)
support_agent = Agent(
name="Technical Support Agent",
instructions="You are a technical support specialist. Help users resolve technical issues.",
)
# Define handoff functions
def transfer_to_sales():
"""Transfer to sales"""
return sales_agent
def transfer_to_support():
"""Transfer to technical support"""
return support_agent
triage_agent.functions = [transfer_to_sales, transfer_to_support]
# Run
response = client.run(
agent=triage_agent,
messages=[{"role": "user", "content": "I'd like to learn about your enterprise pricing"}]
)
# triage_agent will automatically hand off to sales_agent
Swarm Features
- Extremely lightweight (single-file implementation)
- Suitable for simple routing and task division scenarios
- Stateless design
- Primarily for educational/experimental purposes
LangGraph Multi-Agent
Building Multi-Agent Systems with LangGraph
from langgraph.graph import StateGraph, END
class MultiAgentState(TypedDict):
messages: list
current_agent: str
task_status: str
# Define agent nodes
def researcher_node(state):
# Researcher agent logic
result = research_agent.invoke(state["messages"])
return {"messages": state["messages"] + [result], "current_agent": "writer"}
def writer_node(state):
result = writer_agent.invoke(state["messages"])
return {"messages": state["messages"] + [result], "current_agent": "reviewer"}
def reviewer_node(state):
result = reviewer_agent.invoke(state["messages"])
if "approved" in result.lower():
return {"messages": state["messages"] + [result], "task_status": "done"}
return {"messages": state["messages"] + [result], "current_agent": "writer"}
# Build graph
graph = StateGraph(MultiAgentState)
graph.add_node("researcher", researcher_node)
graph.add_node("writer", writer_node)
graph.add_node("reviewer", reviewer_node)
# Define edges
graph.set_entry_point("researcher")
graph.add_edge("researcher", "writer")
graph.add_edge("writer", "reviewer")
graph.add_conditional_edges("reviewer",
lambda s: END if s["task_status"] == "done" else "writer"
)
app = graph.compile()
result = app.invoke({"messages": [initial_task], "current_agent": "researcher", "task_status": ""})
Framework Comparison
| Feature | AutoGen | CrewAI | MetaGPT | Swarm | LangGraph |
|---|---|---|---|---|---|
| Collaboration mode | Conversation-driven | Role-task | SOP-driven | Handoff | Graph workflow |
| Complexity | Medium | Low | High | Very low | Medium |
| Flexibility | High | Medium | Low | Low | High |
| Code execution | Built-in | Via tools | Built-in | Via functions | Via nodes |
| Human involvement | Supported | Supported | Limited | Limited | Supported |
| Production-ready | Medium | Medium | Low | Experimental | High |
| State management | Conversation history | Context passing | Document flow | Stateless | Graph state |
Selection Guide
| Scenario | Recommended Framework | Reason |
|---|---|---|
| Rapid prototyping | CrewAI | Simple and intuitive API |
| Conversational collaboration | AutoGen | Flexible dialogue management |
| Software development simulation | MetaGPT | Built-in development SOPs |
| Simple routing/division | Swarm | Extremely lightweight |
| Complex workflows | LangGraph | Precise process control |
| Production environment | LangGraph / AutoGen | Maturity and reliability |
Practical Recommendations
Common Pitfalls
- Over-engineering: Simple tasks don't need multi-agent; a single agent suffices
- Communication explosion: Excessive dialogue between agents consumes tokens and time
- Unclear responsibilities: Responsibility boundaries between agents must be clear
- Missing termination conditions: Agents may enter infinite conversation loops
When to Use Multi-Agent
- Task involves multiple distinctly different domains of expertise
- Quality check/review stages are needed
- Task can be naturally decomposed into independent subtasks
- Multi-perspective analysis or debate is needed
When Not to Use Multi-Agent
- Simple single-step tasks
- Latency-sensitive scenarios
- Limited budget (multi-agent consumes more tokens)
References
- Wu, Q., et al. (2023). "AutoGen: Enabling Next-Gen LLM Applications via Multi-Agent Conversation"
- Hong, S., et al. (2023). "MetaGPT: Meta Programming for Multi-Agent Collaborative Framework"
- CrewAI Documentation (crewai.com)
- OpenAI Swarm GitHub Repository