Course Content
LangGraph Agents
7 sections · 49 lessons
What are nodes and edges in LangGraph, and what do they represent in execution?
What you need to know
1from typing import TypedDict2from langgraph.graph import StateGraph, START, END34class State(TypedDict):5 question: str6 docs: list[str]7 answer: str89def retrieve(state: State) -> dict:10 return {"docs": [f"policy text about {state['question']}"]}1112def generate(state: State) -> dict:13 return {"answer": f"Based on {len(state['docs'])} document(s): ..."}1415builder = StateGraph(State)16builder.add_node("retrieve", retrieve)17builder.add_node("generate", generate)18builder.add_edge(START, "retrieve")19builder.add_edge("retrieve", "generate")20builder.add_edge("generate", END)21graph = builder.compile()22print(graph.invoke({"question": "return window"}))This runs as written. Each node reads the state and returns only its own keys.
Kinds of edges
- Static —
add_edge("a", "b"): aftera, always runb. - Conditional —
add_conditional_edges("a", route_fn, {...}):route_fn(state)returns a name, a list of names, orSendobjects. - Several outgoing static edges — from one node to two nodes means both run in the next super-step, in parallel (fan-out).
Commandfrom a node — a node can returnCommand(update=..., goto="b"), combining the update and the next hop.
How execution uses them
Execution is breadth-first by super-step. All nodes scheduled in a step run, their updates are merged, then edges from every node that just ran decide the next step. If two edges lead into one node from branches of equal length, that node runs once after both finish. If the branches have different lengths, it may run more than once unless you mark it defer=True.
A real-life example
A telecom support bot has five nodes: classify, lookup_account, troubleshoot, create_ticket and reply. The conditional edge after classify sends "bill too high" to lookup_account, "internet slow" to troubleshoot, and anything with the word "cancel" to create_ticket for a human retention team. When the team drew the graph with graph.get_graph().draw_mermaid(), the product manager spotted that "slow internet" users never reached create_ticket even after three failed fixes — a missing edge, found by reading the diagram rather than the code.
Follow-up questions to expect
- "Can a node call another node?" — No. Nodes never call each other; they communicate only through state, and edges decide the order.
- "What can a node return?" — A dict of changed keys,
Nonefor no change, or aCommandthat also chooses the next node. - "Are
STARTandENDreal nodes?" — They are virtual markers. You add edges fromSTARTand toEND, but they run no code.