Component brief / Developer Tools
LangChain and LangGraph: what they do and when you need them.
What it is
Imagine you are adding a Draft reply button to a support application.
The simplest version reads a case from your database, sends the text to an AI model and displays the suggested reply. You can write that with ordinary application code and the model provider's API. You do not need LangChain or LangGraph for this.
Now suppose the AI needs to decide whether to read earlier cases or search a product manual before writing. You give it functions such as ReadCase and SearchManual. In AI terminology, these functions are tools.
LangChain handles the repeated exchange: send the request to the model, run the function it asks for, return the result, and ask the model again. A model using tools this way is called an agent. Your code still implements the functions and checks permissions.
LangGraph lets you define the steps of a job and what happens next. For example: draft a reply, wait for someone to approve it, then send it. A graph is the steps and the connections between them.
Where it fits
The AI model writes the reply and may request information. LangChain handles the conversation between that model and your functions. LangGraph can manage the overall job.
Your application still supplies the database access, search, review screen and send function. Neither library is a finished support assistant.
A LangChain agent already uses LangGraph internally. You can use that ready-made agent without defining a graph yourself. You define your own steps when the default behaviour does not fit your application.
When to use it
The three Python examples later in Integration example show these versions of the same feature, in this order:
- Read a case and draft a reply: ordinary application code may be enough.
- Let the AI decide which cases or manuals to consult: a LangChain agent can handle the requests and results.
- Draft, pause for review, then send only if approved: a custom LangGraph workflow can manage those steps.
LangGraph can save progress if you configure somewhere to store it. That is useful when a person might review the reply tomorrow. Its pause-and-resume mechanism lets the job wait for their decision. You build the review screen and the code that handles approval or rejection.
Trade-offs
You are exchanging some code you would otherwise write for libraries you must learn and maintain.
If the AI can choose several functions, one user request may cause several model calls. Set limits on time and spending. Check that the selected functions return useful information and that the reply is correct.
If you save progress, you also save application data, which may include customer text. Decide who can read it and when it should be deleted. Make the send function resist duplicate sends if a step is retried.
Alternatives
You can build all three versions yourself. For a fixed sequence, ordinary functions, a database record holding the job's status and a background worker may be enough. An existing workflow engine is another option.
Use these libraries when the parts they handle are worth the added dependency. A feature does not need an agent simply because it calls an AI model.
Integration example
These three Python examples build on each other. They use a fictional case and manual so you can see what each library contributes. Install the packages and set OPENAI_API_KEY in your environment before trying them:
python -m pip install langchain langchain-openai langgraph
Model calls use your provider account and incur its normal charges.
1. Your code chooses the information
Here we use LangChain's model interface to make one model call. There is no agent: your code supplies the case and decides when to call the model. You could also make this call with the provider's own SDK.
from langchain_openai import ChatOpenAI
model = ChatOpenAI(model="gpt-5.4")
case_text = "The customer cannot find the password reset email."
reply = model.invoke([
("system", "Draft a short support reply. Do not invent facts."),
("human", case_text),
])
draft = reply.content
draft contains the suggested reply. The model has no function for reading another case or searching a manual. It only receives the text you supplied.
2. The model can request your functions
Now give a LangChain agent two functions. These return sample text; a real application would read its database and search its documentation.
from langchain.agents import create_agent
def read_case(case_id: str) -> str:
"""Read a support case by its ID."""
if case_id != "1842":
return "Case not found."
return "The customer cannot find the password reset email."
def search_manual(query: str) -> str:
"""Search the sample product manual."""
if "password" in query.lower():
return "Check spam, then request a new password reset email."
return "No matching instructions."
agent = create_agent(
model=model,
tools=[read_case, search_manual],
system_prompt=(
"Draft support replies. Read the case and look up relevant "
"manual instructions. Do not invent missing facts."
),
)
result = agent.invoke({
"messages": [{
"role": "user",
"content": "Draft a reply for case 1842.",
}]
})
draft = result["messages"][-1].content
The model can request read_case("1842"). LangChain runs that function and gives its result back to the model. The model can then request search_manual("password reset") before writing the reply. The exact calls depend on the model's decisions.
The instructions ask the model to read and search. They do not enforce that sequence. Put a required step in application code or a graph when it must always happen. Real functions must also check the caller's permissions.
3. Your workflow requires approval
Reuse agent from example 2. The new requirement is that your application must wait for a person's decision before sending the reply. The intended flow is:
Diagram of example 3. The branch labels are the decision your application supplies when resuming the job. Sending is simulated in this example.
LangGraph calls each step a node and each connection an edge. Here, each node is a Python function. The state is a dictionary carrying the case ID, draft and decision between those functions.
StateGraph is the class you use to assemble the workflow: specify its state fields, register the functions and connect them. Calling compile() checks the graph's structure and produces a workflow you can run. It does not ask the AI to choose the steps; your connections define them.
Define the data and the three steps. TypedDict describes the dictionary's expected fields for Python type checkers. total=False allows fields to be absent: initially we only have case_id; the steps add the other values.
from typing import TypedDict
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.graph import StateGraph, START, END
from langgraph.types import Command, interrupt
class ReplyState(TypedDict, total=False):
case_id: str
draft: str
approved: bool
sent: bool
def draft_reply(state: ReplyState):
# Use the agent from example 2 to write the reply.
result = agent.invoke({
"messages": [{
"role": "user",
"content": f"Draft a reply for case {state['case_id']}.",
}]
})
# Return only the changed field. LangGraph keeps case_id in the state.
return {"draft": result["messages"][-1].content}
def review_reply(state: ReplyState):
# Pause here and give the draft back to the calling application.
# On resume, interrupt() returns the decision supplied by the application.
decision = interrupt({"reply_to_review": state["draft"]})
return {"approved": decision is True}
def send_reply(state: ReplyState):
# Demo only: record a simulated send. No email or API call is made.
# A real implementation would send state["draft"] for state["case_id"].
return {"sent": True}
Each function receives the current state and returns the fields it changes. For example, draft_reply returns {"draft": "..."}; LangGraph adds that value to the state while keeping case_id.
Diagram of the state on the approved path. Gold marks the field added at each step; <reply text> stands for the model's draft. While review is paused, the state is still at step 2. Rejection adds approved: False and ends the job without adding sent.
Connect the steps. Use a named function to choose the route after review, so the approval condition is visible:
def route_after_review(state: ReplyState):
if state["approved"]:
return "send"
return "stop"
# Describe the workflow's data; this does not run any steps yet.
builder = StateGraph(ReplyState)
# Register each function under a name used by the connections below.
builder.add_node("draft", draft_reply)
builder.add_node("review", review_reply)
builder.add_node("send", send_reply)
# START and END are LangGraph markers for entering and leaving the workflow.
builder.add_edge(START, "draft")
builder.add_edge("draft", "review")
# After review, route_after_review returns "send" or "stop".
# Map those results to the send node or the end of the workflow.
builder.add_conditional_edges(
"review",
route_after_review,
{"send": "send", "stop": END},
)
builder.add_edge("send", END)
# A checkpointer saves progress so the review step can pause and resume.
# This demonstration stores that progress only in the current process's memory.
workflow = builder.compile(checkpointer=InMemorySaver())
Approval leads to send; rejection ends the job without calling send_reply. Only draft_reply uses the AI agent. The approval branch is ordinary Python code.
Start the job and display the draft. invoke() runs the workflow until it finishes or pauses. Here it runs draft_reply, then pauses inside review_reply:
# Give this review job an ID so LangGraph can find its saved progress later.
config = {"configurable": {"thread_id": "case-1842-review-1"}}
paused = workflow.invoke({"case_id": "1842"}, config=config)
# __interrupt__ contains the pending review request.
# .value is the dictionary passed to interrupt() in review_reply.
review_request = paused["__interrupt__"][0].value
# Display review_request["reply_to_review"] in your application.
Your application displays review_request["reply_to_review"] with Approve and Reject buttons. LangGraph does not provide that screen. The first invoke() returns control to your application while the job waits; it does not keep the button handler blocked.
Resume with the person's decision. In the Approve button handler, pass True back to the same job:
# Run in the Approve button handler, after checking the user's access:
finished = workflow.invoke(Command(resume=True), config=config)
# True becomes the return value of interrupt() in review_reply.
# That step sets approved=True, so the graph calls send_reply and then ends.
# finished["sent"] is now True, recording the simulated send.
# In the Reject button handler, use Command(resume=False) instead.
# Invoke only the handler corresponding to the user's decision.
Command(resume=True) supplies the approval decision; config identifies which paused job receives it. In the Reject handler, Command(resume=False) makes the review step set approved=False, and the graph ends without a send. Run only the handler for the person's actual decision.
The thread_id identifies this particular job. Use the same ID when resuming it and a new ID for a new review. Keep the compiled workflow and its in-memory store alive between these calls. Progress is lost when the process stops; use a database-backed checkpoint store if it must survive a restart.
As described in the interrupt documentation, LangGraph restarts the paused function when resuming it. In this example it re-enters review_reply, and the interrupt() call returns the supplied decision. The earlier draft step does not need to run again for this normal resume. Keep actions such as sending a message out of the code before interrupt(). A real send function must also prevent duplicate sends if it is retried.
The first example makes one model call. The second lets the model request functions. The third puts that agent inside an application-controlled sequence with a review step.
AI Disclosure
Researched and drafted with AI assistance, then reviewed by Søren Dalby. Sources are linked; errors may remain, and corrections are welcome.