AI Hints
Home›LangGraph Tutorial›State, Nodes and Edges
LangGraph · Lesson 4 of 120

State

The shared notebook.

⏱ 2 min read▶ Video at 3:18🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

Think of state as a shared notebook. Every node can read it, and write to it. When one node finishes, the next node sees what it wrote. That's how information flows through a graph.

State
From the video: State
  • A shared notebook
  • Every node reads it
  • And writes to it

Describing the state

We describe the state with a typed dictionary. Each key has a name and a type. Here, an order has a title, a quantity, a price, and a total. The node reads three keys, and writes the total. Run it. The final state holds everything, including the new total.

state.pypython
from typing import TypedDict
from langgraph.graph import StateGraph, START, END

class Order(TypedDict):
    title: str
    quantity: int
    price: float
    total: float

def add_total(state: Order):
    return {"total": round(state["quantity"] * state["price"], 2)}

builder = StateGraph(Order)
builder.add_node("add_total", add_total)
builder.add_edge(START, "add_total")
builder.add_edge("add_total", END)
graph = builder.compile()
print(graph.invoke({"title": "Dune", "quantity": 2, "price": 9.99}))
Output
{'title': 'Dune', 'quantity': 2, 'price': 9.99, 'total': 19.98}

Good state

Keep state small, and give every key a clear name. Store facts, not long explanations. Each key should have one job. And remember, any node can read any key, so the state is the contract between your nodes.

keyvalue
Smallonly what's needed
Clear namesone job each
TypedTypedDict
Contractbetween nodes

Summary

State is a typed dictionary every node can read and update. It carries information through the graph. Next, nodes.

Key takeaways
  • TypedDict
  • Keys and types
  • Shared by all nodes
Next up: Nodes — functions that update state