Why LangGraph over plain LangChain
LangChain's original chains are linear — each step feeds into the next. LangGraph represents workflows as directed graphs, which enables branching, loops, parallel execution, and explicit state. This makes it suitable for complex agentic workflows that would require awkward conditionals in a chain.
LangGraph is particularly powerful for multi-agentMulti-AgentSystems where multiple AI agents with distinct roles collaborate, debate, or critique each other to solve complex tasks more effectively than single agents.Learn more → systems where different agents specialise in different tasks and hand off work to each other. The graph structure makes the flow of information explicit and easy to reason about.
Install and set up
Install LangGraph and its dependencies: `pip install langgraph langchain-openai`. LangGraph requires Python 3.9+. Set your `OPENAI_API_KEY` environment variable.
Core concepts: State, Nodes, and Edges
A LangGraph graph has three components: (1) State — a TypedDict that is passed through every node and accumulates results; (2) Nodes — Python functions that receive the state, do work, and return a partial state update; (3) Edges — connections between nodes, either fixed or conditional.
The State is the key insight. Every node reads from and writes to a shared state object. This is how nodes communicate without being tightly coupled. A `messages` field in State is the most common pattern — nodes append to it, and downstream nodes can read the full conversation history.
Build a research-and-summarise graph
Define a State with `messages`, `search_results`, and `summary` fields. Create three nodes: `search_node` (calls a search tool), `read_node` (fetches and extracts text from URLs), and `summarise_node` (LLMLLMLarge Language Model — a neural network trained on vast amounts of text data that can understand and generate human-quality language across a wide range of tasks.Learn more → call that synthesises the final summary).
Connect them with edges: START search_node read_node summarise_node END. Compile the graph: `graph = builder.compile()`. Invoke it: `result = graph.invoke({"messages": [{"role": "user", "content": "Summarise the latest news on quantum computing"}]})`. LangGraph runs each node in sequence, updating the state as it goes.
Add conditional edges and loops
Conditional edges let the graph branch based on state. After the search node, check if enough results were found: if yes, proceed to read_node; if no, loop back to search_node with a refined query. Use `add_conditional_edges("search_node", should_continue, {"continue": "read_node", "retry": "search_node"})` where `should_continue` is a function that inspects the state and returns a string.
Loop detection is important — add a `step_count` field to your State and increment it in each node. Have your conditional function return END after a maximum step count.
Multi-agent handoffs
For a multi-agentAgentAn LLM-powered system that can take actions, use tools, and pursue multi-step goals autonomously without human input at each step.Learn more → system, each agent is a subgraph compiled into a single node. A supervisor agent routes tasks to specialist agents (researcher, coder, writer) based on the current task. Each specialist executes its subgraph and returns a result to the supervisor.
LangGraph's `StateGraph.add_subgraph()` method embeds a complete graph as a node. The parent graph passes state into the subgraph and receives a merged state back. This composability is what makes LangGraph scale to complex production systems.