← All topics
Knowledge

Memory: what the agent keeps between turns

An agent's memory has two layers — the short-term context window and a long-term store you retrieve from — and the skill is deciding what to keep, summarize, and forget.

What it is

Memory is everything your agent carries forward so each turn isn't a blank slate. It comes in two layers.

  • Short-term: the context window — the running conversation, tool results, and a scratchpad the model sees on this turn. Fast, but bounded and lost when the run ends.
  • Long-term: an external store (a database, vector index, or file) you write to and retrieve from across sessions. Durable, unbounded, but only present when you fetch it.

The pattern is deciding what lives where.

When to use it

Reach for explicit memory when:

  • A task spans more turns than fit comfortably in context, so you summarize old turns instead of dropping them.
  • The agent should remember facts across sessions — user preferences, prior decisions, a project's state.
  • You want retrieval of past knowledge on demand rather than stuffing everything into every prompt (see rag).

Don't bother for short, single-shot tasks. If the whole job fits in one context window and nothing needs to outlive the run, plain conversation history is your memory — adding a store is just overhead.

How it works

You manage three moves: persist, summarize, forget.

  • Persist: when something matters beyond this turn, write it out — memory.save("user prefers metric units").
  • Summarize: when context fills up, compress old turns into a short recap and keep that instead of the raw transcript.
  • Retrieve: on a new turn, pull only the relevant slice back in — memory.recall("units") — and inject it into the prompt.
  • Forget: drop or expire anything stale so it can't poison future reasoning.

Example flow: turn 1 the user says they're in Berlin, you save it; turn 40 the window is full, you summarize turns 1-30; turn 41 they ask about the weather, you recall "Berlin" and answer without re-reading the whole history.

Watch out for

  • Lossy summaries: compression silently discards the one detail you needed later. Summarize facts and decisions, not vibes, and keep raw records retrievable.
  • Stale memory: an old fact retrieved as current is worse than no memory. Timestamp entries and expire or overwrite them.
  • Retrieval misses: if recall returns the wrong slice, the agent reasons confidently on bad context. Treat retrieval quality as something to measure with evals.
  • Unbounded growth: every saved item is future cost and noise. Be deliberate about what earns a place in long-term store — see cost control.

Related patterns

Ask the swarm