AI Engineer Roadmap All lessons Interactive Quiz MernStackDev GitHub

LangGraph

Play the interactive lesson: LangGraph: a graph of stepsRead on GitHub
On this page
  1. Explain it like I'm new
  2. Watch it
  3. How it works
  4. Code example
  5. When to use it / when not to
  6. Common mistakes
  7. Recap
  8. Interview questions
  9. Go deeper

Level: Intermediate · Reading time: 12 min · Phase: 5 — Frameworks

In one line: LangGraph lets you describe an agent or workflow as a graph of steps (nodes) joined by edges, with shared state passed along.

Before you start: You should understand a simple agent loop (think, act, repeat) from Agent loops and a little of what a tool call is (Tool calling).

Explain it like I'm new

Think of a subway map. Stations are steps. Tracks say where you can go next. Some stations are junctions: you pick a track based on a sign. A loop line lets you go around again.

A plain agent loop (Phase 4) is while not done: think, act. That works until you need branches, retries, human approval or several agents. A graph makes the control flow explicit and visible, so you can reason about it, test it and pause it.

Words to know:

Watch it

LangGraph: a graph of steps: Nodes do the work, edges decide what comes next, state is shared.
LangGraph: a graph of steps: Nodes do the work, edges decide what comes next, state is shared.

It plays on a loop. Read the caption in the box, one step at a time. Want to go at your own pace? Step through it with Previous and Next.

How it works

  1. Define the state shape.
  2. Add nodes (functions) and edges.
  3. Compile the graph.
  4. Invoke with an initial state. The runtime runs a node, merges its updates into state, follows the edge, and repeats until END.
flowchart LR
    S((START)) --> A[agent: call model]
    A -->|tool call requested| T[tools: run function]
    T --> A
    A -->|final answer| E((END))

Mapping to earlier phases: the agent loop from Phase 4 is this exact graph: model node, tool node, and a conditional edge that checks "did the model ask for a tool?". Memory (Phase 4) maps to state plus a checkpointer.

Worked example

This trace comes from the repo's offline demo (fw_mini_graph.py). It is a "write, then review" loop. The write node adds the word word to a draft. The review node marks ok as true once the draft has at least 3 words. The router goes to END if ok is true, else back to write. I added print lines and ran it to get these real values:

#Node runsUpdate returnedRouter decision
1writedraft = 'word '(fixed edge to review)
2reviewok = False (1 word)back to write
3writedraft = 'word word '(fixed edge to review)
4reviewok = False (2 words)back to write
5writedraft = 'word word word '(fixed edge to review)
6reviewok = True (3 words)END

The final state is {'draft': 'word word word ', 'ok': True}. The demo also records the visited nodes as ['write', 'review', 'write', 'review', 'write', 'review']. Notice that nodes never call each other. They only return updates, and the runner merges them into the shared state. A real LangGraph app has the same shape, but check the current docs for exact imports.

Code example

From scratch, using this repo's fw_mini_graph.py:

from fw_mini_graph import MiniGraph, END

g = MiniGraph()
g.add_node("write", lambda s: {"draft": s.get("draft", "") + "word "})
g.add_node("review", lambda s: {"ok": len(s["draft"].split()) >= 3})
g.set_entry("write").add_edge("write", "review")
g.add_conditional_edge("review", lambda s: END if s["ok"] else "write")

print(g.run({})["draft"])   # word word word

In real projects (pip install langgraph):

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

class State(TypedDict):
    text: str

def shout(state: State) -> dict:
    return {"text": state["text"].upper()}

builder = StateGraph(State)
builder.add_node("shout", shout)
builder.add_edge(START, "shout")
builder.add_edge("shout", END)
graph = builder.compile()
print(graph.invoke({"text": "hello"}))   # {'text': 'HELLO'}

Conditional routing uses add_conditional_edges; see the docs for its exact form.

When to use it / when not to

Common mistakes

Recap

If you remember only three things:

Interview questions

  1. Why a graph instead of a while loop? <details><summary>Answer</summary>Control flow becomes explicit data. You can visualize it, add branches, checkpoint state and pause for a human without tangled code.</details>
  2. What does a checkpointer give you? <details><summary>Answer</summary>Saved state after each step: conversation memory across turns, resuming after failure, and human-in-the-loop interrupts.</details>
  3. What is a conditional edge? <details><summary>Answer</summary>An edge whose destination is chosen by a function of the current state, such as "if the model requested a tool, go to the tools node".</details>

Go deeper

Prev: LangChain · Next: LlamaIndex · Back to phase: Phase 5