IBM Developer

Article

Build long-running AI agents with LangGraph memory, checkpointers, and stores

Use LangGraph checkpointers and stores to preserve workflow state, retain user knowledge, and maintain context across multi-step AI agent workflows

By Supal Chowdhury, Anand Bhushan

Most AI applications follow a simple request-response pattern. This approach works well for short tasks, but it becomes a challenge when an AI agent must support a user or project over days, weeks, or months. Long-running AI agents need to remember previous interactions, maintain workflow state, and continue work without requiring users to repeat the same information.

LangGraph provides a memory architecture for building these stateful AI agents. It separates short-term workflow state from long-term knowledge using two memory components: checkpointers, which track the state of an active workflow, and stores, which retain information across conversations and execution threads.

In this article, you'll learn how LangGraph memory works, when to use checkpointers and stores, and how to implement both using practical code examples for long-running AI agent workflows.

Why long-running autonomous AI agents matter

Long-running autonomous AI agents can support users and projects over extended periods of time. Instead of starting from scratch after every interaction, these AI agents retain relevant context, track workflow progress, and continue tasks across multiple sessions.

For example, an AI agent supporting an application migration project can create a migration plan, review progress, identify issues, and continue the workflow over days or weeks without requiring users to repeat project details.

This capability enables AI agents to manage multi-step workflows, coordinate tasks, and act as long-term digital collaborators. However, building reliable long-running AI agents requires effective memory management to maintain context, retrieve relevant information, recover from interruptions, and support consistent decision-making over time.

This is where a robust memory architecture becomes essential.

Context and memory are different

For long-running AI agents, context and memory serve different purposes. Context is the information available to the AI model for a single request, such as user messages, instructions, and tool outputs. Memory is the information that an application stores and reuses across multiple interactions.

A larger context window does not replace a memory architecture. Continuously adding conversation history to prompts increases cost, slows responses, and can introduce irrelevant or outdated information.

A more effective approach is to store important information and retrieve only the information needed for the current task.

Memory lifecycle

Persist → Select → Retrieve → Inject → Use → Update or Forget

For long-running AI agents, the goal is not to remember everything. The goal is to store important information and retrieve the right information at the right time.

Challenges of long-running AI agents

The biggest challenge for a long-running AI agent is maintaining context and workflow state over time. Users may return days later, workflows may be interrupted, and requirements may change. Without memory, the AI agent cannot continue work effectively and may ask users to repeat information.

A well-designed memory architecture helps the AI agent:

  • Remember important information.
  • Resume interrupted workflows.
  • Maintain context across sessions.
  • Avoid repeating completed work.
  • Make consistent decisions.

Most memory architectures include three types of memory:

  • Working memory: Information needed for the current task.
  • Episodic memory: Records of previous interactions and events.
  • Long-term semantic memory: Facts, knowledge, and user preferences retained over time.

For long-running AI agents, memory is a core architectural component. It helps the AI agent understand previous activities, track the current workflow state, and determine the next action. Without a robust memory architecture, AI agents either forget important information or accumulate too much information, reducing reliability and accuracy over time.

LangGraph memory architecture

Long-running AI agents need more than a large context window. They need a memory architecture that can maintain workflow state, recover from interruptions, and retain important information across conversations. LangGraph addresses this requirement by separating short-term workflow state from long-term knowledge.

The LangGraph memory architecture consists of two complementary memory components:

  • Checkpointers (short-term memory): Stores the state of a single conversation or workflow thread. Each checkpoint is associated with a specific thread_id and automatically saves the graph state after each step. Checkpointers help AI agents:

    • Maintain workflow context.
    • Resume work after failures or interruptions.
    • Support human review and debugging.
    • Restore previous workflow states.

      In simple terms, a checkpointer helps an AI agent remember where it is in a workflow.

  • Stores (long-term memory): Retains information across conversations and workflow threads. Data is organized using namespaces and keys, making it accessible from any workflow. Stores are commonly used to save:

    • User preferences
    • Business rules
    • Project information
    • Organizational knowledge

      Stores can also support semantic search when configured with embeddings. In simple terms, a store helps an AI agent remember what it has learned over time.

Using both memory layers together

Most production AI agents use both memory components:

  • Checkpointers manage the current workflow state.
  • Stores manage long-term knowledge.

