Long-Term Memory for AI Agents: Patterns and Storage
Advanced4 minInnovialabs editorial team
In short
Long-term memory means storing selected information outside the model and loading only what is relevant into each request; combine a user profile, extracted facts and vector recall, give every memory a source and expiry, and let users see and delete it.
How agent memory actually works
A language model remembers nothing between requests. Everything it "knows" about a user in a conversation is what you put into the context window. Long-term memory is therefore an application feature: you store selected information in your database and retrieve the relevant parts into the prompt for each new request.
The goal is not to remember everything. The goal is to remember the right things, keep them accurate over time, and forget them when needed.
Four memory patterns
| Pattern | What is stored | Good for | Weakness |
|---|---|---|---|
| Rolling summary | A compressed recap of past dialogue | Continuing long conversations | Details get lost with each rewrite |
| Extracted facts | Short statements: "prefers delivery after 18:00" | Preferences, decisions, constraints | Needs good extraction and conflict handling |
| Vector recall | Embeddings of past messages or notes | "What did we discuss about X?" | May retrieve outdated or irrelevant pieces |
| User profile | Structured fields: language, city, plan, role | Stable attributes, personalization | Rigid, only covers known fields |
Most production agents combine them: the profile is always loaded, facts relevant to the current topic are added, vector recall fills gaps, and a summary of the current session keeps the thread.
Writing memories
Do not save every message. Run a separate memory writer step after a turn or at the end of a session:
- The model reads the recent exchange and proposes candidate memories.
- Each candidate is checked: is it about the user, durable, useful later, and stated by the user rather than guessed?
- It is compared with existing memories: add, update, merge or ignore.
- It is saved with metadata.
A minimal memory record:
{
"user_id": "u_123",
"type": "preference",
"text": "Prefers answers in Uzbek",
"source": "chat:2026-10-02:msg_88",
"confidence": "stated",
"created_at": "2026-10-02",
"expires_at": null,
"sensitivity": "low"
}
The source field lets you trace why the agent believes something. The confidence field separates what the user said from what the model inferred; inferred facts should be used carefully or confirmed with the user.
Reading memories
Retrieval quality decides whether memory helps or confuses the agent.
- Filter first, then search: by user, by type, by validity date. Never search across all users.
- Rank by relevance and recency: a fresh fact usually beats an old one on the same topic.
- Limit the volume: a few relevant memories are better than many loosely related ones.
- Show memories as data: put them in a clearly marked block of the prompt so the model treats them as context, not instructions.
Expiring and updating
Memories go stale. A customer moves to another city, changes their plan, or finishes a project.
- Give time-bound facts an expiry date: "is travelling until Friday".
- On a contradiction, replace the old fact and keep a history entry if you need an audit trail.
- Periodically consolidate: merge duplicates, drop memories that were never retrieved.
- Recompute rolling summaries from facts instead of summarizing summaries endlessly, which degrades detail.
Privacy controls
Remembered data is personal data, often more sensitive than the original chat because it is condensed and organized.
- Tell users that the agent remembers, and what kind of information.
- Let users view, edit and delete their memories, and support deleting everything at once.
- Do not store sensitive categories like health, payment details or passwords unless the product truly requires it and you have a legal basis.
- Isolate tenants strictly: every query is scoped by user and organization IDs at the database level.
- Delete everywhere: when a memory is removed, remove its embedding, caches and copies in logs as well.
- Apply data localization rules to the memory store just like to your main database.
Common mistakes
- Saving raw transcripts and calling it memory.
- Storing model guesses as facts.
- No expiry, so the agent confidently uses outdated information.
- Memory retrieval without user filtering, leaking data between accounts.
- Treating stored text as trusted instructions, which opens the door to prompt injection through memory.