How do agents maintain memory across steps and sessions?
114 views
83 of 83
1
The problem
Question presented to candidate:
"An agent forgets everything between model calls, yet users expect it to remember what it did five steps ago, survive a crash halfway through a long task, and recall next month that they are vegetarian. How do agents actually maintain memory across steps and across sessions, and what goes wrong?"
What a strong answer should cover:
The model is stateless: all memory is something the application stores and puts back into the context; the layers differ in lifetime and in who writes them
Within a task: the transcript (working memory) plus an external scratchpad or checkpoint of plan, progress and key findings, so compaction, a crash or a restart resumes instead of starting over
Across sessions: a long-term store scoped per user or tenant, written selectively (durable, reusable facts only), updated rather than appended when facts change, and retrieved by relevance into a bounded, labelled block
The hard problems: what to store, contradictions and staleness (timestamps, history, confirmation), privacy and deletion, and memory poisoning when untrusted content gets written and replayed as if it were trusted
Real mechanisms to name: framework checkpointers keyed by thread id and long-term stores keyed by namespace (LangGraph), file-based memory tools (Anthropic memory tool), the MemGPT idea of paging between context and external storage
How to tell it is working: evals that check the right memory was recalled, nothing stale or cross-user leaked, and that memory improves the task rather than cluttering it
Clarifying questions expected:
Is memory needed within one long task, across a user's sessions, or shared across users (team knowledge)?
What are the privacy rules: can users see and delete what is remembered, and how long may it be kept?
Code / implementation expected: Optional โ a scoped memory store with upsert and retrieval, or a checkpoint-and-resume loop.
memory
agents
state
long-term-memory
2
Try it yourself
Sketch your approach before you read the solution. That is what the interview tests.
Stuck? Get a hint.
A short nudge toward the approach. It will not give the answer away.
3
Solution
Target Audience: Engineers building agents that run for many steps or serve returning users, and anyone asked in an interview where an agent's "memory" actually lives.
Difficulty: Hard
Verified with: Node 22.22.0 and Python 3.12.3 (both examples below, identical output). Framework and product details were checked in October 2026 against the LangGraph persistence docs (docs.langchain.com), Anthropic's context management announcement, and the MemGPT paper (arXiv 2310.08560).
How to read this doc: Every idea is explained in plain language first, then tagged with ๐ Interview term: so you also learn the words an interviewer expects. All program output shown was produced by actually running the examples; the Verified section quotes it in full.
1. Why This Even Matters โ A Story First
Picture a brilliant consultant with one odd condition: every time they leave the