← All notes

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

Nodes and edges

Compiling

Compilation checks the graph structure and is where you attach checkpointers and breakpoints.

See also

Claims

Sources

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

Permanent ID: iil:note:9vr4vwmq

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.},
}

Markdown

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.