Long-Term Memory is a pattern that persists information across separate conversations and sessions. It stores user preferences, learned facts, and interaction history in an external database, allowing the LLM to recall context from days, weeks, or months ago.
At a Glance
- Problem: Each session starts from zero, so facts a user shared earlier, such as a shellfish allergy, are gone by the next conversation. Users repeat themselves, and the assistant can give advice that contradicts what it was told.
- Forces: Remembering more versus noisy retrieval that crowds out important memories; persistent memories versus staleness as circumstances change; personalization versus privacy and data-retention obligations.
- Solution: A memory writer extracts facts, preferences, and decisions from conversations into a persistent store. A retriever searches that store at the start of each turn and injects relevant entries into the model's context.
- Consequences: Gains: continuity and personalization across sessions. Costs: a persistent store, embedding pipeline, and retrieval logic to operate, 100 to 300 milliseconds per turn, and an ongoing duty to update, deduplicate, and delete memories under user control.
What problem does Long-Term Memory solve?
A user talks to your AI assistant every day for a month. On day one, they mention they are allergic to shellfish. On day fifteen, they ask for dinner recommendations. The assistant suggests shrimp scampi. The user told the system something critical, and the system lost it. The context window holds only the current conversation, so for the model yesterday's session might as well never have happened.
This limitation shapes what LLM applications can and cannot do. Every conversation starts from zero, with no memory of previous interactions, so users repeat themselves, preferences get forgotten, and decisions discussed last week have to be re-established today. The experience feels impersonal and frustrating, especially for applications that are supposed to know the user over time.
Context windows have grown larger, but they do not solve this problem. Even a million-token context window only holds the current session. The information from previous sessions, from different channels, from weeks of accumulated interaction, does not exist from the model's point of view. You need a mechanism that persists information beyond the boundaries of a single conversation.
How does Long-Term Memory work?
Long-term memory gives an LLM application the ability to remember things across sessions by storing important information in an external persistent store and retrieving it when relevant.
The architecture has three components. A memory writer extracts noteworthy facts, preferences, and decisions from conversations and stores them. A memory store holds this information persistently. A memory retriever finds and injects relevant memories into the model's context when generating a response.
The memory writer can work in several ways. The simplest approach is to use the LLM itself to identify what is worth remembering at the end of each conversation turn. You prompt the model with something like: "Based on this conversation, what facts about the user should be remembered for future sessions?" The model extracts structured memory entries that get written to your store. Alternatively, you can use rules-based extraction, pulling out entities, stated preferences, or explicit instructions from the conversation.
The memory store can be a vector database, a key-value store, a relational database, or a knowledge graph. Vector databases are the most common choice because they enable semantic retrieval. You embed each memory entry as a vector and retrieve the most relevant ones based on semantic similarity to the current conversation. Key-value stores work well when memories are categorical (user preferences, settings, profile information). Knowledge graphs fit when the relationships between memories matter (this person manages that team, which works on this project).
The memory retriever runs at the start of each conversation turn. It takes the current user message, searches the memory store for relevant entries, and injects them into the system prompt or the beginning of the conversation context. The model then generates its response with awareness of past interactions.
Conceptually this is retrieval-augmented generation, except that the corpus is conversation history and extracted user facts rather than external documents. The retrieval mechanics are the same; what you retrieve, and why, is different.
When should you use Long-Term Memory?
Long-term memory belongs in any application that interacts with the same users repeatedly and where continuity improves the experience. Personal assistants, coaching applications, customer service systems, and productivity tools all benefit from remembering user context across sessions.
It matters most when users have complex needs that change over time. A project management assistant that remembers your team structure, current priorities, and past decisions can provide far more relevant suggestions than one that starts fresh every time.
Personalization is another strong signal. If your application recommends content, products, or actions, accumulated knowledge about user preferences makes those recommendations better over time. The first interaction is generic; by the hundredth, the application should be working from what it has learned about the user.
Long-term memory also makes sense when re-establishing context is costly. If a consulting chatbot needs 10 minutes of back-and-forth to understand the user's situation before it can help, remembering that context across sessions saves time for both the user and the system.
When not to use it: If users rarely return, or each session is self-contained, the store, embedding pipeline, and per-turn retrieval cost more than they give back; Conversation Memory within the session is enough. Avoid it where data-minimization rules or user expectations forbid keeping personal details beyond the session. If the knowledge to recall is shared documentation rather than facts about a user, index it with Basic RAG instead.
Worked Example: Cost and Latency
A personal assistant handles 10,000 sessions a day, 8 turns each, on the large model, with 1,500 input tokens and 250 output tokens per turn.
| Setup | Model calls per session | Tokens per session (in / out) | Cost per 1,000 sessions | Latency per turn |
|---|---|---|---|---|
| No memory | 8 | 12,000 / 2,000 | $66.00 | 4.7 s |
| Long-term memory (5 memories of 50 tokens per turn, writer at session end) | 8 large + 1 small | 18,000 / 2,150 | $72.70 | 4.9 s |
Without memory, each turn costs 1,500 × $3.00 per million = $0.0045 plus 250 × $15.00 per million = $0.00375, so 8 turns cost $0.066 per session. Injecting 250 tokens of memories adds 250 × $3.00 per million = $0.00075 per turn, or $6.00 per 1,000 sessions. The writer reads the 4,000-token transcript on the small model and writes 150 tokens: 4,000 × $0.15 per million + 150 × $0.60 per million = $0.00069, or $0.69 per 1,000. Embedding queries and new memories, about 310 tokens per session at $0.02 per million, adds about $0.01. Retrieval adds 0.2 s per turn, the midpoint of this page's 100 to 300 ms, and the writer runs after the session, so users never wait on it.
Memory adds about 10% to cost and 0.2 s per turn, which is cheap if it saves users from repeating themselves, but the cost grows quickly if retrieval injects more or longer memories than the 250 tokens assumed here.
What are the common pitfalls?
Storing everything is tempting, and it backfires. If you write every conversational detail to memory, retrieval becomes noisy. The model's context fills up with marginally relevant memories that crowd out the important ones. Decide what to keep. Much of a conversation is not worth remembering, and some memories should be updated or deleted as circumstances change.
Stale memories cause problems. The user mentioned they were working on Project Alpha six months ago. They have since moved to Project Beta. If the old memory persists without being updated, the assistant will reference an outdated context. You need a way to update, archive, or expire entries.
Privacy and data retention are serious concerns. Long-term memory is, by definition, a store of personal information. Users may have shared sensitive details in conversation that they did not intend to be permanently recorded. You need clear policies about what gets stored, how long it persists, and how users can view or delete their memories. In regulated environments, memory retention may conflict with data minimization requirements.
Retrieval will sometimes pick the wrong memories. Semantic search can surface entries that are topically related but not useful for the current query. A question about "Python" might retrieve memories about a snake identification project when the user is asking about the programming language. Including metadata (timestamps, categories, confidence scores) in your memory entries helps the retriever make better selections.
What are the trade-offs?
Long-term memory adds infrastructure. You need a persistent store, an embedding pipeline, and retrieval logic. None of it is exotic, but someone has to run it, and the operational burden scales with the number of users and the volume of memories.
Latency increases slightly because each turn requires a retrieval step before generation. For most applications, this adds 100-300 milliseconds, which is acceptable. For latency-critical applications, you may need to run retrieval in parallel with other preprocessing.
Your extraction and retrieval pipeline caps the quality you can reach. Memories that are poorly extracted or poorly retrieved degrade the model's performance rather than improving it. Irrelevant memories waste context tokens and can confuse the model. A weak writer, store, or retriever drags down the whole system.
You are making a commitment to data management. Memories need to be updated, deduplicated, and sometimes deleted. Users need controls over their stored information. That work continues for as long as the system runs; you do not finish it at launch.
Goes Well With
Conversation Memory handles within-session context management. Long-term memory handles across-session persistence. Together, they give the model both recent conversational context and historical knowledge about the user. Most production systems need both.
Basic RAG uses the same retrieval infrastructure but searches external documents instead of user memories. You can share the same vector store and retrieval pipeline for both, searching user memories and knowledge base documents in a single query and combining the results.
Semantic Indexing improves the quality of memory retrieval. Better embeddings and smarter indexing strategies lead to more relevant memory recall, so the model receives stored information it can use.