Stage 09 — Multi-Agent Frameworks
Multi-Agent Frameworks · Comprehensive Technical Training · ⏱ 10–12 hours
Learning Objectives
By the end of this stage you will be able to:
- Build multi-agent systems with CrewAI
- Design graph-based workflows with LangGraph
- Implement conditional routing and decision points
- Handle agent memory and state across conversations
- Debug and monitor multi-agent systems
Section 1: CrewAI Framework
CrewAI provides high-level abstractions for multi-agent systems. Agents are assigned roles, tools, and goals. Tasks define what agents should accomplish.
Basic CrewAI Setup
from crewai import Agent, Task, Crew, Process
from langchain_openai import ChatOpenAI
# Define agents
researcher = Agent(
role="Research Analyst",
goal="Find and analyze relevant information",
backstory="You are an expert research analyst...",
verbose=True,
allow_delegation=False,
tools=[search_tool, scrape_tool],
llm=ChatOpenAI(model="gpt-4o"),
)
writer = Agent(
role="Content Writer",
goal="Write high-quality content",
backstory="You are a skilled technical writer...",
verbose=True,
tools=[formatting_tool],
llm=ChatOpenAI(model="gpt-4o"),
)
# Define tasks
research_task = Task(
description="Research topic X and provide key insights",
agent=researcher,
expected_output="Detailed research report",
)
writing_task = Task(
description="Write a blog post based on the research",
agent=writer,
expected_output="Polished blog post",
)
# Create crew
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, writing_task],
process=Process.sequential, # Execute in order
verbose=True,
)
# Execute
result = crew.kickoff(inputs={"topic": "AI Agents"})
Section 2: LangGraph for Complex Workflows
LangGraph enables graph-based agent workflows with conditional routing, loops, and state management.
Graph Structure
from langgraph.graph import StateGraph, END
from langchain_core.messages import HumanMessage, AIMessage
# Define state
class State:
messages: list
approved: bool
# Define nodes (agents or functions)
def classify_request(state):
"""First agent: classify the request"""
response = llm.invoke(state.messages)
return {"messages": state.messages + [response]}
def approve_request(state):
"""Second agent: approval step"""
response = llm.invoke(f"Approve or reject: {state.messages[-1]}")
return {"approved": "approve" in response.lower()}
def execute_action(state):
"""Third agent: take action"""
action = llm.invoke(state.messages)
return {"messages": state.messages + [action]}
# Build graph
graph = StateGraph(State)
graph.add_node("classify", classify_request)
graph.add_node("approve", approve_request)
graph.add_node("execute", execute_action)
# Define edges with conditional routing
graph.add_edge("classify", "approve")
graph.add_conditional_edges(
"approve",
lambda state: "execute" if state.approved else END,
)
# Compile
compiled = graph.compile()
# Execute
result = compiled.invoke(State(messages=[HumanMessage("Do X")]))
print(result)
Section 3: State Management
Multi-agent systems need persistent state across steps:
class AgentState(BaseModel):
conversation_history: list
user_profile: dict
task_progress: dict
decisions_made: list
def agent_step(state: AgentState):
# Agent can access full state
user_prefs = state.user_profile.get("preferences", {})
previous_decisions = state.decisions_made
# Make decision based on state
response = llm.invoke(
f"Based on previous decisions {previous_decisions} and prefs {user_prefs}, what's next?"
)
# Update state
state.conversation_history.append(response)
state.decisions_made.append(response)
return state
What's Next
Stage 10 covers Microsoft AutoGen, an alternative multi-agent framework emphasizing conversation patterns and tool integration.
Lock In Founding Member Access
Get full access to every course on TechNodeX — AI, cybersecurity, Python, and everything we build next. $9/month, price locked forever.
Become a Founding Member →