How is long term memory typically implemented in an agent?
Long-term memory persists across tasks in a vector store, key-value store, or database. The agent retrieves relevant entries each task and injects them into the prompt; the model weights stay frozen.
Imagine a friend you only see once a month. They cannot literally remember everything you ever told them, so they keep a small notebook with the important facts: 'allergic to peanuts, has a daughter named Mia, prefers tea to coffee.' Before they meet you again, they flip to your page. Long-term memory works that way for an agent. It cannot keep every conversation alive in its head. So important facts go into a notebook (a database). Before each new task, the agent looks up the relevant page and reads it. The model's brain has not changed at all. The notebook has just grown by a few lines.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Long-term memory is the persistent layer of an agent's memory: the bytes that survive after one task ends and are still available when the next task begins. Where short-term memory is the per-task scratchpad rendered into the prompt, long-term memory is the external store the runtime owns and queries selectively.
The distinction matters because of how the underlying model works. A language model at inference time is frozen. The user can talk to it for hours, and not one weight changes. Short-term memory exists only inside the prompt, and the prompt is rebuilt on each call. So every form of memory the user perceives across days, sessions, or weeks must live somewhere outside the model, in storage the runtime explicitly reads and writes.
This explanation builds up what long-term memory contains, the storage shapes used to implement it, the three operations (write, read, maintain) that govern it, and the production failure modes a serious design has to handle. By the end you should be able to look at any 'the agent remembers me' feature and decompose it into these pieces.
What long-term memory contains and where it lives
Three categories of content account for most production long-term memory.
The first is user profile data: stable facts about the user (name, language, role, preferences, allergies, dietary restrictions, account identifiers, locale). These tend to be small, structured, and updated rarely.
The second is interaction history summaries. A finished task is summarised down to a compact set of facts (the user planned a Tokyo trip arriving 9am Haneda, allergic to peanuts, asked about ramen restaurants) and the summary is persisted. Raw transcripts are usually too large to retrieve efficiently, so the summary is the primary artifact, with the trace remaining available in observability.
The third is learned heuristics or reflections. After repeated failure modes, the agent or a meta-agent writes down lessons like 'when the user says urgent, escalate to overnight shipping' or 'this customer's payment method fails on weekends'. These are the cross-task equivalent of Reflexion-style critiques and are explicitly retrieved when the agent decides what to do next.
Storage choices follow content shape. Free text and summaries go into a vector store for similarity retrieval; common 2026 choices are Pinecone, Chroma, pgvector (used heavily by teams already on Postgres), Weaviate, and Qdrant. Structured profile data goes into a relational or key-value store with exact lookup. Relationships between entities (this user belongs to this team, used these products) fit a graph or document store. Production systems often combine two or three: a relational user table joined with a pgvector table of summaries, queried together at retrieval time.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- Personal assistants built on the OpenAI Agents SDK or Anthropic SDK store user-fact summaries in a vector store like Pinecone or Chroma, retrieved on each new session.
- Coding agents like Cursor and Devin store prior project context and reflections in a per-project memory file or vector index that the agent retrieves on each new session.
What an interviewer would ask next. Try answering before peeking at the approach.
QLong-term memory and RAG both retrieve text and inject it into the prompt. What is the difference?
RAG retrieves from a static or shared knowledge base (docs, web pages, manuals) to answer questions. Long-term memory retrieves from a per-user write-back store of past interactions. The mechanics overlap (vector store + retrieval), but the source of the data and the write path differ sharply.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Believing long-term memory updates the model. The model weights are frozen at inference. Long-term memory is an external store the runtime queries; nothing about the model itself ever changes.
60 second bullets to scan on the way to the call.
Define long-term memory as cross-task persistence external to the model.
Name the contents: user preferences, prior summaries, learned facts, trajectory reflections.
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.