Memory in AI Agents: What to Keep, Compress and Throw Away

Building agents 9 min read

A card index drawer half open, with only a few cards pulled forward
Good memory design is mostly about what you are willing to leave in the drawer.

There is no memory in a language model call. Each turn is a fresh request, and the only thing the model knows is what you assembled into its context this time. Everything people describe as an agent forgetting, or getting worse over a long run, is a decision your code made about what to carry.

Once you accept that, memory design becomes an ordinary engineering problem with a familiar shape: what is always relevant, what is recently relevant, what can be summarised, and what should be fetched fresh instead of stored at all.

The four tiers#

Memory in AI Agents: What to Keep, Compress and Throw Away — The four tiers
TierContentTrimmed?Typical size
PinnedGoal, constraints, user identity, policyNever200–500 tokens
RecentLast few turns verbatim, with tool resultsRolling window2–5 turns
CompressedOlder turns as short factual notesRewritten as it growsUnder 500 tokens
RetrievedDocuments and records fetched for this stepDiscarded after usePer step

Compress facts, not prose#

The usual mistake is to summarise old turns as narrative — the user asked about their order and the agent looked it up. That reads well and helps nothing. Compress into the facts a later step might need: order 4471, status shipped, customer requested refund, refund policy allows 30 days, no refund issued yet. Structured, checkable, and a tenth of the size. If a later step is going to make a decision, the compression must preserve the inputs to that decision or it has failed at its only job.

Compress on a threshold, not every turn. Re-summarising a summary repeatedly is how details quietly disappear.

Retrieval is not memory#

Documents pulled from a knowledge base belong to the step that needed them. Keeping them in the running context after that step is finished is the fastest way to a bloated, expensive, distracted run. Fetch, use, cite, discard — and if a later step needs the same fact, fetch it again. Retrieval is cheap; a context full of stale documents is not.

Memory that persists between sessions#

Long-lived agents accumulate genuinely useful facts about a user or account: preferences, prior decisions, constraints that will not change. Store these deliberately, in a small structured record with an explicit write step, rather than by letting conversation history pile up. Three rules keep it healthy: write only facts a future run would act on, always record where a fact came from, and give every fact an expiry or a review date. Persistent memory without provenance and expiry becomes a slowly rotting source of confident errors.

  • Write on purpose — a tool call, not a side effect of chatting.
  • Store the source and the date alongside every fact.
  • Cap the record size and expire what has not been used.
  • Let the user see and correct what is stored about them.

Symptoms and their causes#

Memory in AI Agents: What to Keep, Compress and Throw Away — Symptoms and their causes
SymptomUsual cause
Forgets a constraint from early in the runConstraint was not pinned; it was trimmed with the history
Quality degrades after several stepsContext is diluted with stale tool output
Repeats a completed stepResult was summarised away without an outcome marker
Cost climbs with run lengthRetrieved documents are accumulating instead of being discarded
Confidently states something outdatedPersistent memory has no expiry or provenance

Frequently asked questions

How much history should I keep verbatim?

Three to five turns covers most reasoning without dominating the context. Keep tool results attached to their calls, and compress anything older into structured facts rather than dropping it silently.

Should I use a vector database for agent memory?

For retrieving documents, often yes. For the running state of a single run, no — that is a small structured object in your own store. Conflating the two produces both a fuzzy state machine and an unfocused search index.

How do I stop persistent memory from going stale?

Give every stored fact a source, a date and an expiry, and make the agent prefer freshly retrieved data when both exist. Expose the stored record to the user so wrong facts can be corrected rather than silently repeated.

ai agent memorycontext managementconversation summarisationpersistent agent memoryllm context window

All guides

Last updated 2026-08-04 by aiagentdevelopment.info · About us

Written by builders

Every guide is written by engineers who run agents in production, not spun from other sites.

Reviewed on a schedule

This field moves fast. Each guide carries the date of its last review, and we publish the date even when nothing changed.

No paid placements

No model provider, framework or agent platform can buy a mention, a ranking or a link here.

Twelve languages

Every guide is translated, not machine-popped — each language has its own URL and its own review date.

Limits named

We say plainly when a task does not need an agent and a plain script would be cheaper and more reliable.