For example, if an AI agent is interrupted during a workflow, a checkpointer allows the agent to resume from the last saved state. If a user preference must be available in future conversations, a store retains that information for long-term use.

Together, these memory components enable AI agents to maintain workflow continuity, recover from interruptions, remember information across sessions, and make decisions using both current context and stored knowledge.

The following table highlights the differences.

Feature Checkpointer Store
Scope Single thread Multiple threads
Purpose Resume the same workflow Retain long-term information
Best use cases Recovery, workflow continuity, debugging User preferences, historical data, shared knowledge
Storage style Workflow state snapshots Persistent documents and records
Typical identifier thread_id Namespace and key
Data model Graph state snapshots Application-defined records
In-memory development backend InMemorySaver InMemoryStore
Production backend PostgresSaver PostgresStore

Use a checkpointer when information belongs to a specific workflow or conversation thread and must be restored when that thread resumes. Use a store when information must be shared across conversations, projects, or execution threads.

LangGraph memory architecture diagram

The following diagram illustrates how a LangGraph agent uses checkpointers and stores together. Checkpointers manage workflow state for a specific thread, while stores retain long-term information that can be shared across conversations and workflows. This separation enables reliable, stateful AI agents for long-running workflows.

LangGraph agent using a checkpointer for short-term workflow state and a store for long-term memory

Example: Using a checkpointer to resume a mobile app development workflow

This example shows how a LangGraph checkpointer preserves the short-term state of a mobile application development workflow. The workflow state includes the application name, platform, generated screens, messages, and current development task. LangGraph saves the workflow state after each invocation so the Mobile App Development Agent can continue the project without asking the developer to repeat information.

Consider a developer who first asks:

Create a login screen for my Flutter app with email and password.

Later, the developer asks:

Now add a forgot password screen.

The second request depends on the existing project context. The Mobile App Development Agent must remember that the developer is building a Flutter application, the application name is BankingApp, and the Login Screen already exists. A checkpointer preserves this information within a project thread.

Mobile App Development Agent using a LangGraph checkpointer to resume a Flutter project workflow

Define the workflow state

The following code defines the state of the mobile application development workflow. The state stores messages, the application name, the selected platform, generated screens, and the current development task. LangGraph uses the state definition to determine which information the checkpointer must save and restore.

from typing import TypedDict, Annotated
from operator import add
from langgraph.graph import StateGraph, START, END
from langchain_core.messages import HumanMessage, AIMessage

class MobileAppState(TypedDict):
    messages: Annotated[list, add]
    app_name: str
    platform: str
    screens: Annotated[list, add]
    current_task: str

The Annotated[list, add] definition tells LangGraph to add new messages and screens to the existing lists. This behavior preserves previously generated messages and screens instead of replacing them during each invocation.

Create the mobile app development agent

The following code defines the Mobile App Development Agent node. The Mobile App Development Agent reads the latest developer message and accesses the existing project information from the workflow state. The Mobile App Development Agent then updates the screen list and records the latest development task.

def mobile_app_agent(state: MobileAppState):
    last_message = state["messages"][-1].content

    # In production, an LLM (or other application logic)
    # would interpret the request and generate the change.

    response = f"""
Mobile App Development Agent
App: {state.get("app_name", "Unknown")}
Platform: {state.get("platform", "Unknown")}
Previous screens: {state.get("screens", [])}
Developer request:
{last_message}
I will implement this change while preserving
the existing application context.
"""

    updated_screens = []

    if "login" in last_message.lower():
        updated_screens.append("Login Screen")

    if "forgot password" in last_message.lower():
        updated_screens.append("Forgot Password Screen")

    return {
        "messages": [AIMessage(content=response)],
        "screens": updated_screens,
        "current_task": last_message,
    }

This example uses simple text matching to determine which screen to add. A production application can use a large language model (LLM) or other application logic to interpret developer requests and generate application changes. The node returns an updated message, screen list, and current task for LangGraph to save.

Build and compile the LangGraph workflow

The following code creates a StateGraph based on MobileAppState. The code adds the Mobile App Development Agent node and defines the execution path from START to END. The code then compiles the graph with an InMemorySaver checkpointer.

