Why Graphs, Not Chains
A chain runs its steps in a fixed order. A graph can choose the next step and go back to an earlier one. Learn the difference with everyday examples, real code, and your first LangGraph program.
What you will be able to do
- Understand the words used in this module: workflow, chain, graph, node, edge, state, cycle
- Explain what a chain is, and which kind of work it suits
- Say exactly what a chain can do (choose a path, retry an error) and what it cannot do (go back because of a result)
- Explain what a graph is, using an everyday example
- Read and run your first LangGraph program, line by line
- Explain why every AI agent is a graph
- Explain why a loop needs state and a limit
- Decide, for a new task, whether you need a chain or a graph
The idea, in plain English
This module teaches LangGraph: a Python library for building workflows as graphs. A workflow is the list of steps that finish a job - read a support ticket, find the order, write a reply.
In Module 2 you built chains: step 1, then 2, then 3, always in the same order. Some jobs cannot be a straight line. A payment check says "pending" - you must go back and check again, maybe three times, then hand it to a human. A chain cannot go back to an earlier step. A graph can.
We compare chains and graphs with everyday examples and real code, and write a first small LangGraph program. LangChain 1.4.3, LangGraph 1.2.14, llama3 on Ollama.
Worked example: A payment support workflow that checks a transaction, checks again while it is "pending", and asks a human after 3 tries.
1 - Classify, then choose a path
The classify step decides the question is about billing. Then the workflow chooses the next step from that answer. A chain can also do this (with RunnableBranch).
Customer: "My payment failed but money was deducted." This run is real: the payment check returned pending, pending, then settled.
The model and the tools take turns until the model has the answer. Nobody knows in advance how many turns that takes.
Words you will see in this module
Come back to this table whenever a word is not clear.
WorkflowAll the steps that together finish one job. Example: read ticket -> find order -> write reply.StepOne piece of work inside a workflow, like "call the LLM" or "check the payment".ChainA workflow whose steps always run in the same fixed order (LangChain, Module 2).GraphA workflow drawn as boxes and arrows, where the next step can change and arrows can go back.NodeA box in the graph: one step. In LangGraph a node is a Python function.EdgeAn arrow in the graph: it says which node runs next.Conditional edgeAn arrow with a choice: a small function looks at the data and picks the next node.Cycle (loop)An arrow that goes back to a node that already ran, so that step can run again.StateThe shared data of one run - the question, the answers so far, a counter. Every node can read it and change it.START / ENDWhere a run begins and where it stops.CompileTurn your graph definition into something you can run.InvokeRun it once with some input: graph.invoke(...).An everyday example: making tea and washing clothes
- Making tea is a chain: boil water, add tea, add milk and sugar, pour. Same steps, same order, never back.
- A washing machine is a graph: wash, then check - still dirty? Wash again. Water not drained? Wait and check again. Then rinse, spin, finish.
- The next step depends on what the machine finds, and some steps run many times.
- Software is the same: same steps for every input -> chain. Next step depends on a result, or a step may repeat -> graph.
Making teaChain. Same steps, same order, every time.Washing machineGraph. "Still dirty?" sends it back to wash again.Printing a documentChain. Open -> format -> print.Calling customer careGraph. "Press 1 for billing" chooses a path; "invalid option, try again" goes back.Google Maps navigationGraph. Missed a turn? It goes back to "plan the route" and plans again.What a chain is good at
- A chain is a pipeline: each step takes the output of the step before (prompt | llm | parser, Module 2).
- Good for: summarize, translate, classify, extract JSON - the steps are known before you start.
- The path never changes, so a chain is easy to read, test and fix. For this kind of work, do not replace it with a graph.
- Example 1 always runs exactly three steps.
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_ollama import ChatOllama
# Step 1: a prompt template with one empty slot, {ticket}
prompt = ChatPromptTemplate.from_template(
"Summarize this support ticket in one short sentence:\n\n{ticket}"
)
# Step 2: the language model
llm = ChatOllama(model="llama3", temperature=0)
# Step 3: turn the model's message into a plain string
parser = StrOutputParser()
# The | symbol joins the steps into a chain: prompt, then llm, then parser
chain = prompt | llm | parser
ticket = (
"Hi, I paid for the Python course yesterday. The payment page showed an error, "
"but the money was taken from my bank account. I still cannot open the course."
)
print(chain.invoke({"ticket": ticket}))The customer paid for a Python course yesterday, despite an error on the payment page, but is still unable to access the course.A chain can choose a path
"Chains cannot make decisions" is not fully true:
- RunnableBranch checks conditions one by one and runs the first branch whose condition is true. The last item is the default.
- Example 2: a keyword function decides the category; billing goes to billing, technical to tech, the rest to the general desk.
- But after choosing, it still only moves FORWARD.
from langchain_core.runnables import RunnableBranch, RunnableLambda
def classify(question):
q = question.lower()
if any(w in q for w in ("payment", "refund", "invoice", "charged")):
return "billing"
if any(w in q for w in ("error", "crash", "login", "bug")):
return "technical"
return "general"
route = RunnableBranch(
# (condition, what to run if the condition is true)
(lambda x: x["category"] == "billing", RunnableLambda(lambda x: f"billing team: {x['question']}")),
(lambda x: x["category"] == "technical", RunnableLambda(lambda x: f"tech team: {x['question']}")),
# the default: runs when no condition above is true
RunnableLambda(lambda x: f"general desk: {x['question']}"),
)
# step 1 adds the category, step 2 chooses the branch
chain = RunnableLambda(lambda q: {"question": q, "category": classify(q)}) | route
print(chain.invoke("My payment failed"))
print(chain.invoke("Login shows an error"))
print(chain.invoke("What courses do you have?"))billing team: My payment failed
tech team: Login shows an error
general desk: What courses do you have?A chain can retry an error
- with_retry(stop_after_attempt=3): if this step raises an error, try again - up to 3 times in total.
- Example 3: a pretend service fails twice with TimeoutError and works the third time.
- But with_retry only reacts to ERRORS, and only repeats this one step.
from langchain_core.runnables import RunnableLambda
attempts = {"n": 0}
def flaky_service(_):
attempts["n"] += 1
if attempts["n"] < 3:
raise TimeoutError("service timeout") # fails on try 1 and try 2
return f"ok on attempt {attempts['n']}"
step = RunnableLambda(flaky_service).with_retry(stop_after_attempt=3, wait_exponential_jitter=False)
print(step.invoke(None))
# with_retry works only when the step RAISES AN ERROR,
# and it repeats only this one step.
# A bank answer "pending" is a normal result, not an error -
# so with_retry does nothing, and nothing sends the run back.ok on attempt 3What a chain cannot do: go back because of a result
Here is the real limit of a chain:
- The payment check returns "pending". That is not an error, so with_retry does nothing. It is a normal answer that means "ask again later".
- The workflow must go back to the check - once, or three times. Nobody knows before the run starts.
- An AI agent is the same: after a tool answers, the model may ask for another tool or answer. The number of rounds is decided while it runs.
- A chain’s order is fixed when you write the code. Going back needs a loop: a while loop in plain Python (Example 5), or an arrow in LangGraph (Example 6) that you can see, print and control.
Steps in a fixed orderChain: yes - its natural shape. Graph: yes.Choose one path by a conditionChain: yes, with RunnableBranch. Graph: yes, with conditional edges.Repeat a step that raised an errorChain: yes, with with_retry. Graph: yes.Go back because of a resultChain: no. Graph: yes - an edge back to an earlier node.Loop until the model stops asking for toolsChain: no. Graph: yes - the agent loop.Shared data for all stepsChain: each step only gets what the step before passes on. Graph: one state that every node reads and updates.Pause and wait for a humanChain: no. Graph: yes - Lesson 3.7.What is a graph?
- Nodes are the boxes: one piece of work each - call an LLM, run a tool, check a value, ask a human.
- Edges are the arrows: which node runs next.
- A normal edge always goes to the same node.
- A conditional edge calls a small routing function that looks at the data and returns the next node’s name.
- An edge back to a node that already ran makes a cycle - a loop. Cycles make retries and agent loops possible.
NodeOne piece of work: classify, check, respond.EdgeAlways run this node next.Conditional edgeA function looks at the state and picks the next node.CycleAn edge back to an earlier node.START / ENDWhere a run enters and where it leaves.Your first LangGraph program
Install it first: pip install langgraph. The nodes here are plain Python - no LLM. The program has four parts:
- 1. State: a TypedDict with the data of one run - a name and a message.
- 2. Nodes: functions that receive the state and return only the fields they change.
- 3. Edges: START -> greet -> shout -> END.
- 4. Compile and invoke: compile() checks and builds the graph; invoke({"name": "Ravi"}) runs it once.
- A straight-line graph is allowed - it does not have to branch or loop. Lesson 3.2 explains each part in detail.
from typing import TypedDict
from langgraph.graph import END, START, StateGraph
# 1. The state: the data that every node can read
class State(TypedDict):
name: str
message: str
# 2. The nodes: plain Python functions.
# Each one receives the state and returns only what it changes.
def greet(state: State):
print(" greet is running")
return {"message": f"Hello, {state['name']}!"}
def shout(state: State):
print(" shout is running")
return {"message": state["message"].upper()}
# 3. The graph: add the nodes, then connect them with edges
builder = StateGraph(State)
builder.add_node("greet", greet)
builder.add_node("shout", shout)
builder.add_edge(START, "greet") # the run starts at greet
builder.add_edge("greet", "shout") # after greet, run shout
builder.add_edge("shout", END) # after shout, stop
# 4. Compile, then run once
graph = builder.compile()
result = graph.invoke({"name": "Ravi"})
print(result) greet is running
shout is running
{'name': 'Ravi', 'message': 'HELLO, RAVI!'}__start__ --> greet;
greet --> shout;
shout --> __end__;
How to read it:
__start__ / __end__ START and END
--> a normal edge (always goes this way)
-.-> a conditional edge (a function chooses) - you will see it belowEvery AI agent is a graph
An agent works in a loop: the model reads the question, asks for a tool if it needs one, reads the result, and decides again.
- A real run from Lesson 3.5: "Should I bring an umbrella in Mumbai, and what is 12 * 8?"
- model -> get_weather("Mumbai") -> "Rainy, 27°C" -> model -> calculate("12 * 8") -> 96 -> model -> the answer.
- Two rounds here. Another question may need zero, or five.
- You already used this graph: create_agent (Lesson 2.7) returned a LangGraph graph with nodes model and tools and a loop between them.
- In this module you build that loop yourself (Lessons 3.4, 3.5), so you can change it - for example add a human approval (Lesson 3.7).
Loops need state and a limit
- State: to decide "again or stop?", the loop must remember what happened - the last status and how many checks. Our support graph’s state holds question, category, attempts and status. Lesson 3.2 is all about state.
- Your own limit: our routing function escalates after 3 attempts.
- LangGraph’s safety net: recursion_limit, the maximum steps in one run. With recursion_limit=4 and a payment pending forever, it stopped with GraphRecursionError.
Watch out: A loop without an exit rule runs until it hits recursion_limit and fails with an error. Always write your own exit rule (a success check and a maximum count). recursion_limit is only the safety net.
Example 5 and 6 - the support workflow, two ways
The same payment-support workflow, written twice. Compare them:
- Example 5 is plain Python. The loop is hidden inside while True and if statements.
- Example 6 is LangGraph. The loop is an edge, check -.-> check, that you can print and see.
- Both gave three different paths for the same question. As workflows grow - more branches, loops, pauses for humans - the visible version stays easier to understand.
# Uses classify() from Example 2.
# service_results pretends to be the bank: one answer per check.
def run_support(question, service_results, max_attempts=3):
state = {"question": question, "category": classify(question), "attempts": 0, "path": ["classify"]}
if state["category"] != "billing":
state["path"].append(state["category"])
return state
while True: # the loop (the cycle)
state["attempts"] += 1
state["status"] = service_results[state["attempts"] - 1]
state["path"].append(f"check#{state['attempts']}={state['status']}")
if state["status"] == "settled": # exit 1: success
state["answer"] = "Payment went through."
break
if state["attempts"] >= max_attempts: # exit 2: too many tries
state["path"].append("escalate")
state["answer"] = "Escalated to a human."
break
state["path"].append("respond")
return state
question = "My payment failed but money was deducted"
for results in (["settled"], ["pending", "pending", "settled"], ["pending"] * 4):
s = run_support(question, results)
print(" -> ".join(s["path"]), "|", s["answer"])classify -> check#1=settled -> respond | Payment went through.
classify -> check#1=pending -> check#2=pending -> check#3=settled -> respond | Payment went through.
classify -> check#1=pending -> check#2=pending -> check#3=pending -> escalate -> respond | Escalated to a human.
run_support("Login shows an error", [])["path"] -> classify -> technical# Uses classify() from Example 2 and question from Example 5.
from typing import TypedDict
from langgraph.graph import END, START, StateGraph
# THE STATE: everything the run needs to remember
class SupportState(TypedDict, total=False):
question: str
category: str
attempts: int
status: str
answer: str
results: list # the pretend bank answers
path: list # which nodes ran, for printing
# THE NODES: each does one job and returns what it changes
def classify_node(s):
return {"category": classify(s["question"]), "attempts": 0, "path": ["classify"]}
def check(s):
attempt = s["attempts"] + 1
status = s["results"][attempt - 1]
return {"attempts": attempt, "status": status, "path": s["path"] + [f"check#{attempt}={status}"]}
def technical(s): return {"answer": "technical answer", "path": s["path"] + ["technical"]}
def general(s): return {"answer": "general answer", "path": s["path"] + ["general"]}
def escalate(s): return {"answer": "Escalated to a human.", "path": s["path"] + ["escalate"]}
def respond(s): return {"answer": s.get("answer") or "Payment went through.", "path": s["path"] + ["respond"]}
# THE ROUTING FUNCTIONS: they return the NAME of the next node
def after_classify(s): # choose a path
return {"billing": "check", "technical": "technical"}.get(s["category"], "general")
def after_check(s): # the loop, and its two exits
if s["status"] == "settled":
return "respond"
return "escalate" if s["attempts"] >= 3 else "check"
# THE GRAPH: nodes + edges
builder = StateGraph(SupportState)
for name, fn in [("classify", classify_node), ("check", check), ("technical", technical),
("general", general), ("escalate", escalate), ("respond", respond)]:
builder.add_node(name, fn)
builder.add_edge(START, "classify")
builder.add_conditional_edges("classify", after_classify, ["check", "technical", "general"])
builder.add_conditional_edges("check", after_check, ["respond", "escalate", "check"])
for name in ("technical", "general", "escalate"):
builder.add_edge(name, "respond")
builder.add_edge("respond", END)
graph = builder.compile()
out = graph.invoke({"question": question, "results": ["pending", "pending", "settled"]})
print(" -> ".join(out["path"]))classify -> check#1=pending -> check#2=pending -> check#3=settled -> respond
graph.get_graph().draw_mermaid() - the edges, as LangGraph compiled them
__start__ --> classify
classify -.-> check classify -.-> technical classify -.-> general
check -.-> respond check -.-> escalate check -.-> check <- the loop
technical --> respond general --> respond escalate --> respond
respond --> __end__
( -.-> conditional edge, --> always )
The same three runs as Example 5 - the graph gives the same paths:
classify -> check#1=settled -> respond
classify -> check#1=pending -> check#2=pending -> check#3=settled -> respond
classify -> check#1=pending -> check#2=pending -> check#3=pending -> escalate -> respond
graph.invoke({... "results": ["pending"] * 50}, {"recursion_limit": 4})
GraphRecursionError: Recursion limit of 4 reached without hitting a stop condition.Tip: Compare Example 5 and Example 6. They do the same thing. In plain Python the loop is hidden inside while True and if statements. In LangGraph the loop is an edge, check -.-> check, that you can print and see. As workflows grow - more branches, more loops, pauses for humans - that visibility is what keeps them understandable.
Practice: chain or graph?
Decide before you look. Ask: does the next step depend on a result, or might a step run again?
Translate an email to TeluguChain. Prompt -> LLM -> text, the same for every email.Summarize a PDFChain. Load -> split -> summarize. No step goes back.Route a ticket to billing, tech or generalEither. A chain with RunnableBranch is enough if nothing comes back.Check a payment until it is settled, max 3 timesGraph. "Pending" sends it back to check.Chatbot that can use a calculator and web searchGraph. The model decides how many tool calls to make - a loop.Write code, run tests, fix and retry until tests passGraph. Failing tests send it back to "fix".Draft an email, wait for the manager to approve, then sendGraph. It must pause and continue later (Lesson 3.7).Extract name, phone and city from a form as JSONChain. Prompt -> LLM -> JSON parser.When to use which - and common questions
- Chain: every input follows the same steps. Graph: the next step depends on what happened.
- Habit: draw the workflow on paper first. If every arrow points to the next box, write a chain.
- Do I need an LLM to use LangGraph? No. The support graph has no LLM at all.
- Does LangGraph make the model smarter? No. It controls the workflow around the model: which step, how many times, when to stop or wait.
- Is LangGraph only for multi-agent systems? No. One tool loop or one retry is already a good reason. Multi-agent (Lesson 3.8) is just one use.
Chains and graphs at a glance
ChainSteps in a fixed order.
prompt | llm | parser
RunnableBranchA chain choosing one of several paths.
RunnableBranch((condition, runnable), ..., default)
with_retryRepeat one step when it raises an error.
step.with_retry(stop_after_attempt=3)
Install LangGraphThe library used in this module.
pip install langgraph
StateGraphA graph whose nodes share one state.
StateGraph(State)
EdgeAlways go to this node next.
builder.add_edge("greet", "shout")Conditional edgeA function picks the next node.
add_conditional_edges("check", after_check)Run itCompile once, then invoke.
graph = builder.compile(); graph.invoke({...})recursion_limitThe maximum number of steps in one run.
graph.invoke(state, {"recursion_limit": 10})draw_mermaidPrint the graph’s nodes and edges.
graph.get_graph().draw_mermaid()
Try it yourself
The code does not change. Swap the content string and the program does something else entirely.
“In Example 4, add a third node "add_emoji" between shout and END that adds " :)" to the message. Run it and print the graph.”
“On paper, draw a password-reset workflow: request, send code, verify, try again up to 3 times, lock the account. Circle the arrows that go back.”
“In Example 5, remove the max_attempts check and give ["pending"] * 10. What happens? Then put the check back.”
“In Example 2, add an "account" branch for questions about passwords.”
“In Example 6, invoke with ["pending"] * 50 and {"recursion_limit": 4}. Read the error. Then remove the limit and see your own rule stop it.”
“Think of an app you use - food delivery, train booking. Name two places where a step goes back because of a result.”
What usually goes wrong
Prompt -> LLM -> parser is a chain. Building a graph for it only adds code. Use a graph when something can branch, repeat or wait.
RunnableBranch chooses a path, and with_retry repeats a step after an error. The real limit is going back because of a normal result.
The routing function must, at some point, return a node that leaves the loop. Count the attempts in the state and stop at a maximum.
✗ return "check" if s["status"] != "settled" else "respond"✓ if s["status"] == "settled": return "respond"
return "escalate" if s["attempts"] >= 3 else "check"A graph is the structure of a workflow. It can have one model, many, or none - the support graph here has no LLM at all.
Edges only list the possible paths. Your routing functions - or a model inside a node - choose which path is taken.
Key points
- A workflow is the list of steps that finish a job.
- A chain runs its steps in a fixed order - best when every input takes the same steps.
- Chains can choose a path (RunnableBranch) and retry an error (with_retry).
- A chain cannot go back to an earlier step because of a result - a graph can.
- A graph is nodes (pieces of work) and edges (what runs next); a conditional edge chooses the next node.
- An edge back to an earlier node is a cycle (loop) - the base of retries and agents.
- Every AI agent is a loop: model -> tool -> model, until there is no tool call.
- A loop needs state to decide and a limit to stop: your own rule first, recursion_limit as the safety net.
- A LangGraph program has four parts: state, nodes, edges, compile + invoke.
Quick check before you move on
Quiz
- 1.
with_retry repeats a failing step. Why is that not enough for "check again while the payment is pending"?
- 2.
In the printed graph you see check -.-> check. What does it mean?
- 3.
In Example 4, which function runs first, and why?
- 4.
What stops a loop that never reaches its exit?
- 5.
Does a graph have to contain an LLM?
- 6.
A task: "Write code, run the tests, and fix the code until the tests pass." Chain or graph?
Interview questions
Why would you use LangGraph instead of a LangChain chain?
When the workflow needs control that a fixed sequence cannot give: loops driven by results, agent tool loops, shared state, saving progress, or pausing for human approval. For a fixed sequence, a chain is simpler.
Chains support branching and retries. So what do graphs add?
Cycles and shared state. A chain can choose a branch and retry an exception, but it cannot go back to an earlier step because of a result, and it has no single state that every step reads and updates.
How is an agent different from a chain?
A chain’s steps are fixed when you write the code. An agent’s path is chosen while it runs - the model decides which tools to call and how many times - so it is a loop with a condition decided by the model.
What are nodes and edges in LangGraph?
Nodes are functions that do work and return updates to the state. Edges decide what runs next; conditional edges call a routing function on the state to pick the next node.
How do you prevent infinite loops in a graph?
Write an explicit exit in the routing function - a success condition and a maximum count kept in the state - and keep recursion_limit as the hard stop.
Is LangGraph only for multi-agent systems?
No. A single agent with one tool loop, or a workflow with one retry, already benefits. Multi-agent systems are one use among many.
Comments
Sign in to leave a comment. Your name and photo come from Google; nothing else is shared.
Loading comments...