Long-Term Agent Memory with Postgres and Qdrant in 10xGraph

In shortGive a 10xGraph Python agent long-term memory across sessions with MemoryConfig, PgVectorStore in Postgres or local Qdrant, user memory and shared agent memory.

  • 12 min read
  • 9 sections
  • Updated
  • v1.0.0
  • Markdown

This example gives a Claude chat agent long-term memory that survives across conversations. A user tells the agent they are vegetarian and live in Lisbon. On a new thread with the same user_id, the agent still knows. The chat model is Anthropic claude-sonnet-5-5, and memories are embedded by OpenAI text-embedding-3-small.

What you will build

A Claude chat agent that remembers facts between conversations. In the first session a user says they are vegetarian and live in Lisbon. In a second session, on a new thread with the same user_id, the agent recalls both facts. A different user sees none of it.

qdrant_memory.py uses a local Qdrant store, so no cloud account is needed. pgvector_memory.py uses the same agent configuration with PgVectorStore, so checkpoints and memories live in one Postgres. A third script, multi_tenant_memory.py, shows tenant columns on both (see Multi-tenant data).

Key concepts

  • MemoryConfig: passed to ReactAgent(memory=...) or Agent(memory=...). Registers the memory tools and adds a short memory section to the system prompt.
  • ReadMode: POSTLOAD (default) gives the model a user_memory_tool to search and remember. PRELOAD searches on every turn and attaches results to the user message, with no write tool. NO_RETRIEVAL turns retrieval off.
  • UserMemoryConfig: private per-user memory, keyed by user_id in the run config.
  • AgentMemoryConfig: shared memory that the model can only search. It is off by default and needs agent_id or app_id.
  • system_user_id(name): the reserved owner id (__system__:<name>) for shared data such as agent memory.
  • create_local_qdrant_store(path, embedding): file-based Qdrant, no server.
  • OpenAIEmbedding: embeds memories with text-embedding-3-small.
  • PgVectorStore: memories in Postgres with pgvector, searched by SIMILARITY, TEXT or HYBRID.

Run it

Terminal
git clone https://github.com/10xGraph/10xGraph.git
cd 10xGraph/agentflow
Terminal
pip install "10xgraph[anthropic,openai,qdrant]"               # qdrant_memory.py
pip install "10xgraph[anthropic,openai,pg_checkpoint,pgvector]" # pgvector_memory.py
export ANTHROPIC_API_KEY=...   # chat model
export OPENAI_API_KEY=...      # embeddings

qdrant_memory.py needs no Docker services: it stores vectors in a temporary directory and deletes it at the end.

Terminal
python examples/long_term_memory/qdrant_memory.py

pgvector_memory.py and multi_tenant_memory.py need Postgres with the pgvector extension, and Redis. examples/persistence/docker-compose.yml starts both (its image, pgvector/pgvector:0.8.7-pg16, ships the extension). On your own Postgres, have an admin run CREATE EXTENSION vector; once.

Terminal
docker compose -f examples/persistence/docker-compose.yml up -d
export POSTGRES_DSN=postgresql://tenx:tenx@localhost:5432/tenx
export REDIS_URL=redis://localhost:6379/0
python examples/long_term_memory/pgvector_memory.py
python examples/long_term_memory/multi_tenant_memory.py   # no API key needed

How the code works

qdrant_memory.py

The store is a Qdrant database in a local folder. The embedding model turns each memory into a vector so it can be found by meaning.

Python
store = create_local_qdrant_store(
    path=QDRANT_PATH,
    embedding=OpenAIEmbedding(model="text-embedding-3-small"),
    collection="assistant_memory",
)
Python
QDRANT_PATH = tempfile.mkdtemp(prefix="10xgraph-memory-")

A throwaway directory keeps the demo repeatable. Point path at a stable folder, or use create_remote_qdrant_store, to keep memories between runs.

