LangChain Tutorial (Python): persisting agent state for intermediate developers

By Cyprian AaronsUpdated 2026-04-21
langchainpersisting-agent-state-for-intermediate-developerspython

This tutorial shows you how to persist LangChain agent state in Python so a conversation can survive process restarts, retries, and handoffs. You need this when your agent is doing real work: support chats, case management, banking workflows, or any flow where losing memory means losing context.

What You'll Need

  • Python 3.10+
  • A virtual environment
  • langchain
  • langchain-openai
  • langgraph
  • python-dotenv
  • An OpenAI API key in OPENAI_API_KEY
  • Basic familiarity with LangChain chat models and tools

Install the packages:

pip install langchain langchain-openai langgraph python-dotenv

Step-by-Step

  1. Start by loading your API key and creating a chat model. For persistence, we’ll use LangGraph’s checkpointer, which stores agent state between runs.
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI

load_dotenv()

if not os.getenv("OPENAI_API_KEY"):
    raise ValueError("OPENAI_API_KEY is not set")

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
  1. Define a simple tool and wire it into the model. The point here is not the tool itself; it’s to create an agent that can accumulate state across turns.
from langchain_core.tools import tool

@tool
def lookup_policy_status(policy_id: str) -> str:
    """Return a mock policy status for a given policy ID."""
    return f"Policy {policy_id} is active and paid through 2026-12-31."

tools = [lookup_policy_status]
llm_with_tools = llm.bind_tools(tools)
  1. Build the agent graph with memory persistence using a SQLite checkpointer. The thread ID becomes your conversation/session key, which is what lets the same agent resume later.
from langgraph.checkpoint.sqlite import SqliteSaver
from langgraph.prebuilt import create_react_agent

checkpointer = SqliteSaver.from_conn_string("agent_state.db")
agent = create_react_agent(
    model=llm_with_tools,
    tools=tools,
    checkpointer=checkpointer,
)

config = {"configurable": {"thread_id": "customer-123"}}
  1. Run the agent once, then run it again with the same thread ID. The second call will see prior messages because LangGraph reloads state from the SQLite file.
result1 = agent.invoke(
    {"messages": [("user", "Check policy P-1001 status")]},
    config=config,
)

print("First response:")
print(result1["messages"][-1].content)

result2 = agent.invoke(
    {"messages": [("user", "What did you just tell me about it?")]},
    config=config,
)

print("\nSecond response:")
print(result2["messages"][-1].content)
  1. Inspect the persisted state directly if you want to confirm storage is working. This is useful when debugging production flows where state corruption or wrong session IDs are common failure points.
state = agent.get_state(config)
print("Stored messages:")
for message in state.values["messages"]:
    role = message.type
    content = getattr(message, "content", "")
    print(f"{role}: {content}")

Testing It

Run the script twice with the same thread_id and you should get continuity across turns. The second prompt should reference earlier context instead of behaving like a fresh session.

Then change thread_id to something else, like "customer-456", and confirm that the new session starts clean. If you delete agent_state.db, all persisted memory is gone, which is expected for this setup.

If you want stronger validation, print the stored messages after each turn and verify they grow over time. In a real app, you’d also assert that retries reuse the same thread ID and that different users never share one.

Next Steps

  • Move from SQLite to Postgres for multi-instance deployments and better operational control.
  • Add structured state beyond chat history, such as case IDs, customer tier, or workflow step.
  • Wrap thread ID generation in your app layer so every authenticated user gets stable session persistence.

Keep learning

By Cyprian Aarons, AI Consultant at Topiax.

Want the complete 8-step roadmap?

Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.

Get the Starter Kit

Related Guides