graph = StateGraph(MobileAppState)
graph.add_node("mobile_app_agent", mobile_app_agent)
graph.add_edge(START, "mobile_app_agent")
graph.add_edge("mobile_app_agent", END)
from langgraph.checkpoint.memory import InMemorySaver
checkpointer = InMemorySaver()
app = graph.compile(checkpointer=checkpointer)

The InMemorySaver stores workflow checkpoints in application memory. This checkpointer is useful for development and testing, but the checkpoints are lost when the application stops. A production application should use durable checkpoint storage.

Create and resume a project thread

The following code assigns the project a unique thread_id. The first invocation creates a Login Screen and saves the resulting workflow state. The second invocation uses the same thread_id, allowing LangGraph to restore the saved state and add a Forgot Password Screen to the existing project.

config = {
    "configurable": {
        "thread_id": "mobile-app-project-001"
    }
}

result = app.invoke(
    {
        "messages": [HumanMessage(content="Create a login screen with email and password.")],
        "app_name": "BankingApp",
        "platform": "Flutter",
        "screens": [],
        "current_task": "",
    },
    config,
)

print(result["messages"][-1].content)

# Later, same thread_id:

result = app.invoke(
    {"messages": [HumanMessage(content="Now add a forgot password screen.")]},
    config,
)

print(result["messages"][-1].content)

The first invocation provides the complete initial state for the project. The checkpointer associates the saved state with mobile-app-project-001. The second invocation provides only the new message because LangGraph restores the remaining project information from the saved checkpoint.

Review the saved checkpoint state

Because both invocations use the same thread_id, LangGraph treats both requests as part of the same project. The second invocation resumes from the saved workflow state instead of treating the developer request as an unrelated task. After the first invocation, the checkpoint conceptually contains the following information:

thread_id     = mobile-app-project-001
app_name      = BankingApp
platform      = Flutter
screens       = [Login Screen]
current_task  = Create a login screen...
messages      = [...]

The checkpoint preserves the active project context, including the application name, platform, generated screens, current task, and message history. LangGraph restores this state when another invocation uses the same thread_id. The Mobile App Development Agent can then process the next request with the correct project context.

Resume the mobile app development workflow

When the developer submits the following request:

"Now add a forgot password screen."

LangGraph uses mobile-app-project-001 to restore the saved workflow state. The Mobile App Development Agent retrieves the BankingApp project, the Flutter platform, and the existing Login Screen. The Mobile App Development Agent then processes the new request and adds the Forgot Password Screen.

The workflow follows this sequence:

Saved checkpoint
├─ App: BankingApp
├─ Platform: Flutter
├─ Existing screen: Login Screen
└─ Current project state

Developer request
└─ "Add a forgot password screen"

Mobile App Development Agent
└─ Generates: Forgot Password Screen

The checkpointer does more than remember a conversation. The checkpointer preserves the active development state of the mobile application project across multiple agent invocations. This capability allows the Mobile App Development Agent to continue the project without requiring the developer to repeat existing project details.

Configure durable checkpoint storage

The InMemorySaver checkpointer stores data only while the application is running. The following code shows how to replace InMemorySaver with SQLite or PostgreSQL storage. Database-backed checkpointers preserve the workflow state across application restarts and system failures.

from langgraph.checkpoint.sqlite import SqliteSaver

with SqliteSaver.from_conn_string("mobile_app_checkpoints.db") as checkpointer:
    app = graph.compile(checkpointer=checkpointer)
    # invoke app here, inside the with-block

# Postgres, for a real production deployment:

from langgraph.checkpoint.postgres import PostgresSaver

with PostgresSaver.from_conn_string(DB_URI) as checkpointer:
    checkpointer.setup()  # one-time: creates required tables
    app = graph.compile(checkpointer=checkpointer)

SqliteSaver provides persistent local storage and can support development or small deployments. PostgresSaver provides durable checkpoint storage for production deployments. The setup() method creates the required PostgreSQL tables during the initial configuration.

With durable storage, the Mobile App Development Agent can recover the latest project state after an application restart. The developer can continue the same project by submitting a request with the existing thread_id.

How the checkpointer supports the workflow

