By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A StateGraph is a builder. You hand it a state schema, register nodes, connect them with edges, and call compile() to get a runnable. START and END are the graph's boundaries, not nodes you write. The two-node example below is the whole runtime in miniature: seed the state, run a node, merge its update, follow an edge, run the next node, stop at END.
from typing import TypedDict
from langgraph.graph import END, START, StateGraph
class TicketState(TypedDict):
message: str
intent: str | None
reply: str | None
def classify(state: TicketState) -> dict:
intent = "refund" if "refund" in state["message"].lower() else "order_status"
return {"intent": intent}
def draft_reply(state: TicketState) -> dict:
return {"reply": f"Ticket routed as {state['intent']}."}
builder = StateGraph(TicketState)
builder.add_node("classify", classify)
builder.add_node("draft_reply", draft_reply)
builder.add_edge(START, "classify")
builder.add_edge("classify", "draft_reply")
builder.add_edge("draft_reply", END)
graph = builder.compile()
print(type(graph).__name__)
print(sorted(graph.get_graph().nodes))
result = graph.invoke(
{"message": "I need a refund", "intent": None, "reply": None}
)
print(result["intent"], "|", result["reply"])None if it changes nothing).a, always run b. START and END are valid on either side.compile() turns the builder into a runnable and performs structural checks first: an edge that targets a node you never added, or a malformed graph, fails here instead of mid-request. Compiling is also where runtime features are attached — compile(checkpointer=...) for persistence, for example. Keep compilation at the edge of your application (a factory function), so tests and production build the graph the same way.
The compiled object exposes get_graph() for structure and invoke() / stream() / get_state() for execution. get_graph().draw_mermaid() renders the same structure as text, which is useful in a README or a pull request description when you change routing.
invoke() returns the final state as a dict — not the last node's return value. That is deliberate: the caller gets one consistent object regardless of which branch ran. If you need the history of what happened, add a checkpointer (Lesson 7); if you need progress while it runs, use stream() (Lesson 11).
Key takeaways
A StateGraph is a builder: schema, nodes, edges, then compile() returns a runnable.
START and END are boundaries; every other name is a node you registered.
invoke() seeds state, walks the edges and returns the final merged state.
Name nodes after the action they take — the name is the unit of tracing, routing and review.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.