AI Hints
Home›LangGraph Tutorial›Cheat Sheets
LangGraph · Lesson 115 of 120

Cheat Sheet: Graph Basics

State, nodes, edges.

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

Here's the first of three reminder pages. The basics. State, nodes, edges, routing, and reducers. Pause the video, and keep it handy.

Cheat Sheet: Graph Basics
From the video: Cheat Sheet: Graph Basics
  • Reminder page one
  • The basics
  • Pause and keep it

Basics cheat sheet

State is a typed dictionary, and annotated adds a reducer. Nodes are functions that return updates. Add node, add edge, and add sequence build the graph. Add conditional edges routes with a function. Compile makes it runnable, and invoke runs it.

cheatsheet_basics.pypython
import operator
from typing import Annotated, TypedDict
from langgraph.graph import StateGraph, START, END

class State(TypedDict):
    title: str
    log: Annotated[list[str], operator.add]     # reducer: append

def step(state): return {"log": ["step ran"]}   # return only changes

b = StateGraph(State)
b.add_node(step)                                 # or b.add_sequence([a, b, c])
b.add_edge(START, "step")
b.add_conditional_edges("step", lambda s: END)   # route by returning a name
graph = b.compile()
graph.invoke({"title": "Dune", "log": []})       # also batch, stream

Remember

A few more basics. Send creates parallel branches at run time. Command updates and routes at once. The recursion limit stops runaway loops. And draw mermaid shows the graph's shape.

keyvalue
Senddynamic branches
Commandupdate + goto
recursion_limitsafety net
draw_mermaidsee the shape

Summary

State with reducers, nodes that return updates, edges and conditional edges, then compile and invoke. Next, the cheat sheet for agents and memory.

Key takeaways
  • TypedDict + Annotated
  • add_node, add_edge
  • compile, invoke
Next up: Cheat Sheet: Agents and Memory — tools, checkpoints