Python
memory = MemoryConfig(
    store=store,
    # POSTLOAD (default): the model gets user_memory_tool and decides when to search or
    #   remember. PRELOAD: relevant memories are searched on every turn and attached to the
    #   user message, with no write tool. NO_RETRIEVAL: no memory tools or injection.
    retrieval_mode=ReadMode.POSTLOAD,
    limit=5,
    score_threshold=0.0,
    # User memory is on by default and keyed by the user_id in the run config.
    user_memory=UserMemoryConfig(),
    # Agent memory is shared by every user and read-only for the model. It is off by
    # default and needs an agent_id (or app_id) to name its owner.
    agent_memory=AgentMemoryConfig(enabled=True, agent_id="support-bot"),
)

MemoryConfig bundles the store with the rules. ReadMode.POSTLOAD lets the model decide when to search or remember through user_memory_tool. User memory is on by default and keyed by the user_id in the run config. Agent memory is shared by every user and read-only for the model. It is off by default and needs an agent_id (or app_id) to name its owner.

Python
agent = ReactAgent(
    model="claude-sonnet-5-5",
    system_prompt=[
        {
            "role": "system",
            "content": (
                "You are a personal assistant. Save durable facts and preferences the "
                "user tells you, and search memory before answering questions about them."
            ),
        }
    ],
    memory=memory,  # forwarded to the Agent, which registers the memory tools
)

The agent only needs memory=memory. ReactAgent forwards it to its inner Agent, which registers user_memory_tool and agent_memory_tool. The system prompt tells the model to save durable facts and to search memory before answering.

Python
async def seed_agent_memory() -> None:
    """Write one shared fact. Agent memory is owned by system_user_id(agent_id)."""
    await store.asetup()
    await store.astore(
        {
            "user_id": system_user_id("support-bot"),
            "agent_id": "support-bot",
            "thread_id": "support-bot",
            "collection": "assistant_memory",
        },
        "Company policy: refunds are available within 30 days of purchase.",
    )

Agent memory is seeded by writing as system_user_id("support-bot"), which is the owner the agent memory tool searches.

Python
async def say(app, text: str, user_id: str, thread_id: str) -> None:
    config = {"user_id": user_id, "thread_id": thread_id}
    result = await app.ainvoke({"messages": [Message.text_message(text)]}, config)
    print(f"[{user_id}/{thread_id}] user: {text}")

Memory is scoped by the run config, not by the thread. Session 2 uses a new thread_id but the same user_id, so the chat history is empty and the only source of the facts is the store.

Python
async def main() -> None:
    app = agent.compile(checkpointer=InMemoryCheckpointer())
    await seed_agent_memory()

    # Session 1: the agent saves facts about alice.
    await say(app, "I'm vegetarian and I live in Lisbon. Please remember that.", "alice", "s1")
    await app.aclose()  # memory writes run in the background; aclose() flushes them

    # Session 2: new thread, same user_id. No chat history is shared, only the store.
    app = agent.compile(checkpointer=InMemoryCheckpointer())
    await say(app, "Suggest a dinner idea for tonight, close to home.", "alice", "s2")
    await say(app, "What is the refund policy?", "alice", "s2")  # agent memory

    # A different user_id gets a different slice of the store.
    await say(app, "What do you know about my diet?", "bob", "s3")

    await app.aclose()
    shutil.rmtree(QDRANT_PATH, ignore_errors=True)

Writes happen in the background, so the script calls await app.aclose() between sessions to flush them before session 2 searches. The last call uses bob, who gets a different slice of the store.

pgvector_memory.py

The agent and the memory scopes are the same. The store is PgVectorStore, in the Postgres that also holds the checkpoints.

Python
store = PgVectorStore(
    embedding=OpenAIEmbedding(model="text-embedding-3-small"),
    dsn=POSTGRES_DSN,
    collection="assistant_memory",
    retrieval_strategy=RetrievalStrategy.HYBRID,
    on_embedding_error="fallback",
)

HYBRID fuses vector similarity and full-text rank with Reciprocal Rank Fusion, so a search for an exact word still finds it. With on_embedding_error="fallback", an embedding outage saves memories as text and searches by text instead of failing; abackfill_embeddings() fills the vectors in later.

Python
agent_memory=AgentMemoryConfig(
    enabled=True,
    agent_id="support-bot",
    retrieval_strategy=RetrievalStrategy.SIMILARITY,
),