The checkpointer saves the project state after each graph execution step. When the developer submits another request with the same thread_id, LangGraph restores the saved state before executing the Mobile App Development Agent. This process enables the agent to maintain project continuity, recover from interruptions, and resume the mobile application development workflow from the latest saved state.

Example: Using a store for long-term memory

A LangGraph store retains information that must remain available across projects, conversations, and workflow threads. For example, a developer might prefer Flutter, Riverpod, Material 3, and Clean Architecture for all mobile application projects. The Mobile App Development Agent can associate these preferences with the developer's user_id and reuse the preferences in future projects, even when each project has a different thread_id.

In this example, the developer first asks the Mobile App Development Agent to remember a set of development preferences:

I prefer Flutter with Riverpod, Material 3, and clean architecture.

Later, the same developer starts a new conversation and asks the Mobile App Development Agent to create an authentication screen. Because the long-term memory belongs to the developer rather than a specific workflow thread, the Mobile App Development Agent can retrieve and apply the saved preferences in the new project.

Developer preferences saved in Conversation A and retrieved from the long-term store in Conversation B

Import the required components

The following code imports the components required to create the LangGraph workflow. The example uses InMemorySaver to manage short-term conversation state and InMemoryStore to manage long-term developer preferences. The code also imports the classes required to define the workflow state and process messages.

from typing import TypedDict, Annotated
from operator import add
from langgraph.graph import StateGraph, START, END
from langgraph.store.memory import InMemoryStore
from langgraph.checkpoint.memory import InMemorySaver
from langchain_core.messages import HumanMessage, AIMessage

Define the short-term conversation state

The following code defines the state used during each conversation. The state contains the messages exchanged between the developer and the Mobile App Development Agent. The Annotated[list, add] definition adds new messages to the existing message history instead of replacing the existing messages.

class MobileAppState(TypedDict):
    messages: Annotated[list, add]

The checkpointer manages this conversation state within a specific thread_id. The long-term store remains separate from the conversation state and retains developer preferences across different threads.

Create the long-term memory node

The following code defines a node that stores and retrieves developer preferences. The node creates a namespace based on the developer's user_id, which keeps the saved information separate for each developer. When a message begins with Remember:, the node saves the preference; otherwise, the node retrieves the saved preference and uses the preference in its response.

def memory_node(state, config, *, store):
    user_id = config["configurable"]["user_id"]

    # Long-term memory namespace
    namespace = (
        "developers",
        user_id,
        "mobile_app_preferences"
    )

    text = state["messages"][-1].content.strip()

    # -----------------------------------------------------
    # Developer explicitly asks the agent to remember
    # -----------------------------------------------------
    if text.lower().startswith("remember:"):
        preference = text.split(":", 1)[1].strip()

        store.put(
            namespace,
            "coding_preferences",
            {
                "text": preference
            }
        )

        return {
            "messages": [
                AIMessage(
                    content=f"I'll remember that: {preference}"
                )
            ]
        }

    # -----------------------------------------------------
    # Retrieve long-term developer preference
    # -----------------------------------------------------
    item = store.get(
        namespace,
        "coding_preferences"
    )

    preference = (
        item.value["text"]
        if item
        else "No developer preferences stored."
    )

    # -----------------------------------------------------
    # Use stored preference when generating response
    # -----------------------------------------------------
    response = f"""
Mobile App Development Agent

Developer preference:
{preference}

Developer request:
{text}

I will generate the mobile-app solution
according to the stored development preference.
"""

    return {
        "messages": [
            AIMessage(content=response)
        ]
    }

The namespace contains three parts: developers, the developer's user_id, and mobile_app_preferences. The coding_preferences key identifies the stored record within that namespace. This structure allows the application to organize long-term memory by developer and information type.

Build the LangGraph workflow

The following code creates the LangGraph workflow and adds the long-term memory node. The workflow starts at START, sends the developer request to memory_node, and then proceeds to END. During execution, memory_node can read from or write to the long-term store.

graph = StateGraph(MobileAppState)

graph.add_node(
    "memory_node",
    memory_node
)

graph.add_edge(
    START,
    "memory_node"
)

graph.add_edge(
    "memory_node",
    END
)

This workflow contains one node because the example focuses only on long-term memory behavior. A production workflow can include additional nodes for planning, code generation, testing, human review, and deployment.

Configure short-term and long-term memory

