Multi-agent coordination

The multi-agent pattern creates a team of specialized AI agents, each with a distinct role and expertise. Agents collaborate by passing information and building on each other's work, producing better outcomes than any single agent could achieve alone.

Multi-agent coordination

Specialized agents work sequentially, each building on previous work.

Specialization improves quality. A single agent juggling research, analysis, writing, and review produces mediocre results at each stage. Dedicated agents with focused system prompts outperform generalist agents on complex workflows.

patterns/07_multi_agent.py
python
class Agent:
    """Specialized agent with a role."""
    def __init__(self, name, role, expertise):
        self.name = name
        self.role = role
        self.expertise = expertise
        self.llm = get_llm()

    def work(self, task, context=""):
        prompt = f"""
        You are {self.name}, a {self.role}
        with expertise in {self.expertise}.
        Task: {task}
        Context: {context}
        Provide your specialized input.
        """
        return self.llm.generate(prompt).content

def coordinate_agents(agents, task):
    """Sequential coordination where each builds on previous work."""
    results = {}
    context = ""
    for agent in agents:
        result = agent.work(task, context)
        results[agent.name] = result
        context += f"\n{agent.name}: {result}"
    return results

# Define the team
agents = [
    Agent("Researcher", "Research Specialist", "gathering information"),
    Agent("Analyst", "Data Analyst", "identifying patterns"),
    Agent("Writer", "Technical Writer", "clear communication"),
    Agent("Reviewer", "Quality Reviewer", "accuracy checks"),
]
results = coordinate_agents(agents, task)

Define an Agent class with role and expertise, then coordinate a team.

Prompt chaining passes output from one call to the next in a fixed pipeline. Multi-agent coordination gives each agent autonomy to decide what to do next, communicate with other agents, and adapt based on results. Agents can run in parallel, negotiate, and even disagree.

Matching exercise: Match Multi-agent concepts

Loading practice…

Quiz: Quiz

Loading practice…

Ordering exercise: Order the Multi-agent pipeline

Loading practice…

Flashcards: Flashcards

Loading practice…

Multi-agent coordination unlocks a new level of complexity. Instead of one agent doing everything, you can build teams of specialists that collaborate. Next, we will tackle memory management so your agents can remember context across conversations.