Search settings resolve in three levels: the store constructor, then MemoryConfig or a memory scope, then the method call. This scope overrides the store’s HYBRID default for agent memory, and the last lines of main() override it again with retrieval_strategy=RetrievalStrategy.TEXT on one asearch call.

Python
checkpointer = PgCheckpointer(postgres_dsn=POSTGRES_DSN, redis_url=REDIS_URL)
await checkpointer.asetup()
await store.asetup()  # creates the extension, table and indexes; safe to repeat

Both asetup() calls are safe to repeat. The rest of main() matches the Qdrant version: two sessions on separate threads, a flush with aclose() in between, and a second user who sees none of the first user’s memories.

multi_tenant_memory.py

Two organisations, acme and globex, each have a user with the same id, sam. The checkpointer and the store declare the same columns:

Python
TENANT_COLUMNS = ["org_id"]                            # isolates data
EXTRA_COLUMNS = {"public_id": "text", "plan": "text"}  # your own data, filterable

checkpointer = PgCheckpointer(
    postgres_dsn=POSTGRES_DSN, redis_url=REDIS_URL,
    tenant_columns=TENANT_COLUMNS, extra_columns=EXTRA_COLUMNS,
)
store = PgVectorStore(
    embedding=None,  # text-only store, so no API key is needed
    dsn=POSTGRES_DSN, retrieval_strategy=RetrievalStrategy.TEXT,
    tenant_columns=TENANT_COLUMNS, extra_columns=EXTRA_COLUMNS,
)

The column values come from config["user"] only. Behind the API your auth class fills it; in this core-only script the script builds it, standing in for your trusted auth:

Python
"user": {"org_id": org_id, "public_id": public_id, "plan": plan}

Each org’s search returns only its own memory, although both belong to sam. The last part lists threads with a filter on an extra column, which can only narrow the isolated result:

Python
await checkpointer.alist_threads(acme, filters={"plan": {"in": ["pro"]}})

The script deletes its rows at the end. The Multi-tenant data guide covers the auth class, isolation scopes and every filter operator.

Full code

qdrant_memory.py

Python
"""Long-term memory for a Claude agent on a local Qdrant store.

Session 1 tells the agent some facts about a user. Session 2 is a brand new thread for
the same user_id and recalls them. A second user sees none of it. The example also shows
the three retrieval modes and shared agent memory (see the README).

Run:  python examples/long_term_memory/qdrant_memory.py
"""

import asyncio
import shutil
import tempfile

from dotenv import load_dotenv

from tenxgraph.core.state import Message
from tenxgraph.prebuilt.agent import ReactAgent
from tenxgraph.storage.checkpointer import InMemoryCheckpointer
from tenxgraph.storage.store import (
    AgentMemoryConfig,
    MemoryConfig,
    OpenAIEmbedding,
    ReadMode,
    UserMemoryConfig,
    create_local_qdrant_store,
    system_user_id,
)


load_dotenv()

# A throwaway directory keeps the demo repeatable. Point `path` at a stable folder
# (or use create_remote_qdrant_store) to keep memories between runs.
QDRANT_PATH = tempfile.mkdtemp(prefix="10xgraph-memory-")

store = create_local_qdrant_store(
    path=QDRANT_PATH,
    embedding=OpenAIEmbedding(model="text-embedding-3-small"),
    collection="assistant_memory",
)

memory = MemoryConfig(
    store=store,
    # POSTLOAD (default): the model gets user_memory_tool and decides when to search or
    #   remember. PRELOAD: relevant memories are searched on every turn and attached to the
    #   user message, with no write tool. NO_RETRIEVAL: no memory tools or injection.
    retrieval_mode=ReadMode.POSTLOAD,
    limit=5,
    score_threshold=0.0,
    # User memory is on by default and keyed by the user_id in the run config.
    user_memory=UserMemoryConfig(),
    # Agent memory is shared by every user and read-only for the model. It is off by
    # default and needs an agent_id (or app_id) to name its owner.
    agent_memory=AgentMemoryConfig(enabled=True, agent_id="support-bot"),
)

