DEV Community

Cover image for I Enabled Memory for My LangGraph Agents. Then I Realized I Had a Concurrency Problem.
Rozera_ai
Rozera_ai

Posted on

I Enabled Memory for My LangGraph Agents. Then I Realized I Had a Concurrency Problem.

Subtitle: Subgraph persistence, checkpoint namespaces, and the architectural decisions that can quietly break a multi-agent system.

There is something easy to overlook when building a multi-agent system with LangGraph.

You give an agent a checkpointer, run a few successful tests, and assume it can now remember previous work safely.

Everything looks fine until the same agent receives two requests at nearly the same time.

One execution is updating its conversation history. Another is trying to continue from that history. Both are working with persistent state, but neither was designed to own it exclusively.

At that point, the problem is no longer about how good your LLM is.

It's about how you've designed state ownership.

I think this is one of the more interesting parts of building production-grade LangGraph applications, so let's look at what actually happens.

The problem starts when agents become stateful

Consider a system that has a supervisor coordinating three specialist agents:

  • A billing agent that handles payment-related questions.
  • A research agent that investigates technical issues.
  • A coding agent that inspects and modifies files.

Each agent has its own graph, tools, and state schema.

The supervisor decides which specialist should handle a request and combines their results.

A simplified architecture looks like this:

                 Supervisor
                      |
          +-----------+-----------+
          |           |           |
       Billing     Research      Coding
        Agent       Agent        Agent
          |           |           |
       State       State        State
Enter fullscreen mode Exit fullscreen mode

So far, nothing unusual.

The interesting question is what happens to those internal states after an agent finishes its work.

Should the research agent remember its previous investigation?

Should the coding agent remember which files it modified?

Should the billing agent start with a clean context for every new request?

These sound like memory decisions, but in LangGraph they are also persistence and execution decisions.

The checkpointer argument on .compile() gives you control over this behavior.

There are three modes worth understanding.

1. Per-invocation persistence: isolation without losing durability

This is the default behavior.

subgraph = builder.compile()

Enter fullscreen mode Exit fullscreen mode

Or explicitly:

subgraph = builder.compile(
    checkpointer=None
)
Enter fullscreen mode Exit fullscreen mode

Each invocation starts with fresh internal subgraph state.

But there is an important distinction here: fresh state does not mean no checkpointing.

When the parent graph has a checkpointer, the subgraph inherits it for the duration of its invocation. This means it can still support interrupts, resumption, and durable execution within that invocation.

What it doesn't do is accumulate its internal state across separate calls.

For example:

Thread: customer-123

Invocation 1:
    Research a payment issue
    Save execution checkpoints
    Finish

Invocation 2:
    Investigate a login problem
    Start with fresh subgraph state
Enter fullscreen mode Exit fullscreen mode

The second invocation doesn't automatically inherit the first invocation's internal conversation history.

This is particularly useful for specialist agents that perform independent tasks.

There is another benefit that matters even more in larger systems: per-invocation persistence supports parallel calls because each invocation gets its own checkpoint namespace.

That makes it a sensible default for many multi-agent architectures.

2. Per-thread persistence: when an agent needs continuity

Now consider a different situation.

You have a coding agent working on a project over several interactions.

During the first interaction, it inspects the authentication module. During the next, it investigates a database issue. Later, it needs to understand how those changes relate to each other.

Starting from scratch every time isn't particularly useful.

This is where per-thread persistence comes in.

subgraph = builder.compile(
    checkpointer=True
)
Enter fullscreen mode Exit fullscreen mode

With this configuration, the subgraph retains its internal state across invocations associated with the same thread.

Conceptually:

Thread: project-456

First invocation:
    Inspect authentication.py
    Record relevant context
    Save checkpoint

Second invocation:
    Load previous subgraph state
    Remember authentication.py
    Inspect database.py

Third invocation:
    Continue from accumulated state
Enter fullscreen mode Exit fullscreen mode

This is useful, but it changes the execution model.

The subgraph is no longer an independent task that happens to run several times.

It is a persistent execution context.

And that's where things get interesting.

3. The concurrency problem nobody wants to debug

Imagine your supervisor has access to a persistent research agent as a tool.

The model receives a request involving two topics:

  • Investigate the authentication system.
  • Investigate the database connection.

It decides to call the research agent twice, in parallel.

A simplified example:

await asyncio.gather(
    research_agent.ainvoke(
        task_a,
        config=config
    ),
    research_agent.ainvoke(
        task_b,
        config=config
    )
)
Enter fullscreen mode Exit fullscreen mode

Both calls use the same thread configuration.

If the research agent uses per-thread persistence, both executions attempt to work with the same persistent checkpoint namespace.

That is the problem.

LangGraph explicitly warns that per-thread subgraphs do not support parallel tool calls to the same subgraph because both calls write to the same namespace.

This is not something you should try to fix by simply increasing the number of workers.

More workers don't solve shared-state ownership.

In fact, they can make the behavior harder to reproduce.

The important distinction is:

Parallel execution and parallel access to the same persistent state are two different things.

LangGraph supports parallel execution. That doesn't mean every persistent subgraph can safely receive concurrent calls against the same thread.

For independent tasks, per-invocation persistence is usually a better fit.

For persistent agents, you need to control how concurrent calls are scheduled.

4. A different approach: stateless subgraphs

Not every subgraph needs persistence.

Sometimes an agent simply receives input, performs a calculation, and returns a result.

Keeping checkpoints for every execution adds complexity without providing much value.

You can disable subgraph checkpointing:

subgraph = builder.compile(
    checkpointer=False
)
Enter fullscreen mode Exit fullscreen mode

