Multi-Agent Systems — Agents Collaborating
Complex tasks benefit from multiple specialized agents working together. AutoGen and CrewAI provide frameworks for orchestrating multi-agent pipelines: a planner agent breaks down tasks, specialist agents execute, a critic agent reviews results. This mimics how human teams collaborate.
Multi-Agent Pipeline with AutoGen
import autogen
from autogen import AssistantAgent, UserProxyAgent, GroupChat, GroupChatManager
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# MULTI-AGENT SYSTEM WITH AUTOGEN
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
config_list = [{"model": "gpt-4o-mini", "api_key": "your-key"}]
llm_config = {"config_list": config_list, "temperature": 0.1}
# Specialist agents
planner = AssistantAgent(
name="Planner",
llm_config=llm_config,
system_message='''You are a project planner. Break down tasks into steps.
Coordinate with Engineer, Critic, and Writer.
Synthesize the final output.''',
)
engineer = AssistantAgent(
name="Engineer",
llm_config=llm_config,
system_message='''You are a Python software engineer.
Write clean, well-tested, production-ready code.
When writing code, check for edge cases and error handling.''',
)
critic = AssistantAgent(
name="Critic",
llm_config=llm_config,
system_message='''You are a code reviewer and quality assurance agent.
Review code for bugs, security issues, performance, and correctness.
Flag issues clearly with severity (HIGH/MEDIUM/LOW).''',
)
writer = AssistantAgent(
name="Writer",
llm_config=llm_config,
system_message='''You are a technical writer.
Write clear documentation, docstrings, README files.
Make complex technical concepts understandable.''',
)
# User proxy executes code automatically
user_proxy = UserProxyAgent(
name="UserProxy",
human_input_mode="NEVER", # fully autonomous
max_consecutive_auto_reply=5,
code_execution_config={
"work_dir": "./agent_workspace",
"use_docker": False,
},
is_termination_msg=lambda msg: "TASK_COMPLETE" in msg.get("content", ""),
)
# Group chat: all agents can speak and hear each other
group_chat = GroupChat(
agents=[planner, engineer, critic, writer, user_proxy],
messages=[],
max_round=15,
speaker_selection_method="auto", # GPT-4 selects who speaks next
)
manager = GroupChatManager(groupchat=group_chat, llm_config=llm_config)
# Start collaborative task
user_proxy.initiate_chat(
manager,
message='''Build a production-ready Python API endpoint that:
1. Accepts a CSV file upload with columns: date, price, volume
2. Returns: trend (bullish/bearish/neutral), simple moving average (7-day), volatility score
3. Include error handling, input validation, type hints
4. Write comprehensive docstrings and usage examples
When done, output TASK_COMPLETE'''
)
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# CREWAI ALTERNATIVE -- simpler, role-based
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
from crewai import Agent, Task, Crew, Process
researcher = Agent(
role="Research Analyst",
goal="Find comprehensive, accurate information about the given topic",
backstory="Expert researcher with 10 years experience in data analysis",
verbose=True, allow_delegation=False,
)
writer_crew = Agent(
role="Content Writer",
goal="Write engaging, accurate content based on research",
backstory="Experienced technical writer who makes complex topics clear",
verbose=True, allow_delegation=False,
)
research_task = Task(description="Research the latest advances in quantum computing", agent=researcher, expected_output="Comprehensive research report with key facts and citations")
writing_task = Task(description="Write a blog post explaining quantum computing to beginners", agent=writer_crew, expected_output="800-word engaging blog post")
crew = Crew(
agents=[researcher, writer_crew],
tasks=[research_task, writing_task],
process=Process.sequential, # or Process.hierarchical for manager-based
verbose=True,
)
result = crew.kickoff()Tip
Tip
Practice MultiAgent Systems Agents Collaborating in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Technical diagram.
Practice Task
Note
Practice Task — (1) Write a working example of MultiAgent Systems Agents Collaborating from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with MultiAgent Systems Agents Collaborating is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready ai code.