Skip to content
mentoor

Topic 2 of 5 · 5 min read

Long-term memory stores and retrieval

Goal: Choose a long-term memory store for a described agent and define what gets written, when, and how it is retrieved.

Long-term memory is any store outside the context window that survives the end of a session, plus the rules for what goes in and how it comes back out. The model never "has" these memories. A tool or the harness reads them and puts the relevant ones into the window.

Check yourself · 1 of 3

A shopping agent should remember each customer's shoe size and preferred currency. Which store fits best?

Choose one answer

Why it matters

Without it, every session starts blank. A support agent asks the same questions each week. A coding assistant forgets that the team uses pnpm. But every stored fact also costs window space when retrieved, and a wrong one is trusted. So the design question is not "can it remember" but "what, where, and how does the right piece come back".

Choosing a store

  • Key-value or profile record. A small structured document per user: {"language": "TypeScript", "package_manager": "pnpm"}. Exact lookup, cheap, easy to show and edit. Best for stable preferences and settings.
  • Vector store. Text is turned into embeddings and searched by meaning. Best for many free-text notes where you do not know the exact words, such as past support cases. It can return near misses, and it has no sense of which note is newer.
  • Graph or relational store. Entities and links: "Dana manages Team Atlas; Atlas owns the billing service". Best when answers need relations or exact filters (by date, by project).
  • Plain files. Covered in the next topic; often the simplest start.

LangChain's docs split memories by type: facts (semantic), past events (episodic) and rules for behaviour (procedural). Different types often want different stores.

Write and read paths

  1. Decide the write trigger. After each session, when the user says "remember", or when the agent calls a save tool mid-task.
  2. Store a small, self-contained record with a date, a source and a scope (user, project, team).
  3. Retrieve on need. Search at the start of a task, or give the agent a search_memory tool so it asks when it needs one. Return the top few results, not everything.

Before and after

Before: the agent saves the whole transcript of every chat into a vector store and retrieves 10 chunks each turn. Answers fill with unrelated old chatter. After: it saves one line per decision ("User prefers metric units, stated 2026-11-04"), filters by user id, and retrieves 3.

What goes wrong

  • Storing everything. Retrieval gets noisy and the right memory loses to similar junk.
  • No scope. One user's note comes back for another user.
  • Wrong store for the question. "What did Dana say on Tuesday?" is a filter, which similarity search answers poorly.

Examples

Picking a store for a travel agent

A booking agent needs three things: the traveller's seat and meal preferences, notes from past trips ("hated the hotel near the airport"), and who in the company approves expenses.

  • Preferences: a profile record per user, shown in settings.
  • Past trip notes: vector store, filtered by user id.
  • Approver chain: the company database, queried exactly.

One store for all three would handle at least one of them badly.

A memory record that retrieves well

{
  "user_id": "u_8841",
  "fact": "Deploys go through the staging branch first",
  "source": "user message, session 2026-11-18",
  "scope": "project:checkout",
  "created": "2026-11-18"
}

It can be filtered by user and project, dated for expiry, and traced back to where it came from.

Key points

  • Long-term memory is an external store; the model only sees what retrieval puts back into the window.
  • Use a profile or key-value record for stable preferences, a vector store for fuzzy recall over free text, and a graph or database for relations and exact filters.
  • Write small, dated, scoped records instead of whole transcripts.
  • Retrieve a few results on demand, filtered by user or project, rather than loading everything each turn.
  • Memory types differ: facts, past events and behaviour rules often need different stores.

Common mistakes

  • Dumping full transcripts into a vector store and calling it memory.
  • Saving memories with no user, project or date, so they cannot be filtered or expired.
  • Using similarity search for questions that need an exact filter, such as a date or an id.
  • Retrieving ten or more results every turn and filling the window with near matches.

Check yourself · 2 of 3

What does it mean that a stored memory is "retrieved"?

Choose one answer

Check yourself · 3 of 3

An agent stores one embedding per full chat and retrieves the top 10 every turn. Answers get worse as history grows. What is the best change?

Choose one answer

Practice items, not counted towards your mastery.

Start here