← Back

LangGraph: agents with memory and human approval

LangGraph in Python: state, nodes and edges, an agent that remembers the conversation, and pausing for a person's approval.

LangGraph: agents with memory and human approval

In 30 seconds

  • LangGraph builds an agent as a graph: steps (nodes) and paths between them (edges).
  • Each step reads the state and adds to it.
  • Its strengths: control of the path, memory, and pausing for a person.

Concepts

Concept Meaning
State Data passed between steps
Node A function that does one step
Edge Where to go next
Conditional edge The path depends on the result
Checkpointer Saves state, so the agent remembers and resumes

Install

pip install -U langgraph langchain langchain-ollama
ollama pull qwen2.5:3b

1. An agent that uses a tool and remembers

The path: the model thinks; if it asks for a tool we run it and return to the model, otherwise we finish.

from langchain.chat_models import init_chat_model
from langchain.tools import tool
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.graph import START, MessagesState, StateGraph
from langgraph.prebuilt import ToolNode, tools_condition

ORDERS = {"A100": "shipped", "B200": "preparing"}


@tool
def get_order_status(order_id: str) -> str:
    """Get the status of an order by its number, for example A100."""
    return ORDERS.get(order_id.upper(), "order not found")


model = init_chat_model("ollama:qwen2.5:3b", temperature=0).bind_tools([get_order_status])


def assistant(state: MessagesState):
    return {"messages": [model.invoke(state["messages"])]}


builder = StateGraph(MessagesState)
builder.add_node("assistant", assistant)
builder.add_node("tools", ToolNode([get_order_status]))
builder.add_edge(START, "assistant")
builder.add_conditional_edges("assistant", tools_condition)  # to "tools" or to the end
builder.add_edge("tools", "assistant")

graph = builder.compile(checkpointer=InMemorySaver())  # remembers each conversation

config = {"configurable": {"thread_id": "customer-1"}}
for text in ["Where is my order A100?", "Which order number did I just ask about?"]:
    result = graph.invoke({"messages": [{"role": "user", "content": text}]}, config)
    print(result["messages"][-1].content)

The second question has no order number, yet the agent knows it because the conversation is saved under the same thread_id.

InMemorySaver forgets everything when the program stops. In production use a checkpointer backed by a database.

2. Human in the loop

Sensitive actions, such as sending money or deleting data, should wait for a person:

from typing import TypedDict

from langgraph.checkpoint.memory import InMemorySaver
from langgraph.graph import END, START, StateGraph
from langgraph.types import Command, interrupt


class State(TypedDict):
    amount: int
    result: str


def review(state: State):
    # the graph stops here and waits for a person
    approved = interrupt(f"Approve a refund of {state['amount']}?")
    return {"result": "refund sent" if approved else "refund rejected"}


builder = StateGraph(State)
builder.add_node("review", review)
builder.add_edge(START, "review")
builder.add_edge("review", END)
graph = builder.compile(checkpointer=InMemorySaver())

config = {"configurable": {"thread_id": "refund-1"}}

paused = graph.invoke({"amount": 250}, config)
print("Waiting:", paused["__interrupt__"][0].value)

final = graph.invoke(Command(resume=True), config)
print("Result:", final["result"])

Output:

Waiting: Approve a refund of 250?
Result: refund sent

Careful: on resume, the paused node runs again from its start, so put nothing before interrupt that must not happen twice.

Common mistakes

  • A complex graph on day one. Start with two steps.
  • Forgetting thread_id when using a checkpointer.
  • Endless loops: limit the number of steps (recursion_limit).

Next guide

AI agents: what they are and how to build one

Code tested on 30 November 2026 with Python 3.10, LangChain 1.4 and LangGraph 1.2.