add_edge: two steps in a row
An edge is a connection between two nodes: add_edge(a, b) tells LangGraph that node b runs after node a.
Last updated: 27 Sep, 2026 · LangGraph 1.2
One node is not a program. The moment you have two, you have to say which runs first, and an edge is how you say it. The second node can then read what the first one wrote to the state.
The add_edge call
from langgraph.graph import START, END
builder.add_edge(START, "first") # the run begins at "first"
builder.add_edge("first", "second") # "second" runs after "first"
builder.add_edge("second", END) # the run ends after "second"The state
Describe a state with three keys: the ticket in, the category the first node writes, and the reply the second node writes.
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START
class State(TypedDict):
ticket: str
category: str
reply: strThe categorise node
The first node sets the category.
def categorise(state):
return {"category": "billing"}The write_reply node
The second node reads that category from the state and writes a reply that uses it.
def write_reply(state):
# read category from the state the first node wrote
return {"reply": f"Sending this to our {state['category']} team."}Wiring the edge
Register both nodes, then add the edge that puts categorise before write_reply.
builder = StateGraph(State)
builder.add_node("categorise", categorise)
builder.add_node("write_reply", write_reply)
builder.add_edge(START, "categorise")
builder.add_edge("categorise", "write_reply") # categorise runs first, then write_reply
graph = builder.compile()Running the graph
Run it and print the reply key from the final state.
print(graph.invoke({"ticket": "charged twice", "category": "", "reply": ""})["reply"])Two nodes in a row
The same pieces in one file, ready to run.
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START
class State(TypedDict):
ticket: str
category: str
reply: str
def categorise(state):
return {"category": "billing"}
def write_reply(state):
return {"reply": f"Sending this to our {state['category']} team."}
builder = StateGraph(State)
builder.add_node("categorise", categorise)
builder.add_node("write_reply", write_reply)
builder.add_edge(START, "categorise")
builder.add_edge("categorise", "write_reply")
graph = builder.compile()
print(graph.invoke({"ticket": "charged twice", "category": "", "reply": ""})["reply"])How the value crossed the edge
- The second
add_edgeis the new part: it is the only thing that sayscategoriseruns beforewrite_reply. - The word billing was not written by
write_reply. It came out of the state, wherecategoriseput it a moment earlier. - Passing values between nodes through the state is the whole point of an edge.
When one step feeds the next
- Any flow with more than one step: classify then reply, retrieve then answer, validate then act.
- Whenever a later step needs something an earlier step worked out.
add_node lines changes nothing. Nodes are only registered by name; the edges decide the order.Related
- Add a third node
logafterwrite_replythat prints the reply. Wire the edge. - Remove the
categorise → write_replyedge and run it. What is inreplynow? - Swap the two
add_nodelines and confirm the output does not change.
Every expert started right here.