The following code creates both memory components used by the workflow. The InMemorySaver checkpointer manages state within each conversation thread, while the InMemoryStore retains developer preferences across threads. Compiling the graph with both components gives each memory component a separate responsibility.

checkpointer = InMemorySaver()

store = InMemoryStore()

app = graph.compile(
    checkpointer=checkpointer,
    store=store
)

The two components serve different purposes:

  • The checkpointer saves conversation state associated with a thread_id.
  • The store saves developer preferences associated with a user_id.

Because this example uses in-memory components, the saved information remains available only while the application is running. A production application should use durable storage so checkpoints and developer preferences remain available after application restarts.

Store the developer preferences in conversation A

The following code starts the first conversation for developer-001. The developer asks the Mobile App Development Agent to remember specific framework, state management, design, and architecture preferences. The memory node saves these preferences in the developer's long-term memory namespace.

config_a = {
    "configurable": {
        "thread_id": "mobile-project-thread-a",
        "user_id": "developer-001"
    }
}

app.invoke(
    {
        "messages": [
            HumanMessage(
                content=(
                    "Remember: "
                    "I prefer Flutter with Riverpod, "
                    "Material 3 and clean architecture."
                )
            )
        ]
    },
    config_a
)

The thread_id identifies Conversation A, while the user_id identifies the developer. The checkpointer can use mobile-project-thread-a to manage the conversation state. The store uses developer-001 as part of the namespace for the saved development preferences.

Conceptually, the store contains the following information after this invocation:

Namespace:
developers/developer-001/mobile_app_preferences

Key:
coding_preferences

Value:
I prefer Flutter with Riverpod, Material 3, and clean architecture.

Retrieve the developer preferences in conversation B

The following code starts another conversation with a different thread_id but the same user_id. The developer asks the Mobile App Development Agent to create an authentication screen for a new mobile application. The memory node retrieves the saved preferences for developer-001 and uses the preferences when preparing the response.

#    Different thread, SAME developer
config_b = {
    "configurable": {
        "thread_id": "mobile-project-thread-b",
        "user_id": "developer-001"
    }
}

result = app.invoke(
    {
        "messages": [
            HumanMessage(
                content=(
                    "Create the authentication screen "
                    "for my new mobile app."
                )
            )
        ]
    },
    config_b
)

print(result["messages"][-1].content)

Conversation B uses mobile-project-thread-b, so the checkpointer treats Conversation B as a separate conversation. However, both conversations use developer-001, so the long-term memory node can retrieve the same developer preferences. The developer does not need to repeat the preferred framework, state management library, design system, or architecture.

Understand long-term memory across conversations

The following flow shows how the Mobile App Development Agent stores preferences in Conversation A and retrieves them in Conversation B:

Conversation A
├─ Thread ID: mobile-project-thread-a
├─ User ID: developer-001
└─ Request:
   "Remember: I prefer Flutter with Riverpod,
   Material 3 and clean architecture."

Long-term store
└─ Saves the preferences for developer-001

Conversation B
├─ Thread ID: mobile-project-thread-b
├─ User ID: developer-001
└─ Request:
   "Create the authentication screen
   for my new mobile app."

Mobile App Development Agent
├─ Retrieves the preferences for developer-001
└─ Applies the preferences to the new project

The thread_id changes because the developer starts a different conversation or project. The user_id remains the same because both conversations belong to the same developer. This design allows the store to retain developer-level knowledge across independent workflows.

Checkpointer and store responsibilities

The checkpointer and store manage different types of memory:

  • A checkpointer maintains messages and workflow state within a specific conversation or execution thread.
  • A store maintains developer preferences and other durable knowledge across different conversations and threads.

In this example, the checkpointer manages the state of Conversation A and Conversation B separately. The store connects the two conversations through the shared user_id and makes the developer's preferences available to both workflows.

For production deployments, developers should replace InMemoryStore with a durable persistent store. Durable storage allows developer preferences to remain available after application restarts, deployments, and system failures.

How the store supports long-term memory

A LangGraph store allows the Mobile App Development Agent to retain knowledge that does not belong to one workflow. By organizing records with namespaces and keys, the application can store information for individual developers, projects, teams, or organizations. The Mobile App Development Agent can retrieve the relevant information during future conversations and provide consistent results without asking users to repeat saved preferences.

