LangGraph & Multi-Agent Workflows
Standard chains are strictly Directed Acyclic Graphs (DAGs)—they run linearly from start to finish. Real-world autonomous software requires cycles: iterative refinement, multi-agent collaboration, persistent state tracking, and human-in-the-loop approval gates.LangGraph is the state-of-the-art framework for orchestrating cyclic agent graphs.
1. The Core LangGraph Architecture
LangGraph models complex AI processes as formal state machines with four core elements:
- State: A central schema (often typed dict) that represents the current snapshot of your application.
- Nodes: Python or TypeScript functions that receive the current State, execute computation (such as calling an LLM or database), and return state updates.
- Edges: Direct transitions connecting one Node to the next.
- Conditional Edges: Decision functions that evaluate the State and dynamically route to different Nodes (e.g., "continue tool loop" vs "finish").
2. Constructing a Stateful Cyclic Agent
Here is how to construct a cyclic agent using StateGraph:
# Install: pip install langgraph
from typing import Annotated, TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from langchain_openai import ChatOpenAI
from langchain_core.messages import BaseMessage, HumanMessage
# 1. Define shared state schema with message reducer
class AgentState(TypedDict):
messages: Annotated[list[BaseMessage], add_messages]
# 2. Define Node functions
model = ChatOpenAI(model="gpt-4o", temperature=0)
def chatbot_node(state: AgentState):
response = model.invoke(state["messages"])
return {"messages": [response]}
# 3. Build Graph
builder = StateGraph(AgentState)
# Add Node
builder.add_node("chatbot", chatbot_node)
# Define Transitions
builder.add_edge(START, "chatbot")
builder.add_edge("chatbot", END)
# Compile into executable Runnable
graph = builder.compile()
# Execute graph
output = graph.invoke({"messages": [HumanMessage(content="Explain LangGraph in 1 sentence.")]})
print(output["messages"][-1].content)3. The Multi-Agent Supervisor Pattern
In complex enterprises, a single prompt cannot handle coding, data analysis, and documentation writing. Instead, you construct specialized agents (e.g., CoderAgent, ResearcherAgent) governed by a centralized Supervisor Agent that delegates tasks and synthesizes the final output:
4. Human-in-the-Loop Approval Checkpoints
For high-stakes actions (transferring funds, deleting databases, sending customer emails), agents must pause execution and await human confirmation. LangGraph provides built-in interrupt checkpoints using interrupt_before and persistent state checkpointers (SQLite / Postgres):
from langgraph.checkpoint.memory import MemorySaver
# Persistent memory checkpointer tracks conversation state at every node
memory = MemorySaver()
# Interrupt execution before the sensitive action node executes
app = builder.compile(
checkpointer=memory,
interrupt_before=["execute_wire_transfer"]
)
# Execution halts! The human inspects the state, validates parameters, and calls:
# app.invoke(None, config={"configurable": {"thread_id": "tx_492"}}) to approve!Explore Related Tools
Hand-picked utilities and calculators related to this tool.
