LangGraph StateGraph: state with reducers, nodes that return updates, edges that route, then compile
Answer: Build a graph by defining state, adding nodes and edges to a StateGraph, then compiling; you must compile the graph before you invoke or stream it. Nodes do the work; edges decide what runs next.
Example
import operator
from typing import Annotated
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
class State(TypedDict):
foo: int
bar: Annotated[list[str], operator.add]
def node_a(state: State):
return {"foo": state["foo"] + 1, "bar": ["a"]}
def route(state: State):
return "node_b" if state["foo"] < 3 else END
def node_b(state: State):
return {"bar": ["b"]}
builder = StateGraph(State)
builder.add_node("node_a", node_a)
builder.add_node("node_b", node_b)
builder.add_edge(START, "node_a")
builder.add_conditional_edges("node_a", route)
builder.add_edge("node_b", END)
graph = builder.compile()
graph.invoke({"foo": 1, "bar": []})
State and reducers
- State is the shared data that nodes read and update, stored in channels (the keys of your state object).
- Each key has its own reducer; with no reducer, updates overwrite the previous value.
Annotated[list[str], operator.add]appends instead. - For chat history, use
add_messages, which appends new messages and updates existing ones by ID;MessagesStateis a prebuilt state with a singlemessageskey that uses it.
Nodes and edges
- Nodes are sync or async functions that accept the state and optionally a config and runtime.
- Normal edges always go from one node to the next; conditional edges call a routing function that returns the next node name (or a list of names).
- Destinations of several outgoing edges run in parallel in the next super-step.
STARTis the virtual entry that delivers user input;ENDmarks a terminal edge.
Compiling
Compilation checks the graph structure and is where you attach checkpointers and breakpoints.
See also
Claims
- id: C1 claim: With no reducer, updates overwrite the previous value. source: S2 evidence: "With no reducer, updates overwrite the previous value."
- id: C2 claim: You must compile the graph before invoking or streaming it. source: S2 evidence: "You must compile the graph before you invoke or stream it."
- id: C3 claim: MessagesState is a prebuilt state with a single messages key. source: S1 evidence: "MessagesState is a prebuilt state with a single messages key"
Sources
- [S1] Graph API overview. LangChain docs (LangGraph). https://docs.langchain.com/oss/python/langgraph/graph-api . License: MIT. Accessed 2026-11-18.
- [S2] graph-api.mdx (source page). langchain-ai/docs (GitHub). https://github.com/langchain-ai/docs/blob/main/src/oss/langgraph/graph-api.mdx . License: MIT. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: LangChain docs (LangGraph), Graph API overview; langchain-ai/docs (GitHub), graph-api.mdx (source page). MIT (https://raw.githubusercontent.com/langchain-ai/docs/main/LICENSE). Copyright LangChain. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: LangChain docs (LangGraph), Graph API overview; langchain-ai/docs (GitHub), graph-api.mdx (source page). MIT (https://raw.githubusercontent.com/langchain-ai/docs/main/LICENSE). Copyright LangChain.
Cite this
iinventedlight. 2026. "LangGraph StateGraph: state with reducers, nodes that return updates, edges that route, then compile." iinventedlight Library, iil:note:9vr4vwmq, version of 2026-11-18. https://iinventedlight.com/library/notes/langgraph-graph-api/. License: CC-BY-4.0.
@misc{iil_note_9vr4vwmq,
author = {{iinventedlight}},
title = {{LangGraph StateGraph: state with reducers, nodes that return updates, edges that route, then compile}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/langgraph-graph-api/},
note = {Permanent ID iil:note:9vr4vwmq. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/langgraph-graph-api.md.