agent = ReactAgent(
    model="claude-sonnet-5-5",
    system_prompt=[
        {
            "role": "system",
            "content": (
                "You are a personal assistant. Save durable facts and preferences the "
                "user tells you, and search memory before answering questions about them."
            ),
        }
    ],
    memory=memory,  # forwarded to the Agent, which registers the memory tools
)


async def seed_agent_memory() -> None:
    """Write one shared fact. Agent memory is owned by system_user_id(agent_id)."""
    await store.asetup()
    await store.astore(
        {
            "user_id": system_user_id("support-bot"),
            "agent_id": "support-bot",
            "thread_id": "support-bot",
            "collection": "assistant_memory",
        },
        "Company policy: refunds are available within 30 days of purchase.",
    )


async def say(app, text: str, user_id: str, thread_id: str) -> None:
    config = {"user_id": user_id, "thread_id": thread_id}
    result = await app.ainvoke({"messages": [Message.text_message(text)]}, config)
    print(f"[{user_id}/{thread_id}] user: {text}")
    print("assistant:", result["messages"][-1].text(), "\n")


async def main() -> None:
    app = agent.compile(checkpointer=InMemoryCheckpointer())
    await seed_agent_memory()

    # Session 1: the agent saves facts about alice.
    await say(app, "I'm vegetarian and I live in Lisbon. Please remember that.", "alice", "s1")
    await app.aclose()  # memory writes run in the background; aclose() flushes them

    # Session 2: new thread, same user_id. No chat history is shared, only the store.
    app = agent.compile(checkpointer=InMemoryCheckpointer())
    await say(app, "Suggest a dinner idea for tonight, close to home.", "alice", "s2")
    await say(app, "What is the refund policy?", "alice", "s2")  # agent memory

    # A different user_id gets a different slice of the store.
    await say(app, "What do you know about my diet?", "bob", "s3")

    await app.aclose()
    shutil.rmtree(QDRANT_PATH, ignore_errors=True)


if __name__ == "__main__":
    asyncio.run(main())

pgvector_memory.py

Python
"""Long-term memory for a Claude agent on Postgres with pgvector.

One Postgres holds both the conversation checkpoints (PgCheckpointer) and the memories
(PgVectorStore). Session 1 tells the agent some facts. Session 2 is a new thread for the
same user_id and recalls them. The store searches with HYBRID retrieval (vector similarity
and full-text, fused with RRF), and one memory scope overrides that default.

Run:  docker compose -f examples/persistence/docker-compose.yml up -d
      export POSTGRES_DSN=postgresql://tenx:tenx@localhost:5432/tenx
      export REDIS_URL=redis://localhost:6379/0
      python examples/long_term_memory/pgvector_memory.py
"""

import asyncio
import os

from dotenv import load_dotenv

from tenxgraph.core.state import Message
from tenxgraph.prebuilt.agent import ReactAgent
from tenxgraph.storage.checkpointer import PgCheckpointer
from tenxgraph.storage.store import (
    AgentMemoryConfig,
    MemoryConfig,
    OpenAIEmbedding,
    PgVectorStore,
    ReadMode,
    UserMemoryConfig,
    system_user_id,
)
from tenxgraph.storage.store.store_schema import RetrievalStrategy


load_dotenv()

POSTGRES_DSN = os.environ["POSTGRES_DSN"]
REDIS_URL = os.environ["REDIS_URL"]

# Requires the pgvector extension in the database (the pgvector/pgvector image has it).
store = PgVectorStore(
    embedding=OpenAIEmbedding(model="text-embedding-3-small"),
    dsn=POSTGRES_DSN,
    collection="assistant_memory",
    # Store-level default for every search that does not pick a strategy.
    # SIMILARITY: vectors only. TEXT: full-text only. HYBRID: both, fused with RRF.
    retrieval_strategy=RetrievalStrategy.HYBRID,
    # If the embedding provider fails, writes are saved as text and searches fall back
    # to full-text. Use "raise" to fail instead. abackfill_embeddings() fills gaps later.
    on_embedding_error="fallback",
)