LangGraph store retaining developer preferences across multiple conversation threads using namespaces and keys

Common memory mistakes in long-running AI agents

A common mistake when building long-running AI agents is treating memory as a place to store everything. Effective memory management requires storing the right information, retrieving relevant information when needed, and removing information that is no longer useful.

  • Storing too much information: Saving every message and event can make memory noisy, slow, and expensive to manage.

    Example: Do not store every greeting, acknowledgment, or repeated question. Store only information that can help with future decisions or tasks.

  • Storing too little information: Saving only conversation history can cause the AI agent to lose important context and user preferences.

    Example: Store a user's preference for short, simple answers so the AI agent can apply that preference in future interactions.

  • Mixing short-term and long-term memory: Short-term workflow state and long-term knowledge serve different purposes and should be managed separately.

    Example:

    • Current workflow progress belongs in short-term memory.
    • User preferences belong in long-term memory.
  • Not removing outdated information: Memory should be updated as information changes. Outdated, duplicate, and irrelevant records should be removed or replaced.

    Example: If a user changes a preferred framework from React to Vue, replace the old preference with the new preference.

  • Not planning for recovery: Long-running workflows can fail because of application crashes, network issues, or system interruptions. AI agents should save workflow progress so work can resume from the latest state.

    Example: If a 5-step workflow stops during step 4, the AI agent should resume from the last saved checkpoint instead of restarting from step 1.

LangGraph memory design best practices

A well-designed LangGraph memory architecture helps AI agents remain reliable, efficient, and scalable over long-running workflows. Follow these best practices when designing memory for production AI agents:

  • Use checkpointers to save the state of active workflows.
  • Use stores to retain information across conversations and workflow threads.
  • Store important facts and decisions instead of complete conversation histories.
  • Summarize older conversations before saving information to long-term memory.
  • Isolate memory by user, project, team, or organization.
  • Retrieve only the information required for the current task.
  • Update stored information when requirements, preferences, or facts change.

Example: Store important facts instead of full conversations

Instead of saving an entire conversation, store the information that will be useful in future interactions.

Store this fact: Customer prefers email communication.

The AI agent can retrieve and use this preference in future conversations without storing the complete chat history. This approach reduces storage requirements, improves retrieval accuracy, and keeps memory focused on relevant information.

When to use the LangGraph memory architecture

The LangGraph memory architecture is useful when an AI agent must remember information and continue work over an extended period of time. It helps AI agents maintain workflow continuity, retain important knowledge, and resume tasks across multiple conversations and sessions.

Use the LangGraph memory architecture when an AI agent needs to:

  • Remember user preferences, such as preferred languages, communication styles, or application settings.
  • Track customer issues and continue working on them across multiple interactions.
  • Store research findings and use them in future conversations.
  • Resume interrupted workflows from the last completed step.
  • Maintain context during multi-step workflows.
  • Support long-running projects that span days, weeks, or months.
  • Retain knowledge across different conversations and workflow threads.

Example

Consider a customer support AI agent. A customer reports a billing issue on Monday. The AI agent stores the issue details and current status. When the customer returns on Tuesday, the AI agent can retrieve the previous information and continue resolving the issue without asking the customer to repeat the same details.

In these scenarios, checkpointers help the AI agent track the current workflow state, while stores help the AI agent retain important information across conversations and sessions. Together, they enable reliable and stateful AI agent behavior for long-running workflows.

Summary

This article demonstrated how LangGraph enables AI agents to remember information and continue work across conversations, sessions, and long-running workflows.

  • Checkpointers provide short-term memory by storing the current conversation and workflow state.
  • Stores provide long-term memory by retaining user preferences, facts, and other important information across conversations and execution threads.
  • Retrieval helps AI agents access only the information needed for the current task instead of loading all historical data into a single context window.

Many AI applications are stateless and lose context when a conversation ends. LangGraph addresses this limitation by enabling AI agents to save workflow state, restore previous state after interruptions, and reuse stored knowledge in future interactions.

These capabilities are especially valuable for long-running applications where AI agents must handle interruptions, remember user preferences, maintain workflow context, and continue tasks over time without requiring users to restart the process or repeat previously provided information.