# Add Conversation Memory to a LangGraph Agent

Yes. An agent wires a SQLite checkpointer for the embedded case, keys each conversation by thread_id, tests it across restarts and opens a pull request.

[Run this on Rush](https://cloud.getrush.ai/run/add-conversation-memory/langgraph/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

First the agent finds out whether the graph forgets, and where. In a LangGraph project like langchain-ai/react-agent, `src/react_agent/graph.py` ends with `graph = builder.compile(name="ReAct Agent")` and passes no checkpointer, and `langgraph.json` hands that variable to the LangGraph server as `agent`. Under the server this agent already remembers: the README says follow-up requests in Studio are appended to the same thread, because the server keeps every thread itself. Imported into a script, a worker or a web handler it does not. Two `graph.ainvoke` calls in a row are two unrelated conversations, and the second model request holds only the system prompt and the new question. `State` in `state.py` keeps `messages` under the `add_messages` reducer, so the graph is built to accumulate a history; nothing stores it between calls.

The fix for the embedded case is a checkpointer, and the agent does not put it on the exported graph. It checks first what `langgraph dev` does with one, using the `langgraph-api` 0.15.0 in `uv.lock`: the graph fails to load with an error that names the custom checkpointer and explains that the platform handles persistence. So `graph` stays as it is for the server, and the agent adds a small module beside it with an async context manager that opens a saver and yields `builder.compile(name="ReAct Agent", checkpointer=saver)`. For the saver it adds `langgraph-checkpoint-sqlite` to `pyproject.toml` and uses `AsyncSqliteSaver.from_conn_string(path)`. The async class matters here: `call_model` and the `search` tool are both `async def`, callers use `ainvoke`, and the synchronous `SqliteSaver` raises `NotImplementedError` on that path with a message that names the async one.

A conversation is then whatever shares a `thread_id`. Each call passes `{"configurable": {"thread_id": ...}}`; a call without it fails with a `ValueError` that lists the keys the checkpointer needs. With the same id the model's second request carries the first exchange, and a different id starts clean. The agent writes down two things the saver does not do. It does not store `context`: the `Context` dataclass with the system prompt, the model name and `max_search_results` is passed on every call, so a caller can change model in the middle of a thread without knowing it. And it never trims: each turn appends the question, the tool call, the full search result and the answer, and with `max_search_results` defaulting to 10 the prompt grows on every turn of a long thread.

The tests live in `tests/unit_tests/` and need no key. They are marked `pytest.mark.anyio` like the integration test, replace `load_chat_model` with a fake that records the messages it was given, and reach the module through `importlib.import_module("react_agent.graph")`, because `import react_agent.graph as g` returns the compiled graph that the package's `__init__.py` re-exports under the same name. One test shows the exported graph forgetting, one shows a thread remembering and a second thread not, one writes to a database file under `tmp_path`, opens a second saver on the same file and finds the history there, and one deletes a thread with `adelete_thread` and reads it back empty. Then `uv sync --extra dev`, since `ruff` and `mypy` are in the `dev` extra, and the commands `.github/workflows/unit-tests.yml` runs on Python 3.11 and 3.12: `ruff check .`, `mypy --strict src/` and `pytest tests/unit_tests`.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Give this LangGraph agent conversation memory when it runs outside the LangGraph server. Read pyproject.toml, uv.lock, langgraph.json, the README, the Makefile, every file under src/react_agent, tests/conftest.py, both test directories and .github/workflows first. Show with a test that two graph.ainvoke calls do not share history today. Do not compile the exported graph with a checkpointer: run langgraph dev against a graph that has one, and report what it does. Keep graph as it is for the server, and add a module with an async context manager that opens AsyncSqliteSaver.from_conn_string(path) and yields builder.compile(name=\"ReAct Agent\", checkpointer=saver); add langgraph-checkpoint-sqlite to pyproject.toml and update uv.lock. Check whether the synchronous SqliteSaver works with ainvoke and say why you did or did not use it. Add unit tests under tests/unit_tests with a fake model that records the messages it receives, no API key: the same thread_id remembers, another thread_id does not, a missing thread_id fails with a clear error, history survives closing the saver and opening a new one on the same file under tmp_path, and adelete_thread removes a conversation. Document in the README how to call the persistent graph, that context is passed on every call and is not stored, that thread_id decides who can read a conversation, and that history is never trimmed. Run uv sync --extra dev, ruff check ., mypy --strict src/ and pytest tests/unit_tests, do not run tests/integration_tests, paste the output in the pull request and open it.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request with one new module under `src/react_agent/`, one new test file, one added dependency in `pyproject.toml` with the matching `uv.lock` change, and a README section. `graph.py` changes by at most an export: the compiled `graph` that `langgraph.json` points at is the same object as before, so a deployment on the LangGraph server behaves as it did. The README section shows the embedded call from start to finish: open the context manager with a database path, call `ainvoke` with a `thread_id` and a `Context`, call it again, and read the thread back with `aget_state`.

A reviewer checks where `thread_id` will come from in the application, because it is the only thing separating one user's conversation from another's. An id taken straight from a request lets any caller read a thread they can guess; an id the server derives from the signed-in user does not. They check where the database file lives and whether more than one process will write to it. And they read the lockfile diff for anything that moved besides the new package, since adding one checkpoint library can raise the shared `langgraph-checkpoint` version with it.

## Run it on every new agent service

Teams that start each new agent from the same template hit this the first time one is embedded in an API in place of the LangGraph server. Have the script that creates the repository send this page's body to `POST /o/{org}/sessions`, with the new repository in `resources` and a `request_id` that is a UUID v5 of the repository's full name, so a scaffold that is retried returns the run it already started. The Rush GitHub App has to reach the new repository first; that is set in the console under Settings → Connections.

## Why run it on Rush?

Memory bugs in an agent look like working code. The graph answers, the tests that call it once pass, and the forgetting only shows on the second message from the same user, or after the process restarts. Proving the fix means running two turns, closing the saver, opening the file again and reading the thread back, and doing it against the versions in the lockfile. The agent does that in its own sandbox with a fake model, so no provider is called and no key is needed, and the checks behind each design choice are in the transcript at `GET /o/{org}/sessions/{id}/messages` with the commands in their `args`.

It is also a job that is easy to hand off halfway through a different one. You are building the endpoint that calls the agent, you notice it forgets, and the fix is a side quest through a checkpoint library's sync and async classes. Sending this request from your terminal turns that into a pull request you review later; the session parks when its turn ends and a follow-up to `POST …/sessions/{id}/messages` wakes it where it left off if you want Postgres after all.

## Frequently asked questions

### Why does my LangGraph agent forget the previous message?

Because the graph was compiled without a checkpointer and is being called directly. Each `invoke` then starts from the input alone. Compile `builder` with a saver and pass the same `thread_id` in `configurable` on every call of a conversation.

### Do I need a checkpointer on LangGraph Platform or under langgraph dev?

No. The server stores every thread itself, and the `langgraph-api` version in this template's lockfile refuses to load a graph that was compiled with a custom checkpointer. That is why this change leaves the exported `graph` alone and adds a separate way to build a persistent one.

### Can I use Postgres instead of SQLite?

Yes. The compile call is the same and the saver comes from `langgraph-checkpoint-postgres`. This page's request asks for SQLite because it needs no server to test against; say Postgres in the prompt and the agent installs a server in its sandbox from system packages to run the same tests.

## More LangGraph jobs

- [Add Human-in-the-Loop Approval to a LangGraph Agent](https://getrush.ai/run/add-human-in-the-loop/langgraph/)
- [Add a Fallback Model to a LangGraph Agent](https://getrush.ai/run/add-model-fallbacks/langgraph/)
- [Add Rate Limiting to a LangGraph Agent](https://getrush.ai/run/add-rate-limiting/langgraph/)

## More jobs like this: Hand off a long task

- [Add Cookie Sessions to an aiohttp App](https://getrush.ai/run/add-cookie-sessions/aiohttp/)
- [Add Cookie Sessions to a React Router App](https://getrush.ai/run/add-cookie-sessions/react-router/)
- [Add a Copy Button to Sphinx Code Blocks](https://getrush.ai/run/add-copy-button/sphinx/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
