Skip to content
Tech Interview Prep
Technical interview guide

Agent Memory & Context Management

Giving agents state across turns and sessions despite a fixed, finite context window.

Minutes to read
3
Practice MCQs
25
Interview Q&A
25
Edition
v5
Editorial status
Reviewed

Scope: MemGPT, Generative Agents, Reflexion, RAG, Lost in the Middle, Sentence-BERT, HNSW, Model Cards, NIST Privacy Framework and GenAI Profile, and OWASP Prompt Injection/Sensitive Disclosure references reviewed 2026-09-04.

Overview

Curated: · Written: · Reviewed:

Agent Memory & Context Management

In one line

Agent memory is an external, governed retrieval corpus that the agent writes into and reads from under a fixed token budget. Reach for it when state must survive a context window or a session, and the application, not the model, decides what gets stored.

Key definitions

  • Working memory: what sits in the current window (objective, plan, recent turns, validated results). It disappears when the window or workflow ends unless it was persisted.
  • Episodic memory: dated past events with provenance, such as "on 2025-01-12 user cancelled plan X".
  • Semantic memory: extracted facts and preferences stored as structured records, with an embedding index built over them.
  • Procedural memory: approved, reusable tool sequences.
  • Supersession: a new fact marks the old row with superseded_by and valid_from. The old row is kept, not overwritten.

Formulas and complexity

headroom=W−(system+tools+reserve+working)headroom = W - (system + tools + reserve + working)

Quote headroom, not W, as the space that tool results and retrieved memory compete for. Retrieved items must fit inside it, and the lowest-priority items are evicted first.

How it works

  • Read path: derive scope from the authenticated identity, filter (tenant, subject, ACL, purpose, type, time, deletion), rank, then cap top-k and tokens.
  • Write path: candidates pass ordered gates (sensitivity, provenance, purpose, stability across sessions) before storage. First sightings go to a durable pending store, not a container-local file.
  • Under pressure, eviction follows a written priority: current requirements and safety constraints outrank old conversation.
User turn Scope from auth Filter: tenant, ACL, time, deletion Rank, cap top-k, assemble prompt Model and tools Extract candidates Gates: sensitivity, provenance, purpose, stability Governed store

When to use and when not to

SituationUse memory?Better alternative
Per-user preference that changesYes, supersede rather than appendNone: fine-tuning cannot update or delete one user's data
Shared, current documentsNoRetrieval over the documents
Single-session taskNoWorking memory only
"What happened last time?"Yes, episodic records with session IDs and outcomesA semantic summary loses which session it came from
Sensitive domain where persistence is a liabilityOnly with strict governance and confirmationAuthoritative system of record lookup

Traps interviewers probe

  1. Keying memory to a per-visit session ID. Fix: key memory to the stable subject or tenant identity. A session ID scopes one conversation, and a TTL on it deletes the memory with it.
  2. Letting similarity grant rights. Fix: similarity only ranks eligible records. The server checks each action against authoritative state using the authenticated scope.
  3. Filtering tenants after top-k. Fix: apply the tenant filter inside the search, before ranking. Otherwise another tenant's record displaces a permitted one.
  4. Appending every fact. Fix: use entity-level supersession with valid_from and superseded_by. Retrieval excludes superseded rows, and an as-of query walks the validity intervals.
  5. Treating decay as deletion. Fix: decay only lowers retrieval eligibility. Explicit requests, legal hold and export ignore it. Deletion tombstones the record and removes it from indexes, caches and summaries.

Interview tips

Open with: "Memory is a governed retrieval corpus. I would design the write path first, then scope-first retrieval under the token budget, then deletion and an evaluation against a no-memory baseline."

Expect these follow-ups:

  • How do you decide what to write? Name the gates and their order.
  • What happens when a fact changes? Supersede it and exclude stale rows.
  • How does deletion reach embeddings and summaries? Tombstone first, then rebuild indexes, invalidate caches and regenerate derived artifacts from source turns.
  • Why not fine-tune? Per-user mutability and deletion rule it out.
  • How do you know memory helps? Compare against a no-memory arm, stratify by subgroup, and check the result against the noise band.

30-second outline:

  1. Memory is external state, and the model only sees what gets retrieved.
  2. Write gates: sensitivity, provenance, purpose, and stability across sessions.
  3. Read path: scope from auth, filter inside search, rank, cap tokens.
  4. Changes supersede old rows with valid_from and superseded_by.
  5. Deletion tombstones serving state, then rebuilds indexes, caches and summaries.
  6. Evaluate against a no-memory baseline before claiming it works.

Feedback on this overview

Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.