memory = MemoryConfig(
    store=store,
    retrieval_mode=ReadMode.POSTLOAD,
    limit=5,
    # Settings resolve in three levels: store constructor -> MemoryConfig or scope ->
    # method call. This scope overrides the store's HYBRID default for agent memory.
    user_memory=UserMemoryConfig(),
    agent_memory=AgentMemoryConfig(
        enabled=True,
        agent_id="support-bot",
        retrieval_strategy=RetrievalStrategy.SIMILARITY,
    ),
)

agent = ReactAgent(
    model="claude-sonnet-5-5",
    system_prompt=[
        {
            "role": "system",
            "content": (
                "You are a personal assistant. Save durable facts and preferences the "
                "user tells you, and search memory before answering questions about them."
            ),
        }
    ],
    memory=memory,
)


async def say(app, text: str, user_id: str, thread_id: str) -> None:
    config = {"user_id": user_id, "thread_id": thread_id}
    result = await app.ainvoke({"messages": [Message.text_message(text)]}, config)
    print(f"[{user_id}/{thread_id}] user: {text}")
    print("assistant:", result["messages"][-1].text(), "\n")


async def main() -> None:
    checkpointer = PgCheckpointer(postgres_dsn=POSTGRES_DSN, redis_url=REDIS_URL)
    await checkpointer.asetup()
    await store.asetup()  # creates the extension, table and indexes; safe to repeat

    # Shared agent memory is owned by system_user_id(agent_id).
    await store.astore(
        {
            "user_id": system_user_id("support-bot"),
            "agent_id": "support-bot",
            "thread_id": "support-bot",
            "collection": "assistant_memory",
        },
        "Company policy: refunds are available within 30 days of purchase.",
    )

    app = agent.compile(checkpointer=checkpointer)
    await say(app, "I'm vegetarian and I live in Lisbon. Please remember that.", "alice", "s1")
    await app.aclose()  # memory writes run in the background; aclose() flushes them

    # Session 2: new thread, same user_id. Only the store carries the facts over.
    app = agent.compile(checkpointer=checkpointer)
    await say(app, "Suggest a dinner idea for tonight, close to home.", "alice", "s2")
    await say(app, "What is the refund policy?", "alice", "s2")

    # A different user_id sees none of alice's memories.
    await say(app, "What do you know about my diet?", "bob", "s3")

    # A search call can override the strategy again (third level).
    hits = await store.asearch(
        {"user_id": "alice", "collection": "assistant_memory"},
        "vegetarian",
        retrieval_strategy=RetrievalStrategy.TEXT,
    )
    print("alice TEXT search:", [h.content for h in hits])

    await app.aclose()
    await store.arelease()
    await checkpointer.arelease()


if __name__ == "__main__":
    asyncio.run(main())

multi_tenant_memory.py

Python
"""Multi-tenant memory and threads on one Postgres, with no LLM.

Both PgCheckpointer and PgVectorStore get the same developer-owned columns:

- tenant column ``org_id``: isolates data. Two orgs can reuse the same user_id and still
  never see each other's rows.
- extra columns ``public_id`` and ``plan``: plain data stored with each row. ``plan`` is
  filterable in ``alist_threads``.

Where the values come from (trusted-source rule): tenant and extra column values come only
from ``config["user"]``. ``user`` is a reserved config key that clients cannot set. In an
API deployment your auth class fills it (BaseAuth.authenticate() result or JWT claims). In
core-only use, as here, YOU build ``config["user"]`` from your own trusted auth (a verified
session or token). Never read these values from request config, metadata, state or model
output.

The store is text-only (``embedding=None``), so no embedding key is needed.

Run:  export POSTGRES_DSN=postgresql://tenx:tenx@localhost:5432/tenx
      export REDIS_URL=redis://localhost:6379/0
      python examples/long_term_memory/multi_tenant_memory.py
"""

import asyncio
import os

from tenxgraph.storage.checkpointer import PgCheckpointer
from tenxgraph.storage.store import PgVectorStore
from tenxgraph.storage.store.store_schema import RetrievalStrategy
from tenxgraph.utils import ThreadInfo


POSTGRES_DSN = os.environ["POSTGRES_DSN"]
REDIS_URL = os.environ["REDIS_URL"]

