Conversation memory that grows forever

L2 · PractitionerDesign~10 minMemory, context & RAG

Your assistant keeps the full chat history in every request. Long-time users now have conversations of 500 turns, requests are slow and expensive, and some fail with context-length errors. Design the conversation memory.

Answer it out loud or on paper first. Then open the short answer.

Short answer

Split memory by how it's used. Keep the last N turns verbatim (a token budget, not a message count). Replace older turns with a running summary that preserves decisions, facts about the user and open threads. Store everything in a database and retrieve older turns on demand when the current message refers back. Pin the system prompt and any standing user instructions so they never fall out. Set a token budget for each part and test that answers referring to old turns still work.

Full answer

Budgeted context

PartBudget (example)Contents
System + toolsFixed, cachedInstructions, tool definitions
Pinned facts~500 tokensName, preferences, standing instructions
Summary of older turns~1,000 tokensDecisions, facts, open questions
Retrieved older turns~1,500 tokensOnly if the new message refers back
Recent turns~4,000 tokensVerbatim

Strategies compared

StrategyProCon
Sliding windowSimple, cheapForgets early context abruptly
Running summaryKeeps gistLossy; summaries drift; extra calls
Retrieval over historyPrecise recall of specificsNeeds good queries; misses implicit references
CombinationBest qualityMore moving parts

Summary quality

Summarise with a schema (facts, decisions, preferences, open items), not free prose. Update incrementally: old summary + newly evicted turns → new summary. Keep the raw history so you can rebuild summaries when the prompt improves.

Test it

Eval conversations where the answer depends on turn 3 when you're at turn 300. Check pinned instructions survive.

Follow-ups

  1. L2. Where do summaries go wrong?

    Expected answer

    They drop specifics (numbers, names), merge contradictory facts, or preserve injected instructions as if they were the user's. Use structure, keep sources, and re-summarise from raw history when needed.

  2. L3. When is cross-session memory a different problem?

    Expected answer

    Long-term memory about the user needs write policies, user control and deletion, not just compression (see the next question).

  3. L3. How does prompt caching interact?

    Expected answer

    Keep the stable parts first and unchanged so they cache; rewriting the summary each turn invalidates the cache after it, so update it in chunks.

Wrong answers interviewers hear
  • "Use a 1M-token model and keep everything." Slower and costlier every turn, and recall of specifics in very long contexts degrades.
  • "Keep the last 20 messages." Message counts ignore size; one long message can blow the budget, and early instructions vanish.
  • "Summarise everything into one paragraph each turn." Lossy, drifts over time, and costs an extra call per turn.

Taught in

Improve this answer