AI agent memory works by storing information outside the language model, retrieving the most relevant pieces on demand, and pasting them back into the model's limited context window on every call. The model itself never remembers anything. Each API call to a large language model (LLM) starts blank: it has no record of your last message, your last session, or the fact you told it your name five minutes ago. What looks like a continuous, learning agent is really a stateless model wrapped in an external system that decides what text to show it next.
Two ideas need fixing before the mechanics make sense. First, the human-brain vocabulary ("short-term," "episodic," "semantic") makes it sound like the agent is recalling things from inside itself. It isn't. Nothing gets written into the model. Memory lives entirely outside it: databases, files, and summaries that feed text into a fixed prompt. Second, "more memory equals a smarter agent" is false. Dumping large amounts of retrieved text into the context makes answers worse, because the model's attention thins out and irrelevant passages pull it off track. Good memory is about retrieving less but righter, not more.
Why Agents Need Memory At All
An LLM is stateless: it handles every call on its own, with nothing carried over between them. The model computes an output from the tokens in front of it, then throws everything away. Send it a follow-up question with no history attached, and it has no idea what "it" refers to.
That's why continuity has to be simulated. The agent's software keeps a record of the conversation and past facts somewhere durable, then rebuilds the relevant slice of that record into each new prompt. The illusion of memory comes from disciplined re-injection, not from the model holding state. Change the software layer and the "memory" changes; the model underneath stays identical either way. That distinction matters because it tells you where to fix problems: memory bugs live in your retrieval and prompt-assembly code, never in the model weights.
The Hard Limit: The Context Window
Everything the model can "see" in a single moment must fit inside its context window: a fixed maximum number of tokens (roughly, word fragments) it can process at once. This is the constraint every memory technique works around. Once the window is full, something has to be dropped, summarized, or moved to external storage.
| Model | Context window (tokens) |
|---|---|
| GPT-3.5 | ~8,000 |
| GPT-4o | 128,000 |
| Claude (3.5 Sonnet) | 200,000 |
| Gemini 1.5 Pro | 1,000,000+ |
Bigger windows help, but they aren't a memory system. A million-token window still fills up over a long-running agent's lifetime, costs more per call as you pack it fuller, and loses focus on the middle of long inputs. You still need something outside the window deciding what deserves a seat inside it.
The Types of Memory
Agent memory is usually split into four categories. They differ by what they hold and, more importantly, where that content physically lives: inside the context window (working memory) or in an external store (everything else).
| Type | What it holds | Where it lives | Example |
|---|---|---|---|
| Working / short-term | Current chat, recent tool outputs, task state | The context window | The messages in this conversation so far |
| Semantic | Facts and preferences | External store | "User prefers Python" |
| Episodic | Past events and interactions | External store | "Tuesday we debugged the auth script and fixed it by rotating the API key" |
| Procedural | How-to rules and skills | System prompts and tool schemas | "Always confirm before deleting" |
The labels come from cognitive psychology, but don't read them literally. Only working memory is "live" inside the model. Semantic and episodic memory are rows in a database that get pulled in as text when relevant. Procedural memory is mostly the fixed instructions and tool definitions you write into the system prompt — behavior the agent follows, not knowledge it looks up.
How Memory Retrieval Works
Retrieval is the engine that turns an external store into usable context, and the dominant pattern is Retrieval-Augmented Generation (RAG): convert stored text into numeric vectors, find the ones closest in meaning to the current query, and inject those into the prompt. Here is the pipeline end to end.
- Chunk the text. Split documents and past conversations into passages, sentences or paragraphs, small enough to embed and retrieve precisely.
- Convert each chunk to an embedding. An embedding model (OpenAI's
text-embedding-3, or open-source options like BGE and E5) maps text to a high-dimensional vector (often 1,536 numbers) that encodes its meaning. - Store the vectors. Save each embedding plus its metadata (timestamp, source, tags) and the original text in a vector database.
- Embed the incoming query. When a new input arrives, run it through the same embedding model to get a query vector.
- Run a similarity search. Compare the query vector against stored vectors using cosine similarity or nearest-neighbor search, sped up by approximate algorithms like HNSW or IVF.
- Pull the top-K. Take the K most similar chunks, typically 3 to 10, not everything that matches.
- Inject into the context window. Paste those chunks into the prompt before the model generates its answer.
The top-K limit is deliberate. Retrieving 50 loosely related chunks is worse than retrieving 5 sharp ones, because the extra text crowds out the useful signal.
Where Long-Term Memory Is Stored
Long-term memory lives in three kinds of store, and mature agents use them together rather than picking one. Each answers a different question: fuzzy recall, exact relationships, or precise state.
| Storage type | Best for | How it works | Example |
|---|---|---|---|
| Vector database | Fuzzy semantic recall | Embeddings + similarity search | Pinecone, Weaviate, Chroma, pgvector, FAISS, Milvus |
| Knowledge graph / GraphRAG | Relational, multi-hop reasoning | Nodes + edges | Neo4j, Neptune — [User]-PREFERS->[Python] |
| Relational / SQL | Exact lookups and state | Tables | Postgres/MySQL for task status, financial figures |
Vector search alone isn't enough. It finds text that feels similar, which is perfect for "what did the user say about their project," but it misses exact facts, sequence, and relationships. Ask "which projects use the framework the user prefers," and similarity matching flounders — that's a graph traversal. Ask "what is the current status of task 47," and you want a SQL row, not a fuzzy match. The stores complement each other. A knowledge graph stores entities and the explicit edges between them, so the agent can follow logical paths a vector database cannot represent.
The Live Memory Loop
In practice, a memory-enabled agent runs the same cycle on every turn: it reads the input, pulls relevant memories, builds a prompt, acts, then writes new memories back. The short-term working context and the long-term stores both refresh on each pass.
- Receive input. A user message, an API event, or an observation from a tool.
- Retrieve relevant memories. Run a vector similarity search plus any structured lookups (graph or SQL), and optionally re-rank the results with a cross-encoder or a service like Cohere Rerank to push the best matches to the top.
- Assemble the prompt. Combine, in order, the system instructions (procedural), the retrieved memories (semantic and episodic), the recent conversation history, and the current input into one prompt.
- Generate or act. The model produces a response or calls a tool.
- Write new memories. Extract new facts, embed and store them, and summarize aging conversation turns to free up working memory.
Then it repeats. The model is called fresh each time; the loop is what makes it feel like one continuous mind.
An Agent Memory Cycle in Practice
Walk a single turn through concretely. You tell an agent, "Book the same flight as last time." Step 2 embeds that sentence and searches the vector store, which returns an episodic memory: "User booked United 1123, SFO→JFK, on March 3." A SQL lookup confirms the fare class from a stored booking record. Step 3 assembles a prompt containing the system rules ("confirm before purchasing"), that retrieved memory, the last few messages, and your request. Step 4, the model drafts a confirmation and calls the booking tool. Step 5 writes the new booking as a fresh episodic record. None of this required the model to "remember" anything — the software found the March 3 record and handed it over as text.
Keeping Memory Useful Over Time
Storing everything forever makes retrieval slower, noisier, and more expensive, so agents actively curate what they keep. Several techniques manage the pile-up.
| Technique | What it does |
|---|---|
| Summarization | Compresses old conversation turns into a running summary that survives in fewer tokens |
| Importance / relevance / recency scoring | Ranks memories for retrieval by how recent, how important, and how relevant they are |
| Decay / forgetting | Archives or deletes low-value entries to cut noise and cost |
| Reflection | Periodically synthesizes higher-level insights from many past events |
| Paging / memory tiers | Treats the context window as RAM and external storage as disk, moving data between them |
The scoring approach has a well-known reference implementation. Stanford's 2023 Generative Agents paper ranked each memory by combining three signals: recency (an exponential decay favoring recent memories), importance (a 1–10 score the LLM itself assigns to how significant the memory is), and relevance (vector similarity to the current situation). That combination is now a common template for deciding which memories earn a place in the prompt.
Reflection goes a step further: instead of storing raw events, the agent occasionally reviews them and writes a distilled conclusion — for example, "I should always check timezone differences when scheduling." That insight then gets retrieved like any other memory.
Frameworks and Tools
Most teams don't build memory from scratch; they use libraries that package retrieval, storage, and prompt assembly. The main options differ in how much they automate.
| Tool | What it provides |
|---|---|
| LangChain / LangGraph | Memory classes (ConversationBufferMemory, ConversationSummaryMemory, VectorStoreRetrieverMemory) and checkpointers for state persistence |
| LlamaIndex | Indexing and retrieval modules that connect data sources to the model |
| Mem0 | Automatic fact extraction and updating into a dedicated memory layer |
| Letta (formerly MemGPT) | OS-style paging with core, archival, and recall memory tiers |
| Zep | Managed long-term memory service with automatic fact extraction |
Letta's design is worth understanding because it makes the RAM-versus-disk analogy literal. Core memory stays resident in the context window (guaranteed visible), archival memory is a vector store the agent queries on demand, and recall memory holds the message history. An LLM-driven control process pages information between these tiers using function calls — the agent manages its own context. For multi-agent setups, AutoGPT persists reasoning across sessions with file and vector logs, while CrewAI and MetaGPT use shared short-term memory so several agents can coordinate on a task.
What Goes Wrong
Memory systems fail in specific, recurring ways — and knowing them tells you why "just retrieve more" backfires.
- Lost in the middle. Models attend most to the start and end of a long prompt and skim the middle. A critical memory buried in the center of a large injected block can be effectively ignored.
- Context rot and pollution. Too much retrieved text dilutes attention and degrades reasoning. Irrelevant chunks actively distract the model.
- Retrieval inaccuracy. Bad chunking or an embedding mismatch surfaces the wrong passage, and the model confidently builds a hallucinated answer on top of it.
- Latency and cost. Every retrieval step (embedding, search, re-ranking) adds delay, and continuously embedding and storing millions of interactions gets expensive at scale.
- Contradiction handling. When new information conflicts with old ("the user moved cities"), the system has to update or supersede the stale fact, or it will retrieve both and confuse the model.
- Missing sequence and causality. Vector similarity captures "feels similar" but not order or cause. "What did we do before the deploy failed" needs temporal metadata or a graph, not a nearest-neighbor search.
- Privacy. Persisted memory retains personal data, which requires encryption, access control, and a real deletion path.
Who Should NOT Build a Memory System
Skip a memory system entirely if your use case is a single-shot question with no continuity across turns. A one-off classification, a stateless translation endpoint, a summarizer that takes a document and returns a summary — none of these benefit from external memory. You just need a good prompt. Adding a vector database and retrieval loop to a stateless task buys you latency, cost, and new failure modes for no gain.
If your whole relevant history fits comfortably in the context window and the session is short, plain conversation history in the prompt is your memory — you don't need a vector store yet. Reach for external memory when history outgrows the window, when facts must survive across sessions, or when the same knowledge is reused across many conversations. Below that threshold, the memory system is just overhead.
FAQ
Does an AI agent's memory change the language model itself?
No. None of these mechanisms touch the model's weights. Memory is runtime scaffolding that changes what text the model sees on each call, not what the model has learned. Updating the model itself would require fine-tuning or retraining, which is a separate process from agent memory.
Is a bigger context window a replacement for a memory system?
No. A larger window is more capacity, not memory management. Even million-token windows fill up over a long-running agent's life, cost more as you pack them, and lose track of information buried in the middle. You still need external storage plus logic that decides what to load.
Why not just retrieve every memory that might be relevant?
Because more retrieved text usually makes answers worse. Irrelevant passages dilute the model's attention and can trigger hallucinations, and long prompts suffer the "lost in the middle" effect. Agents cap retrieval at a top-K of roughly 3 to 10 and re-rank for precision, aiming for the fewest, most relevant memories.
What is the difference between short-term and long-term agent memory?
Short-term (working) memory is the content actually inside the context window right now — the current chat and recent tool outputs. Long-term memory is stored externally in vector databases, knowledge graphs, or SQL tables and persists across sessions. Long-term memory becomes usable only when retrieved and injected into the short-term window.
Is vector similarity search enough on its own?
No. Vector search finds text that feels semantically similar, which misses exact facts, sequence, and explicit relationships. That's why production agents pair vector databases with knowledge graphs (for multi-hop relational queries) and relational databases (for exact lookups and state tracking).
References
- OpenAI, GPT-4o and embedding model documentation (
text-embedding-3) - Anthropic, Claude model documentation (200K context window)
- Google, Gemini 1.5 Pro documentation (1M+ token context)
- Park et al., "Generative Agents: Interactive Simulacra of Human Behavior," Stanford, 2023
- Packer et al., "MemGPT: Towards LLMs as Operating Systems," 2023 (Letta)
- Liu et al., "Lost in the Middle: How Language Models Use Long Contexts," 2023
- LangChain, LlamaIndex, Mem0, and Zep official documentation