TENANT_COLUMNS = ["org_id"]
EXTRA_COLUMNS = {"public_id": "text", "plan": "text"}


def run_config(user_id: str, thread_id: str, org_id: str, public_id: str, plan: str) -> dict:
    """Build a run config. The `user` dict must come from your trusted auth, not a client."""
    return {
        "user_id": user_id,
        "thread_id": thread_id,
        "collection": "tenant_demo",
        "user": {"org_id": org_id, "public_id": public_id, "plan": plan},
    }


async def main() -> None:
    checkpointer = PgCheckpointer(
        postgres_dsn=POSTGRES_DSN,
        redis_url=REDIS_URL,
        tenant_columns=TENANT_COLUMNS,
        extra_columns=EXTRA_COLUMNS,
    )
    store = PgVectorStore(
        embedding=None,  # text-only store: TEXT search, no pgvector extension needed
        dsn=POSTGRES_DSN,
        retrieval_strategy=RetrievalStrategy.TEXT,
        tenant_columns=TENANT_COLUMNS,
        extra_columns=EXTRA_COLUMNS,
    )
    await checkpointer.asetup()
    await store.asetup()

    # Two orgs, the same user_id "sam".
    acme = run_config("sam", "t-acme", "acme", "pub-acme-1", "pro")
    globex = run_config("sam", "t-globex", "globex", "pub-globex-1", "free")

    for cfg, org in ((acme, "acme"), (globex, "globex")):
        await checkpointer.aput_thread(cfg, ThreadInfo(thread_id=cfg["thread_id"], user_id="sam"))
        await store.astore(cfg, f"Sam works at {org} and prefers email over phone.")

    # Same user_id, different org: each sees only its own memory.
    for cfg in (acme, globex):
        hits = await store.asearch(cfg, "email")
        print(cfg["user"]["org_id"], "->", [h.content for h in hits])

    # Filter threads by an extra column. Filters can only narrow the isolated listing.
    pro_threads = await checkpointer.alist_threads(acme, filters={"plan": {"in": ["pro"]}})
    print("acme pro threads:", [t.thread_id for t in pro_threads])
    none_for_globex = await checkpointer.alist_threads(globex, filters={"plan": {"in": ["pro"]}})
    print("globex pro threads:", [t.thread_id for t in none_for_globex])

    # Clean up the demo rows.
    for cfg in (acme, globex):
        await checkpointer.aclean_thread(cfg)
    await store.arelease()
    await checkpointer.arelease()


if __name__ == "__main__":
    asyncio.run(main())

Source on GitHub: examples/long_term_memory

Expected output

Illustrative output. Wording varies by run.

Text
[alice/s1] user: I'm vegetarian and I live in Lisbon. Please remember that.
assistant: Saved. I'll keep in mind that you're vegetarian and based in Lisbon.

[alice/s2] user: Suggest a dinner idea for tonight, close to home.
assistant: Since you're vegetarian and in Lisbon, try a bean and chestnut stew...

[alice/s2] user: What is the refund policy?
assistant: Refunds are available within 30 days of purchase.

[bob/s3] user: What do you know about my diet?
assistant: I don't have anything saved about your diet yet.

Using a different model

Change the model string, for example ReactAgent(model="gpt-5-mini", ...) with pip install "10xgraph[openai,qdrant]" (or pgvector), or model="gemini-2.5-flash" with the google-genai extra. To embed with Google instead, swap OpenAIEmbedding for GoogleEmbedding. The memory configuration does not change.

Next steps

Frequently asked questions

How does the agent know which user a memory belongs to?
Every store operation uses the user_id in the run config. Memories written for one user_id are never returned for another, and the store raises if user_id is empty.
What is the difference between user memory and agent memory?
User memory is private to a user_id and the model can both search and write it. Agent memory is shared by all users, owned by system_user_id(agent_id), and the model can only search it.
Do memory writes block the response?
No. Writes run as background tasks so streaming is not delayed. Call aclose() on the compiled graph to flush pending writes before the process exits.
Last updated for v1.0.0Edit this page on GitHubReport an issue