AI agents · OpenClaw · self-hosting · automation

Quick Answer

How to Add Long-Term Memory to an AI Agent (2026 Guide)

Published:

The short answer

Long-term memory is a store you write to after a task and read from before the next one, scoped to a user or project. The fastest working setup, as of December 5, 2026:

  1. Keep short-term and long-term memory separate. Thread history for the current conversation; a namespaced store for what should survive it.
  2. Pick a store that fits your stack:
    • LangGraph stores — if you already use LangGraph.
    • Claude memory tool (memory_20250818) — if you build on Claude and want memories as files on storage you control.
    • Mem0 — if you want extraction, deduplication and retrieval handled for you (free hosted tier or open source).
  3. Write deliberately, retrieve narrowly, delete on request.

Step 1: Choose what to remember

LangChain’s memory guide borrows three categories from psychology, and they map well to agents:

TypeWhat it storesExample
SemanticFacts”User prefers TypeScript and deploys to Fly.io”
EpisodicPast experiences”Last migration failed on a missing index; fix was X”
ProceduralRules and instructions”Always run tests before opening a PR”

Most products need semantic memory first. Procedural memory is often just an editable instructions file (AGENTS.md or a system-prompt section the agent may update). Decide up front what the agent must never store — secrets, payment data, health data — and filter it at write time.

Step 2: Scope memories correctly

The most common memory bug is one user’s memory surfacing for another. Put every memory under a namespace: (org_id, user_id) for personal facts, (org_id, project_id) for shared project knowledge. LangGraph’s stores take a namespace tuple on every put and search; with Claude’s memory tool, your handler maps the /memories path onto a per-user directory or database prefix.

Step 3: Pick the store

LangGraph stores. LangGraph treats short-term memory as thread-scoped state saved by a checkpointer, and long-term memory as items in a store under custom namespaces, recallable “at any time and in any thread”. Use InMemoryStore for development, a database-backed store in production, and add an embedding function to enable semantic search over memories.

Claude memory tool. Add {"type": "memory_20250818", "name": "memory"} to the request. Claude checks its memory directory before a task and writes what it learns as it goes; your code executes each view, create or edit request and returns the result. It is supported on the Claude API, Bedrock, Google Cloud and Microsoft Foundry, and is ZDR-eligible because storage is yours. Anthropic’s docs require you to restrict operations to /memories to prevent path traversal.

Mem0. A dedicated memory layer: you send conversations, it extracts and updates memories, and you query them. The hosted free plan covers unlimited end users with 10,000 adds and 1,000 retrievals a month for the first 30 days (Mem0 pays model calls), or 50,000 adds and 10,000 retrievals with your own LLM key. Graph memory, SSO, audit logs and on-prem are custom-plan features. For alternatives (Zep, Letta, Hindsight, cognee) see our ranking of agent memory systems.

Step 4: Write after, retrieve before

  • Write at the end of a task or turn, not every message: summarise the decision or fact, attach a timestamp and source, and update an existing memory instead of appending a contradiction.
  • Retrieve before the next task with a query built from the task, and cap it (for example the top 5 memories). Loading everything defeats the purpose and raises token costs.
  • Show provenance to the model (“remembered from Nov 12 session”) so it can weigh stale facts.

Step 5: Let memories expire and be deleted

Give users a way to view and delete what the agent remembers, and delete everything under a namespace when an account closes. Add a time-to-live for episodic memories that go stale. Memory files and stores are also an attack surface: treat retrieved memories as untrusted input, because a poisoned memory is a persistent prompt injection. Our guide on how to secure AI agent memory covers that in depth; which agent frameworks support long-term memory compares built-in options.

Last verified: December 5, 2026.

Sources