This makes the subgraph behave more like a regular function call.

The trade-off is straightforward:

  • No checkpoint-based recovery.
  • No interrupt-based pause and resume.
  • No persistent subgraph state.

If the process crashes halfway through, the subgraph must run again from the beginning.

For short-lived, independent operations, that can be a perfectly reasonable trade-off.

I wouldn't enable persistence just because an application is described as an AI agent.

Persistence should solve an actual requirement.

5. Checkpoint namespaces: the subtle issue with multiple subgraphs

There is another detail worth paying attention to, especially when building larger graphs.

LangGraph uses checkpoint namespaces to identify state belonging to different graph executions and subgraphs.

When multiple subgraphs are invoked inside a single node, invocation-order-based namespaces can become a problem.

Consider this:

def run_agents(state):

    fruit_result = fruit_agent.invoke(
        state["fruit_task"]
    )

    vegetable_result = vegetable_agent.invoke(
        state["vegetable_task"]
    )

    return {
        "fruit_result": fruit_result,
        "vegetable_result": vegetable_result
    }
Enter fullscreen mode Exit fullscreen mode

Now imagine someone refactors the function:

def run_agents(state):

    vegetable_result = vegetable_agent.invoke(
        state["vegetable_task"]
    )

    fruit_result = fruit_agent.invoke(
        state["fruit_task"]
    )

    return {
        "fruit_result": fruit_result,
        "vegetable_result": vegetable_result
    }
Enter fullscreen mode Exit fullscreen mode

The business logic hasn't changed.

But the invocation order has.

With certain nested invocation patterns, relying on call-order-based namespaces can cause problems when restoring persisted state.

The recommended approach is to give different stateful subgraphs stable identities by adding them as separate named nodes in the parent graph.

For example:

builder = StateGraph(ParentState)

builder.add_node("fruit_expert", fruit_agent)
builder.add_node("vegetable_expert", vegetable_agent)
Enter fullscreen mode Exit fullscreen mode

Now the graph composition explicitly identifies each specialist.

This is easier to reason about, easier to inspect, and less dependent on the order in which Python happens to execute function calls.

One small architectural decision can save a lot of debugging later.

6. Checkpointers and Stores are not interchangeable

There is one more distinction I think deserves attention.

Developers sometimes use the word memory to describe everything an agent remembers.

But LangGraph has two different persistence concepts.

Checkpointers store graph state associated with a thread.

They are useful for:

  • Conversation continuity.
  • Resuming interrupted executions.
  • Recovering from failures.
  • Inspecting previous graph states.

Stores provide application-level persistence across threads.

They are useful for:

  • User preferences.
  • Long-term facts.
  • Shared knowledge.
  • Information that should survive beyond one conversation.

For example, imagine a customer who has two separate conversations with your application.

In the first conversation, they explain their preferred programming language.

In the second conversation, they ask for help with a new project.

A thread-scoped checkpointer alone isn't the right abstraction for sharing that preference across both conversations.

A Store is a better fit.

The distinction looks like this:

                 Application
                      |
          +-----------+-----------+
          |                       |
      Checkpointer              Store
          |                       |
      Thread State          Cross-thread Data
          |                       |
    Conversation A          User Preferences
    Conversation B          Shared Knowledge
Enter fullscreen mode Exit fullscreen mode

I find this distinction particularly important because persistent conversation history and long-term application memory solve different problems.

Trying to use one as a replacement for the other usually leads to unnecessary coupling.

7. Choosing a persistence strategy

Here's how I would approach the decision in a real application.

Scenario Recommended approach
Independent specialist tasks Per-invocation
Agent needs multi-turn memory Per-thread
Short-lived computation Stateless
Parallel independent calls Per-invocation
Shared user preferences Store
Long-running coding assistant Per-thread + concurrency control

There isn't one persistence mode that fits every agent.

A billing agent, a coding agent, and a document summarizer have different requirements.

Giving all of them the same memory configuration just because they belong to the same application isn't necessarily a good design decision.

8. What I would check before deploying

Before putting a multi-agent system into production, I'd test a few things explicitly.

1. Two simultaneous calls to the same persistent subgraph.

Make sure the application prevents unsupported concurrent access to the same per-thread namespace.

2. Restarting the application.

An in-memory checkpointer is useful during development, but its data disappears when the process restarts. Production applications need an appropriate persistent checkpointer.

3. Resuming interrupted subgraphs.

Test what happens when an execution pauses, resumes, and continues using previously saved state.

4. Multiple conversations from the same user.

Verify that thread-specific information doesn't accidentally become shared user-level memory.

5. Refactoring graph structure.

If your architecture contains nested stateful subgraphs, test that changing their composition doesn't unexpectedly affect checkpoint restoration.

These tests cover different failure modes. Passing one doesn't mean you've covered the others.

My take

I used to think of agent memory as a relatively simple decision: either the agent remembers previous interactions or it doesn't.

Looking more closely at LangGraph's persistence model makes that distinction feel incomplete.

Memory has a scope. State has an owner. Executions have a lifecycle.

And once multiple agents start working concurrently, those details become architectural concerns rather than implementation details.

The interesting part is that the default per-invocation behavior is often exactly what you want. Not every specialist needs to remember everything it has ever done.

Sometimes a clean execution context is more useful than a persistent conversation.

For me, the question isn't whether an agent should have memory.

It's this:

Who owns that memory, how long should it live, and what happens when two executions try to change it at the same time?

If you can't answer those three questions, adding a checkpointer is probably the easiest part of your architecture.


Further reading


Tags: langgraph python ai multiagent architecture

Top comments (0)