LangGraph Fundamentals
Master LangGraph Fundamentals — build graphs, manage state, use nodes and edges, define conditional routing, custom reducers, and implement loops in agentic workflows.
This module explores the core programming primitives of LangGraph: building compilation graphs, defining nodes and edges, state manipulation via TypedDict interfaces, routing with conditional edges, and accumulating session logs using custom reducer annotations.
Building StateGraphs
Why this matters
StateGraph is the core LangGraph primitive — nodes mutate shared state through explicit edges.
StateGraph defines nodes (functions) and edges (control flow). Entry point is START; terminal nodes connect to END.
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
class State(TypedDict):
messages: list
def chatbot(state: State):
return {"messages": state["messages"] + ["reply"]}
builder = StateGraph(State)
builder.add_node("chatbot", chatbot)
builder.add_edge(START, "chatbot")
builder.add_edge("chatbot", END)
graph = builder.compile()- Conditional edges route based on state (e.g. tool call vs finish).
- Cycles enable ReAct-style loops — always add termination conditions.
Common mistakes
- Forgetting to compile the graph with a checkpointer when persistence is required.
- List state fields without reducers — updates overwrite instead of append.
- Infinite loops in cyclic graphs with no max iteration or termination edge.
Interview checkpoints
- Q: Explain stategraph in LangGraph. A: One-sentence definition + one API.
- Q: Common bug? A: State, checkpointer, or routing loop issue.
Practice
- Basic: Sketch a minimal stategraph example.
- Intermediate: Run a notebook cell demonstrating StateGraph.
- Advanced: Break StateGraph intentionally and read the LangSmith trace.
Recap
- You can explain stategraph clearly.
- You know one mistake to avoid.
- You see how this connects to the next lesson.
Next: State & Reducers
State, Reducers & Execution
Why this matters
Reducers define how state updates merge (especially lists); wrong reducers cause silent data loss.
State is a typed dict (often TypedDict). Reducers on annotated fields control merge behavior for lists and counters.
Annotated[list, add_messages]— append messages instead of replace.- Parallel nodes run when fan-out edges exist; join via reducer or barrier node.
.invoke(),.stream(),.astream_events()for execution.
Common mistakes
- Forgetting to compile the graph with a checkpointer when persistence is required.
- List state fields without reducers — updates overwrite instead of append.
- Infinite loops in cyclic graphs with no max iteration or termination edge.
Interview checkpoints
- Q: Explain state & reducers in LangGraph. A: One-sentence definition + one API.
- Q: Common bug? A: State, checkpointer, or routing loop issue.
Practice
- Basic: Sketch a minimal state & reducers example.
- Intermediate: Run a notebook cell demonstrating State & Reducers.
- Advanced: Break State & Reducers intentionally and read the LangSmith trace.
Recap
- You can explain state & reducers clearly.
- You know one mistake to avoid.
- You see how this connects to the next lesson.
Next: Checkpointers
