07. Frameworks & Libraries
LangGraph Fundamentals (Nodes, Edges, State, Routing)
LangGraph models agentic workflows as graphs: nodes do work, edges decide what runs next, shared state carries data across steps, and routing (conditional edges) branches on that state. Use it when you need cycles, explicit control flow, and durable runs — beyond a straight LangChain chain.
What you will learn
- Define nodes, edges, state, and routing in LangGraph
- Sketch a simple agent graph with a tool cycle
- Design typed state and reducers at a high level
- Apply conditional routing with fail-closed defaults
- Answer interview questions with short, solid answers
Definition
LangGraph is a library for building stateful, multi-actor applications with LLMs as a graph of steps. Each node performs work and returns state updates; edges (including conditional edges) select the next node; a shared state object is the source of truth for the run.
It sits beside LangChain: use LangChain for models, tools, and prompts; use LangGraph when control flow needs loops, branching, persistence, or clear multi-step orchestration.
Introduction
This lesson (#3 in Frameworks & Libraries) follows Building AI Applications with LangChain. You can ship many apps with chains; LangGraph is for workflows that must loop, branch, or resume.
Next: LangGraph Checkpointing, Human Approval and Multi-Agent — durability and HITL on top of these fundamentals.
Why LangGraph
- First-class cycles (agent ↔ tools) without hacks
- Explicit topology you can draw and review
- Shared state instead of hidden prompt stuffing alone
- Foundation for checkpoints and human interrupts (next lesson)
Nodes
- A node is a function (or runnable) that reads state and returns updates
- Examples - call LLM, run tools, validate schema, ask human stub
- Keep nodes focused - one job per node when possible
- Side effects belong in nodes with clear authz/timeouts
Edges
Edges define legal transitions. Fixed edges always go A→B; conditional edges compute the next node from state.
- START / END - graph entry and terminal nodes
- Normal edge - unconditional next step
- Conditional edge - router function maps state → next node name
- Cycles - allowed; always pair with max-step budgets
State
- Declare a schema (messages, flags, artifacts, counters)
- Nodes return partial updates; reducers merge into state
- Example - append messages; overwrite current_plan; increment step
- State is what routing and checkpoints (later) inspect
- Do not hide critical control data only inside free-form prose
Routing
Routing is the design-pattern name for conditional edges: classify or score state, then pick the next specialist node — with a safe default.
- Inputs - state fields (intent, confidence, errors)
- Output - next node id from an allowlisted set
- Compose - route first, then run specialist subgraphs
- Guard - max hops; log every routing decision
LangChain vs LangGraph (quick)
- LangChain chain - mostly linear Runnable pipeline
- LangChain agent - loop helper; less explicit topology
- LangGraph - you own the graph, state, and branches
- Together - LangChain components inside LangGraph nodes
Failure modes
- Infinite cycles - no step/token budget
- Fat nodes - entire app in one node; lose clarity
- Untyped state - routing on fragile strings
- Open routing - unknown intent hits privileged node
- Hidden side effects - hard to test and replay
Program / implementation notes
Design the graph on paper first: state fields, node list, edge table. Compile the graph, then invoke with an initial state. Add checkpointing in the next lesson.
- Python package: langgraph (with langchain-core pieces inside nodes)
- Next: LangGraph Checkpointing, Human Approval and Multi-Agent
Advantages and limitations
- Advantage: clear, reviewable control flow with cycles
- Advantage: typed shared state for routing and debugging
- Advantage: natural base for HITL and multi-actor designs
- Limitation: more upfront design than a simple chain
- Limitation: poor budgets still allow runaway loops
- Limitation: over-graphing tiny tasks adds noise
Key points
- LangGraph = nodes + edges + shared state + routing.
- Nodes update state; edges choose the next node.
- Conditional edges implement routing from state.
- Cycles are first-class — cap steps and tokens.
- Use LangChain tools/models inside nodes.
- Checkpoints and HITL build on this foundation (next lesson).
FAQs
Do I need LangGraph for every agent?
No. Simple tool agents or RAG chains can stay in LangChain. Choose LangGraph when you need explicit graphs, loops, branching, or durable runs.
What is a reducer in LangGraph state?
A merge rule for how a field updates — e.g. append to a message list vs replace a scalar flag.
Is routing only for multi-agent?
No. Single-agent graphs also route (tool vs end, retry vs escalate). Multi-agent graphs route between role nodes.
How do I stop graph loops?
Enforce max steps / recursion limits, token budgets, and edges that eventually reach END or escalate.
Interview questions
- What is LangGraph?
Answer A framework for building stateful LLM workflows as graphs of nodes and edges with shared state and conditional routing.
- What is a node vs an edge?
Answer A node performs work and updates state. An edge decides which node runs next — fixed or conditional.
- What role does state play?
Answer State is the typed shared memory of the run; nodes write updates and routers read it to branch.
- How does routing work in LangGraph?
Answer Conditional edges run a function on state (or model output) and return the next node name from an allowlisted set.
- When prefer LangGraph over a LangChain chain?
Answer When the workflow needs cycles, complex branching, explicit topology, or later checkpoint/HITL support.
- Give a minimal agent graph.
Answer START → agent → (tools → agent)* → END, with state holding messages and a step counter.
Related topics
Practice task
Complete each numbered practice step in a notebook or doc before opening the next lesson.
- List state fields for a support graph (messages, intent, ticket_id, step, needs_human).
- Name 5 nodes and draw fixed vs conditional edges between them.
- Write the routing function outcomes for intents: billing, tech, unknown.
- Define a max-step stop that forces END or escalate.
- Answer interview questions 1, 2, and 4 in two sentences each.