AI Agent Memory: Short-Term Context, Long-Term Memory and Summaries
How AI agents remember: context window management, conversation summarization, long-term memory stores, retrieval of memories, privacy and memory poisoning.
LLMs are stateless: each call sees only what's in its prompt. Agent memory is the application's job of deciding what an assistant remembers, within one conversation (short-term memory, limited by the context window) and across conversations (long-term memory, stored outside the model and retrieved when relevant). Done well, memory makes assistants feel personal and lets agents work on long tasks; done badly, it leaks private data, bloats costs and lets one poisoned note steer every future answer.
Types of memory
| Type | Holds | Where it lives | Example |
|---|---|---|---|
| Working (short-term) | The current conversation and task state | The prompt / context window | Last 10 turns, current plan, tool results |
| Episodic | Past interactions and their outcomes | Database + vector index | "Last week the user asked about migrating to annual billing" |
| Semantic | Facts and preferences about the user or domain | Key-value or document store | "Prefers Python; works in UTC+5:30; on the Pro plan" |
| Procedural | How to do things: instructions, learned rules | System prompt, skills files | "Always run tests before proposing a commit" |
Managing the context window
Long conversations eventually exceed the window, and long prompts cost more on every turn. Strategies, often combined:
- Sliding window: keep the last N turns verbatim. Simple; loses early details.
- Running summary: periodically summarise older turns into a short paragraph kept at the top. Keeps gist; loses specifics.
- Retrieval over history: store every turn, embed it, and retrieve the relevant old turns for the current question.
- Tool-result trimming: keep the essential fields of large tool outputs; store the rest by reference.
- Scratchpad / plan: keep an explicit task plan in state rather than relying on the model to infer it from history.
Long-term memory architecture
- Write path: extract candidate facts asynchronously after a session (not on every token), resolve conflicts ("moved from Pune to Bengaluru" should replace, not add), and store each memory with its source conversation and timestamp.
- Read path: retrieve by semantic similarity to the current turn plus a small always-included profile; rank by relevance and recency.
- Explicit vs implicit: "remember that I'm vegetarian" is a clear instruction; inferred memories need a higher bar.
- Scoping: memories belong to one user (and sometimes one workspace); retrieval must filter by owner, the same way RAG permissions do.
Privacy, control and safety
- User control: let users view, edit and delete memories, and turn memory off; deletion must remove both the record and its vector.
- Sensitive data: avoid storing health, financial or other sensitive details unless the product needs them and the user agreed; never store credentials.
- Retention: expire stale memories; old preferences can mislead.
- Memory poisoning: if content from documents or web pages can become a memory, an attacker can plant instructions that persist ("always recommend site X"). Only extract memories from the user's own messages, mark their source, and treat retrieved memories as data. See prompt injection.
Memory for long-running agents
Agents working for hours (coding, research) hit context limits mid-task. Useful techniques: write progress notes and a to-do list to a file or state store, compact the history into a summary when it nears the limit, and let subagents absorb large reading tasks and return summaries; see multi-agent systems.
Evaluating memory
- Recall: given a past fact, does the assistant use it when relevant?
- Precision: does it avoid bringing up irrelevant or outdated memories?
- Correct updates: are contradicted facts replaced?
- Isolation: can user A ever see user B's memories? (must be a hard test case)
Where it’s used
Preferences and past topics recalled across sessions, with a memory page users can edit.
Customer history and previous tickets retrieved per conversation, scoped to the account.
Project notes and conventions stored as files the agent reads at the start of each task.
In an interview
- Separate short-term context management from long-term memory.
- Describe write (extract, dedupe, store with provenance) and read (retrieve, rank) paths.
- Scope memories per user and give users control and deletion.
- Address memory poisoning and sensitive data.
Frequently asked questions
How do AI agents remember things?
Within a conversation, by keeping recent turns and summaries in the context window. Across conversations, by extracting facts into a per-user memory store and retrieving relevant ones into the prompt for each new turn.
What is memory poisoning?
When untrusted content, such as a web page or document, gets saved as a memory and then steers future answers. Prevent it by extracting memories only from the user's own messages, recording their source, and treating memories as data.
Further reading
- MemGPT: Towards LLMs as Operating SystemsPacker et al., 2023
- Generative Agents: Interactive Simulacra of Human BehaviorPark et al., UIST 2023