Course Content
LangGraph Agents
7 sections · 49 lessons
How do you define nodes, add edges, and compile a graph into an executable object?
What you need to know
Python
1from typing import TypedDict2from langgraph.graph import StateGraph, START, END3from langgraph.checkpoint.memory import InMemorySaver4from langgraph.types import RetryPolicy56class State(TypedDict):7 message: str8 route: str9 reply: str1011def classify(state: State) -> dict:12 text = state["message"].lower()13 return {"route": "refund" if "refund" in text else "faq"}1415def faq(state: State) -> dict:16 return {"reply": "Here is our help article."}1718def refund(state: State) -> dict:19 return {"reply": "I have started your refund request."}2021builder = StateGraph(State)22builder.add_node("classify", classify)23builder.add_node("faq", faq)24builder.add_node("refund", refund, retry_policy=RetryPolicy(max_attempts=3))25builder.add_edge(START, "classify")26builder.add_conditional_edges("classify", lambda s: s["route"],27 {"faq": "faq", "refund": "refund"})28builder.add_edge("faq", END)29builder.add_edge("refund", END)30graph = builder.compile(checkpointer=InMemorySaver())Node details
- Signature —
fn(state), orfn(state, config), orfn(state, runtime)to reach the context and store. Async functions work too. - Return — a dict of changed keys,
None, or aCommand. - Per-node options —
retry_policy,cache_policy,defer=Truefor fan-in, anddestinationsfor drawingCommand-based routes. - Name — if you pass only the function, its name becomes the node name.
Edge details
- The path map in
add_conditional_edgeslists the possible targets, which makes the diagram accurate. - A node with no outgoing edge ends the run after it, but an explicit edge to
ENDis clearer.
What compile attaches
checkpointer, store, cache, interrupt_before, interrupt_after and a name. Use graph.get_graph().draw_mermaid() to render it.
A real-life example
A payments company's dispute bot uses this exact shape. In code review, a senior engineer asks for two things: retry_policy on the refund node, because the payment gateway sometimes returns a 503, and a Mermaid diagram in the pull request. The diagram shows that the faq path has no way to reach a human. The team adds an escalate node before merging, instead of after a customer complaint.
Follow-up questions to expect
- "When are
interrupt_beforeandinterrupt_afteruseful?" — For debugging and demos: they pause before or after a node without changing its code. For real approvals, callinterrupt()inside the node so you can send a payload and use the reply. - "Can you add nodes after compiling?" — No. Build a new graph and compile again; compiled graphs are fixed.
- "How do you give a node access to a database client?" — Put it in the runtime context via
context_schema, and read it withruntime.contextin the node.