Some AI workflows are more than a simple chain. They branch, retry, ask for human approval, call tools, revise outputs, and carry state across steps. LangGraph is the pattern used when an LLM application needs explicit workflow control instead of a loose agent loop.
The main idea is simple: define state, define nodes that update state, then connect nodes with edges. The graph makes control flow visible, testable, and safer than hiding all decisions inside one prompt.
A chain is excellent when every request follows the same sequence. A graph is better when the workflow can branch: retrieve documents, decide whether evidence is enough, ask a human reviewer, call a tool, revise the answer, or stop with a refusal.
For risky workflows, the graph can pause before an irreversible action. The model may draft a refund, ticket update, or email, but a person approves before the system commits the action.
This pseudocode-style example shows the shape of a controlled workflow.
from typing import TypedDict
class SupportState(TypedDict):
question: str
documents: list
answer: str
needs_review: bool
def retrieve(state: SupportState):
docs = retriever.invoke(state["question"])
return {"documents": docs}
def answer(state: SupportState):
response = rag_chain.invoke({
"question": state["question"],
"documents": state["documents"],
})
return {"answer": response}
def grade_risk(state: SupportState):
risky = any(word in state["answer"].lower() for word in ["refund", "legal", "delete"])
return {"needs_review": risky}
def review_node(state):
attempts = state.get('attempts', 0) + 1
approved = state.get('score', 0) >= 0.8
return {**state, 'attempts': attempts, 'approved': approved}
state = {'score': 0.86, 'attempts': 0}
print(review_node(state))
{'score': 0.86, 'attempts': 1, 'approved': True}
No. Start with simple chains. Use graph workflows when branching, state, review, or long-running execution become important.
No. A graph defines explicit workflow control. An agent lets the model choose actions from tools. They can be combined, but they solve different problems.
Put it immediately before the irreversible or privileged node, with enough state for a reviewer to decide.
Finish the concept here, then reinforce it with hands-on coding, interview prep, or a tool that matches the topic.
Explore 500+ free tutorials across 20+ languages and frameworks.
Fresh tutorials, interview guides, and coding practice in your inbox.