LangGraph Agents

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
from typing import TypedDictfrom langgraph.graph import StateGraph, START, ENDfrom langgraph.checkpoint.memory import InMemorySaverfrom langgraph.types import RetryPolicyclass State(TypedDict):    message: str    route: str    reply: strdef classify(state: State) -> dict:    text = state["message"].lower()    return {"route": "refund" if "refund" in text else "faq"}def faq(state: State) -> dict:    return {"reply": "Here is our help article."}def refund(state: State) -> dict:    return {"reply": "I have started your refund request."}builder = StateGraph(State)builder.add_node("classify", classify)builder.add_node("faq", faq)builder.add_node("refund", refund, retry_policy=RetryPolicy(max_attempts=3))builder.add_edge(START, "classify")builder.add_conditional_edges("classify", lambda s: s["route"],                              {"faq": "faq", "refund": "refund"})builder.add_edge("faq", END)builder.add_edge("refund", END)graph = builder.compile(checkpointer=InMemorySaver())

Node details

  • Signature — fn(state), or fn(state, config), or fn(state, runtime) to reach the context and store. Async functions work too.
  • Return — a dict of changed keys, None, or a Command.
  • Per-node options — retry_policy, cache_policy, defer=True for fan-in, and destinations for drawing Command-based routes.
  • Name — if you pass only the function, its name becomes the node name.

Edge details

  • The path map in add_conditional_edges lists the possible targets, which makes the diagram accurate.
  • A node with no outgoing edge ends the run after it, but an explicit edge to END is 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_before and interrupt_after useful?" — For debugging and demos: they pause before or after a node without changing its code. For real approvals, call interrupt() 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 with runtime.context in the node.