AppliedAIPrep logoAppliedAI/Prep
RAG & Agent System Design / 10
hard★ EssentialAnthropicOpenAISierra

How do you manage memory and context for a long-running conversational agent?

Conversations and agent tasks outgrow the context window, and naive 'stuff the whole history' fails on cost, latency, and lost-in-the-middle. The signal is a tiered memory design: recent buffer, summarized mid-term, retrieved long-term.

Updated Aug 2026 · Grounded in real Applied AI Engineer interview loops and written to a senior-engineer editorial bar.

TL;DR: Do not dump the full history into context. Use tiered memory: a short-term buffer of recent turns verbatim, a running summary of older turns (compressed), and a long-term store (vector DB or structured memory) you retrieve from on demand. Keep durable facts (user preferences, decisions) as structured state, not buried in transcript. This bounds tokens, cost, and latency, avoids lost-in-the-middle, and keeps the agent coherent over long sessions.

CONTEXT WINDOW (add turns until it overflows)
15 / 28 tokens
system
turn 1
turn 2
The window holds a fixed budget of 28 tokens. Add turns and the oldest fall out of the window once it is full (0 evicted so far). Everything inside is reprocessed every call, and facts buried in the middle are the easiest for the model to overlook.

How to approach it. State the constraint: context windows are finite and not free (cost, latency, and lost-in-the-middle all degrade with length), so for a long session you must decide what to keep in context, not keep everything. Then lay out the tiers and how facts persist as structured state.

A strong answer. The problem: a long conversation or multi-step task accumulates more history than fits (or than is affordable or effective) in the context window. Even with large windows, stuffing everything costs tokens on every turn, raises latency, and suffers lost-in-the-middle (the model attends poorly to buried context). So you manage memory in tiers:

  • Short-term (working) memory. Keep the last N turns verbatim, the immediate conversational state the model needs for coherence right now.
  • Mid-term: summarization / compression. As turns age out of the buffer, summarize them into a running summary that stays in context (recursive or rolling summarization). You trade fidelity for a bounded token footprint, keeping the gist without the full transcript.
  • Long-term memory: retrieval. Store the full history and prior sessions in a vector store and retrieve only the relevant pieces when the current turn needs them (RAG over the conversation). This scales to arbitrarily long histories: context holds recent turns, the summary, and a few retrieved snippets, not everything.
  • Structured state for durable facts. Critical, reusable information (user preferences, account details, decisions made, the task goal) should live as explicit structured state, not be left to be re-summarized or re-retrieved from transcript. Update it deliberately so the agent reliably remembers key facts across turns and sessions.
TierWhat it holdsMechanismFailure if misused
Short-termLast N turns verbatimIn-context bufferCoherence loss if too small
Mid-termOlder turns, compressedRolling summaryLossy: drops details, errors compound
Long-termFull history, prior sessionsVector retrievalStale or irrelevant pulls
Structured statePreferences, decisions, goalExplicit store, deliberate writesDrift if not invalidated

Operational details: decide what is worth remembering (not every turn matters), avoid stale or contradictory memories (update and invalidate), pin the original goal so a long task does not drift, and watch the token budget so memory does not crowd out the actual task. For tool-using agents, persist intermediate results and checkpoints so a long task is resumable.

The defensible framing: bound context with a recent buffer plus summary plus on-demand retrieval, and persist durable facts as structured state, rather than relying on an ever-growing prompt.

Key takeaways.

  • Finite context is the constraint; the design question is what to keep, not how to fit everything.
  • Four tiers: verbatim recent buffer, rolling summary, retrieved long-term store, structured durable state.
  • Precise facts belong in structured state, not in lossy recursive summaries.
  • Pin the goal and invalidate stale memories, or the agent drifts and acts on outdated info.

What interviewers probe next.

  • "Why not just use a huge context window?" Cost per token every turn, latency, and lost-in-the-middle; long-running agents also exceed even large windows eventually. Retrieval plus summary is cheaper and more reliable.
  • "Summarization risks?" Lossy: it can drop details that matter later, and errors compound across recursive summaries; keep critical facts in structured state and retrieve originals when precision is needed.
  • "How do you decide what to store long-term?" Salience: durable facts, decisions, user preferences, and task-relevant context; not every chit-chat turn.
  • "Avoiding goal drift in a long task?" Pin the original objective in context and periodically re-check actions against it.

Common mistakes.

  • Concatenating the entire history every turn, hitting cost, latency, and lost-in-the-middle.
  • Relying on summarization for precise facts that should be structured state.
  • Never invalidating stale or contradictory memories, so the agent acts on outdated info.
  • Letting memory crowd out the task, or losing the original goal over a long session.
HOW DID IT GO?
0
UP NEXT ON YOUR JOURNEY
DISCUSSION · 0

No comments yet — be the first to share your approach.