LangGraph Multi-Agent Tutorial: Build AI Agent Workflows Step-by-Step
What You Will Learn
In this tutorial, you will learn how to build multi-agent workflows using LangGraph, including supervisor agents, routing logic, shared state memory, and production-ready AI automation examples.
The artificial intelligence paradigm has shifted. While traditional generative AI chatbots require continuous user guidance to produce outputs, Agentic AI leverages autonomous loops, stateful memory, and external tools to accomplish complex goals step-by-step.
💡 Prerequisite Tip: To index documents efficiently for your agentic tools, see our OpenAI Embeddings Production Optimization Guide.
Multi-Agent Orchestration Flow (Supervisor Pattern)
1. What are AI Agents?
An AI Agent is an artificial intelligence system that autonomously performs tasks by designing workflows, decomposing goals, and invoking available tools. At their core, agents use the advanced natural language processing and reasoning capabilities of large language models (LLMs) as their central "brain" to determine when and how to interact with external environments.
AI Agents vs. AI Assistants
Traditional AI assistants are reactive; they require continuous, turn-by-turn user prompts to perform tasks and lack independent planning capabilities. In contrast, AI agents exhibit autonomy. Given a high-level goal, they initialize a plan, create subtasks, and execute them without needing constant human intervention.
Agentic AI vs. Generative AI
While standard generative AI focuses on static content generation (text, images, or code) based on prompt patterns, Agentic AI introduces action loops. It is a compound system framework that combines LLMs with stateful routing, long-term memory, and tool integration to achieve dynamic end-to-end task completion.
The Agentic Lifecycle: Three Core Stages
- Goal Initialization and Planning: The agent receives a high-level directive, performs task decomposition, and formulates a step-by-step execution path.
- Reasoning with Available Tools: The agent uses APIs, web search, vector databases, or files to bridge information gaps, continuously updating its internal state.
- Learning and Reflection (Iterative Refinement): The agent assesses its outputs against critics or user feedback (Human-in-the-loop), learning from mistakes to refine its execution dynamically.
Agentic RAG
Standard Retrieval-Augmented Generation (RAG) is a linear pipeline: query ➔ retrieve ➔ generate. Agentic RAG transforms this into a multi-step loop. The agent retrieves documents, assesses relevance, refines queries if the context is insufficient, and executes multiple retrieval runs to formulate a comprehensive and factually accurate response.
2. Reasoning Paradigms: ReAct vs. ReWOO
How do agents execute complex workflows? There are two primary architectural paradigms for agentic reasoning:
| Paradigm | How it Works | Token Efficiency |
|---|---|---|
| ReAct (Reason + Action) | Iterative loops of Think ➔ Act ➔ Observe. The next step depends on the output of the previous tool. | Lower (High overhead per loop) |
| ReWOO (Reason Without Observation) | Plans all steps upfront, allocating parameters before running tools. Decouples reasoning from tool outputs. | Higher (Saves API costs) |
3. LangGraph vs. LangChain Agents: When to Upgrade?
A common question for developers is whether to use standard LangChain agents or upgrade to LangGraph.
LangChain Agents are built for linear execution chains. They take a list of tools and iteratively decide which tool to call in a sequential chain. This is perfect for simple single-agent setups like basic question-answering or scripting assistants.
LangGraph is designed for cyclical orchestration and multi-agent coordination. If your workflow requires loop-backs (e.g. "if the code contains syntax errors, send it back to the developer agent"), shared state persistence, state routing, or human-in-the-loop approval, LangGraph provides the necessary framework.
4. The 5 Types of AI Agents
Depending on complexity, autonomous agentic systems can be classified into 5 main categories:
- 1. Simple Reflex Agents: Act purely on condition-action rules (e.g. "If temperature > 25°C, turn on AC"). They maintain no internal memory and fail in partially observable environments.
- 2. Model-Based Reflex Agents: Maintain an internal model of the world to track hidden states. They remember past interactions to update their current state, but are still bound by set rules.
- 3. Goal-Based Agents: Combine internal world states with specific goals. They search, plan ahead, and evaluate multiple action paths to reach their destination.
- 4. Utility-Based Agents: Go beyond simply hitting goals. They calculate utility functions to score multiple valid paths and select the single most cost-effective or fastest route.
- 5. Learning Agents: Autonomously update their knowledge base using critics (performance metrics) and problem generators, allowing them to adapt to completely unfamiliar environments.
5. LangGraph Multi-Agent Code Tutorial (Step-by-Step)
To build a real supervisor-worker pattern, we define a centralized state, specialized worker nodes, and a router function that determines if the workflow should continue or terminate.
Step 1: Define the Shared State
The agents communicate by appending messages to a shared state array.
from typing import TypedDict, Annotated, Sequence
from langchain_core.messages import BaseMessage
from langgraph.graph.message import add_messages
# The AgentState keeps track of conversation history across all agents
class AgentState(TypedDict):
messages: Annotated[Sequence[BaseMessage], add_messages]
next_agent: str # Router updates this to decide the next active nodeStep 2: Create the Supervisor Node & Routing Logic
The supervisor reads the chat history and determines which worker agent should execute next, or if the final goal is met.
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
# Create supervisor prompt instructing it to act as a project manager
supervisor_prompt = ChatPromptTemplate.from_messages([
("system", "You are the project manager supervising workers: Researcher, Coder. "
"Given the state, respond with the name of the next worker to act "
"or 'FINISH' if the goal is fully accomplished."),
("placeholder", "{messages}")
])
def supervisor_agent(state: AgentState):
model = ChatOpenAI(model="gpt-4o")
# Bind output to force the model to select next action
chain = supervisor_prompt | model.with_structured_output(dict)
response = chain.invoke(state)
return {"next_agent": response.get("next_agent", "FINISH")}
def router_edge(state: AgentState):
# Conditional edge route based on supervisor state output
if state["next_agent"] == "FINISH":
return "end"
return state["next_agent"]Step 3: Compile the State Graph
Now, assemble the nodes (agents) and connect them using edges to create the execution loops.
from langgraph.graph import StateGraph, END
workflow = StateGraph(AgentState)
# Add nodes
workflow.add_node("Supervisor", supervisor_agent)
workflow.add_node("Researcher", researcher_node)
workflow.add_node("Coder", coder_node)
# Set entry point
workflow.set_entry_point("Supervisor")
# Add conditional edges
workflow.add_conditional_edges(
"Supervisor",
router_edge,
{
"Researcher": "Researcher",
"Coder": "Coder",
"end": END
}
)
# Workers always report back to Supervisor to assess state
workflow.add_edge("Researcher", "Supervisor")
workflow.add_edge("Coder", "Supervisor")
app = workflow.compile()Example Agent Input & Output
"Write a python script that fetches top articles from Hacker News and emails the list."
- 🚀 [Supervisor] → Decides "Researcher" needs to search Hacker News API structure.
- 🔍 [Researcher] → Invokes Web Search tool. Finds API base url. Appends data.
- 🚀 [Supervisor] → Reads updated state. Decides "Coder" must write the script.
- 💻 [Coder] → Writes script. Appends response code blocks.
- 🚀 [Supervisor] → Analyzes results. Confirms task complete. Decides "FINISH".
6. Production Best Practices & Governance
Deploying autonomous systems into production carries unique risks. Implementing these industry best practices is essential:
- Human-in-the-Loop (HITL) Interruption: Implement interruptibility nodes so humans must approve high-impact actions (e.g. sending customer emails or executing financial trades).
- Structured Activity Logs: Keep detailed records of every tool call, reasoning thought, and API response to ensure auditability and debug loops.
- Unique Agent Identifiers: Assign unique cryptographic tags to agents accessing external servers, allowing full tracing of developers and deploys if errors occur.
- Data Governance: Wrap models in strict safety guardrails and input-output moderators to prevent model manipulation or hallucinated inputs.
🔗 Further Reading: Check out our guide on Hybrid Search vs Vector Search to learn how dense vectors and sparse keywords combine to give your agent's retrieval tools a 98% accuracy boost.
Frequently Asked Questions (FAQs)
What is LangGraph used for?
LangGraph is designed for building stateful, multi-agent systems with cycles and loops. It allows developers to define complex flowcharts where agents can repeatedly query tools, interact with each other, and update a shared state.
Is LangGraph better than LangChain?
LangGraph is not a replacement for LangChain, but an extension. While LangChain is ideal for linear chains of actions, LangGraph is specifically built to handle cyclical workflows, complex orchestration, and multi-agent coordination.
What are multi-agent systems?
A multi-agent system is an AI architecture where a single complex goal is divided among several specialized agents (e.g., a research agent, a coder agent, and a supervisor router) that collaborate to solve the problem.
How do agents communicate in LangGraph?
Agents in LangGraph communicate by reading and writing to a shared, centralized State object. This state is updated append-only or via custom reducer functions as each node in the graph executes.
Is LangGraph production ready?
Yes, LangGraph is production-ready. It features built-in persistence for conversation memory, short-term and long-term memory streams, and node-level interruptibility for human-in-the-loop validation.