TLDR; - LangGraph Development Cheatsheet and Guidelines
Motto: Control Flow, Master State.
LangGraph reimagines AI application development beyond simple chains, offering a graph-based framework for building sophisticated workflows with Large Language Models. It addresses the limitations of linear chains by providing developers with fine-grained control over application logic, state management, and execution flow, enabling the creation of robust and complex agentic systems.
This cheatsheet provides a concise, actionable guide to LangGraph development, covering core concepts, best practices, debugging techniques, and common use cases, empowering developers to build high-quality, production-ready AI applications with greater efficiency and mastery.
Core Concepts
- Graph: Workflow Blueprint. Interconnected Nodes & Edges. Defines app logic.
StateGraph(versatile),MessageGraph(chatbots). - Node: Computation Building Block. Python/JS function. Input:
State. Output: State updates/Command. Executes tasks: LLM call, tool use, routing. - Edge: Defines Execution Flow. Connections between Nodes. Normal (sequential), Conditional (dynamic routing),
Send(parallel tasks),Command(state+control). - State: Shared Application Memory. Data Structure. Schema (
TypedDict/PydanticBaseModel). Channels for node communication. - Reducer: Concurrent State Update Manager. Functions to combine/merge State updates. Crucial for lists, parallel branches.
Quick Setup
- Install (Python):
pip install langgraph langchain_openai - Import:
from langgraph.graph import StateGraph, START, END; from langgraph.types import Command, Send, interrupt; from langchain_core.runnables import RunnableConfig - State (Pydantic):
class MyState(BaseModel): user_input: str; llm_output: str = ""
Key Operations
- Add Node:
builder.add_node("node_name", node_function) - Normal Edge:
builder.add_edge("node_a", "node_b") - Conditional Edge:
builder.add_conditional_edges("node_a", route_fn) - Compile Graph:
graph = builder.compile(checkpointer=MemorySaver()) - Invoke Graph:
graph.invoke({"input": "val"}, config={"configurable": {"thread_id": "thread_1"}}) - Stream Output:
graph.stream({"input": "val"}, config=config, stream_mode="updates") Commandin Node:def command_node(state: MyState) -> Command[Literal["node_b"]]: return Command(goto="node_b")interrupt(HITL):user_input = interrupt({"q": "Approve?"}); return Command(resume=user_input)
Key Features
- Persistence: Checkpointers, Threads, Memory, Time Travel, Fault-Tolerance. Save progress, enable advanced features.
- Streaming:
values,updates,tokensmodes. Real-time UX, responsiveness. - Configuration:
config_schema,configurable. Dynamic, reusable, adaptable graphs. - Recursion Limit: Prevents infinite loops. Safety net for runaway executions.
- Subgraphs: Modular apps, nested graphs, agent teams. Encapsulation, reusability.
- Breakpoints:
interrupt_before,interrupt_after,NodeInterrupt. Debugging, human-in-the-loop control. interrupt: Human-in-the-loop. Pause, resume, human feedback integration.
Use Cases & Patterns
- Router Agents: LLM Traffic Controllers. Route to specialized nodes. Intent-based, Adaptive RAG, Multi-Agent dispatch.
- Tool-Calling (ReAct) Agents: Reason-Act-Observe. LLM + Tools for complex tasks. Planning, memory, external interaction.
- Multi-Agent Systems: AI Teams. Network, Supervisor, Hierarchical. Modularity, handoffs, agent communication & coordination.
- Human-in-the-Loop (HITL): Human-AI Collaboration.
interruptfor approval, editing, review, multi-turn. Human oversight, guidance, error correction. - Map-Reduce: Parallel Power.
Sendfor parallel tasks, Reducers for aggregation. Scalability, efficient data processing. - Self-Correcting Agents (Reflection): AI Learns. LLM or rule-based evaluators, feedback loops. Iterative improvement, self-optimization.
Troubleshooting, Performance, Fixes & “Gotchas”
StateGraphvsMessageGraph?:StateGraph= default,MessageGraph= chatbots (simple).- Node Errors?:
try-except+ logging + graceful degradation. - Debug LangGraph?: Breakpoints, Time Travel, LangSmith, Logging (essential tools).
- Human-in-the-loop?: Use
interrupt(value)&Command(resume=value). - Optimize Performance?: Streaming, Parallel, Lean State, Recursion Limit (tune).
- Side Effects &
interrupt: Avoid side effects beforeinterrupt- re-execution trap! - Resume Re-executes Node:
interruptresumes entire node, not line-by-line. - Subgraph State: Isolated. Explicitly manage State flow between graphs.
- Reducers (Parallel): Mandatory for concurrent State updates. Prevent
InvalidUpdateError. - Recursion Limit: Safety net, not design fix. Design graphs for termination.
- Dynamic
interruptNodes: Complex, use with caution.
Happy LangGraph building! 🚀