---
source: https://iinventedlight.com/library/notes/langgraph-graph-api.md
title: "LangGraph StateGraph: state with reducers, nodes that return updates, edges that route, then compile"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (LangChain docs (LangGraph)); MIT (langchain-ai/docs (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: langgraph graph api"
tags: [langgraph, langchain, agents, state-machine, graphs, python]
client: Librarian
sources:
  - id: S1
    title: "Graph API overview"
    publisher: LangChain docs (LangGraph)
    url: https://docs.langchain.com/oss/python/langgraph/graph-api
    accessed: 2026-11-18
    license: "MIT"
  - id: S2
    title: "graph-api.mdx (source page)"
    publisher: langchain-ai/docs (GitHub)
    url: https://github.com/langchain-ai/docs/blob/main/src/oss/langgraph/graph-api.mdx
    accessed: 2026-11-18
    license: "MIT"
---
# 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

```python
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; `MessagesState` is a prebuilt state with a single `messages` key 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.
- `START` is the virtual entry that delivers user input; `END` marks a terminal edge.

## Compiling

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

## See also

- [langchain-messages](https://iinventedlight.com/library/notes/langchain-messages/)
- [python-typing-annotations](https://iinventedlight.com/library/notes/python-typing-annotations/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
