Advanced Patterns
Multi-Agent Systems
Orchestrate multiple specialized agents working together to solve complex tasks.
Why Multi-Agent?
A single agent can struggle with complex tasks. Multi-agent systems use specialized agents that collaborate:
- Parallelism: Multiple agents work simultaneously
- Specialization: Each agent is an expert at one thing
- Verification: One agent checks another's work
- Scale: Break large tasks into manageable pieces
Common Patterns
Orchestrator + Workers
A main agent coordinates specialist sub-agents.
Pipeline
Agents pass output sequentially — A → B → C.
Debate / Critique
Two agents argue opposite positions; a judge decides.
Swarm
Many identical agents work in parallel on sub-problems.
Communication Patterns
- Direct: Orchestrator calls worker directly
- Message queue: Agents communicate via shared queue
- Shared state: Agents read/write shared memory/DB
Example
python
from anthropic import Anthropic
from dataclasses import dataclass
from typing import Callable
client = Anthropic()
@dataclass
class Agent:
name: str
role: str
tools: list = None
def run(self, task: str, context: str = "") -> str:
system = f"You are {self.name}. Your role: {self.role}"
user_content = f"{context}
Task: {task}" if context else task
response = client.messages.create(
model="claude-3-5-haiku-20241022",
max_tokens=1024,
system=system,
messages=[{"role": "user", "content": user_content}]
)
return response.content[0].text
# Create specialized agents
researcher = Agent(
name="Research Agent",
role="You gather and summarize information on topics. Be thorough and factual."
)
writer = Agent(
name="Writing Agent",
role="You write clear, engaging content based on research. Use a professional tone."
)
critic = Agent(
name="Critic Agent",
role="You review content and provide specific, actionable feedback for improvement."
)
editor = Agent(
name="Editor Agent",
role="You refine content based on feedback, improving clarity and flow."
)
# Orchestrator
def content_pipeline(topic: str) -> str:
print(f"[Orchestrator] Starting content pipeline for: {topic}
")
# Step 1: Research
print("[Researcher] Gathering information...")
research = researcher.run(f"Research key facts and insights about: {topic}")
print(f"Research complete ({len(research)} chars)
")
# Step 2: Write draft
print("[Writer] Writing draft...")
draft = writer.run(
f"Write a 3-paragraph article about: {topic}",
context=f"Research findings:
{research}"
)
print(f"Draft complete
")
# Step 3: Critique
print("[Critic] Reviewing draft...")
feedback = critic.run(
"Review this article and list 3 specific improvements:",
context=f"Article:
{draft}"
)
print(f"Feedback: {feedback[:200]}...
")
# Step 4: Edit
print("[Editor] Applying improvements...")
final = editor.run(
"Improve this article based on the feedback:",
context=f"Original article:
{draft}
Feedback:
{feedback}"
)
return final
result = content_pipeline("the future of AI agents in software development")
print("=== FINAL ARTICLE ===")
print(result)Try it yourself — PYTHON