LLM Lab

State, nodes & edges

LangGraph describes an application as a graph: a shared state, nodes that update it, and edges that decide what runs next.

Where this sits in a LangGraph application
State→Nodes→Edges & routing→Parallel (Send)→Checkpoints & interrupts→Several agents

Three ideas

The state is a typed dictionary shared by the whole run, defined with TypedDict. A node is a function that reads the state and returns the keys it wants to change. An edge says which node runs next; START and END mark where a run begins and stops. You add nodes and edges to a StateGraph builder, then compile() it into a runnable graph.

read / updateSTARTclassifyanswerENDStatemessages · topic
A LangGraph graph: nodes run in the order the edges give, and every node reads the shared state and returns updates to it.

Reducers decide how updates merge

By default a returned value overwrites the old one. A reducer, attached with Annotated, merges instead: add_messages appends to the chat history (and updates a message with the same ID), and operator.add concatenates lists. Reducers matter most when several nodes write to the same key, as in parallel branches.

from typing import Annotated, TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages

class State(TypedDict):
    messages: Annotated[list, add_messages]   # reducer: append
    topic: str                                # no reducer: overwrite

def classify(state: State) -> dict:
    return {"topic": "billing"}               # return only what changes

def answer(state: State) -> dict:
    return {"messages": [("assistant", "Refunds take 5-7 days.")]}

builder = StateGraph(State)
builder.add_node("classify", classify)
builder.add_node("answer", answer)
builder.add_edge(START, "classify")
builder.add_edge("classify", "answer")
builder.add_edge("answer", END)
graph = builder.compile()
graph.invoke({"messages": [("user", "How long do refunds take?")]})
A complete two-node graph. Each node returns a small dict of updates; the graph merges them into the state using each key’s reducer.

A worked example

The run starts with one user message. classify returns {"topic": "billing"}: the topic key changes, the messages stay. answer returns one assistant message: because of add_messages, the list now holds both messages instead of only the new one. Had messages no reducer, the user’s question would have been overwritten.

Easy to confuse

Check yourself · 1 of 3

A node returns {"topic": "billing"}. What happens to the state?

Check yourself · 2 of 3

What does the add_messages reducer do?

Check yourself · 3 of 3

Why must you call compile() before invoke()?

Read more in the books

  • LangChain and LangGraph documentation, LangChain: LangGraph: Overview and Graph API.
  • Learning LangChain, Mayo Oshin, Nuno Campos: the chapters on LangGraph.
All books
Labrun a graph
How to read this
  • The stage map is the graph, from START to END; the lit stage is the node that just ran. Next runs one step.
  • The state panel shows every key after the step. The key the node changed is outlined; messages grow because their reducer appends, while topic is overwritten.
  • Switch the reducer for notes between overwrite and append to see the difference after both nodes write to it.
Reducer for notes