← Back to Tutorials

LangGraph Architecture

Graph-Based Design

LangGraph models agent workflows as state machines. Nodes process state; edges define conditional transitions. This enables loops, branching, and complex orchestration.

pip install langgraph langchain-openai

Core Components

LangGraph state graph flow

State Management

from typing import TypedDict, List
from langgraph.graph import StateGraph, END

class AgentState(TypedDict):
    messages: List[str]
    next_agent: str

def router_node(state: AgentState):
    # Classify and route
    if "billing" in state["messages"][-1].lower():
        return {"next_agent": "billing"}
    return {"next_agent": "support"}

def billing_node(state: AgentState):
    return {"messages": [f"Billing: Processing..."]}

graph = StateGraph(AgentState)
graph.add_node("router", router_node)
graph.add_node("billing", billing_node)
graph.add_conditional_edges("router", lambda s: s["next_agent"])
graph.set_entry_point("router")
app = graph.compile()

Advanced Features

Browser Automation

Integrate Playwright agents as LangGraph nodes for web browsing, form filling, and data extraction.

Feedback Loops & Evaluators

Add evaluator nodes that critique outputs and trigger re-processing if quality is insufficient.

def evaluator_node(state: AgentState):
    output = state["messages"][-1]
    if len(output) < 50:
        return {"needs_revision": True}
    return {"needs_revision": False}
✏️ Exercise: Build a LangGraph chatbot with three nodes: (1) a classifier node that determines intent, (2) a response node that generates answers, and (3) an evaluator node that checks response quality and triggers revision if needed. Use checkpointing to persist state across interruptions.