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.

Intermediate 3 min read · Updated

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

TypeHoldsWhere it livesExample
Working (short-term)The current conversation and task stateThe prompt / context windowLast 10 turns, current plan, tool results
EpisodicPast interactions and their outcomesDatabase + vector index"Last week the user asked about migrating to annual billing"
SemanticFacts and preferences about the user or domainKey-value or document store"Prefers Python; works in UTC+5:30; on the Pro plan"
ProceduralHow to do things: instructions, learned rulesSystem 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.
System + toolsMemoriesSummary of older turnsRecent turns verbatim6KRetrieved docs4KRoom for output
A context budget for one turn of a long conversation (illustrative). Recent turns are kept verbatim, older ones compressed into a summary, and long-term memories retrieved only when relevant.

Long-term memory architecture

ConversationExtract memoriesLLM · asyncDedupe / updatemerge or replaceMemory storeper user · vector + KVNew turnRetrieve relevanttop 5 + profilePrompt
Long-term memory: after a conversation, an extraction step proposes memories, which are deduplicated and stored with provenance. On each new turn, relevant memories are retrieved and added to the prompt.
  • 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

Personal assistants

Preferences and past topics recalled across sessions, with a memory page users can edit.

Support bots

Customer history and previous tickets retrieved per conversation, scoped to the account.

Coding agents

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