Foundations to Frontier
Tests
F. NLP and Large Language Models › F11 Agents and Tool Use

Agent memory

A language model remembers only what is in its context window, so agents that run for hours or talk to a user over weeks need memory outside it. The options are to keep a window of recent turns, to keep a running summary, to retrieve relevant past turns, or to write facts into a structured store. Each forgets in its own way: windows forget abruptly, summaries gradually, retrieval misses what is worded differently, and stores depend on what was extracted. This page covers short-term and long-term memory, memory writing, retrieval scoring and updates. In the exercises you write a token-budget window, a newest-wins key-value store and a generative-agents recall score. You then simulate a 400-turn conversation with 80 facts, 30 of them later updated. A 50-turn window answered 13% of questions and a capped summary 50%. Retrieval answered 70%, and 85% when memories carried timestamps, but updates phrased as 'it' were often missed. A key-value store with a 90%-accurate extractor answered 94% with 10 tokens of context.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

A notebook for a forgetful assistant

Imagine an assistant who can only remember the last page of your conversation. To help over weeks they need a notebook: they could keep a short diary summary, file every page and search it later, or keep a tidy table of key facts and update it when something changes. Each has trade-offs: summaries lose details, searches miss notes written in other words, tables are only as good as what was written in them. The demo compares how each recalls a fact from long ago.

Agent lab
Recall of a fact from 30 turns ago: window no, running summary 40%, retrieval memory 85%. Windows forget abruptly, summaries gradually; retrieval keeps old facts at a fixed recall.
Why engineers care

Memory turns a chatbot into a long-running assistant: remembering a user’s preferences, a project’s decisions or a robot’s map of where things are.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Without external memory, a model remembers…
  2. 2. A running summary forgets…
  3. 3. A structured store keeps…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Short-term (working) memory: the conversation in the context window; trimmed by token budget, keeping the system prompt and recent turns.
  • Summaries: periodically compress old turns into a summary; cheap but lossy, and errors compound over rewrites.
  • Episodic retrieval memory: store past turns or events with embeddings and timestamps; retrieve by relevance, recency and importance (generative agents).
  • Semantic / structured memory: extract facts into key-value records, profiles or knowledge graphs; updates overwrite; supports exact lookup.
  • Writing memory: decide what to store (importance), rewrite to stand alone (resolve ‘it’), deduplicate, and handle contradictions.
  • Systems: MemGPT / Letta (memory paging), Mem0, LangGraph stores, ChatGPT and Claude memory features.
  • Risks: privacy (what is stored, for how long, deletion), stale or poisoned memories, memory leaking across users.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Generative agents score memories by…
  2. 2. Before storing a turn, rewrite it to…
  3. 3. A memory risk:

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Implementing memory

Exercise 1.

  • Trim a conversation to a token budget.
  • Newest-wins key-value memory.
  • A recall score combining relevance, recency and importance.

Practice.

  • Count tokens with the model’s tokenizer; reserve room for the answer.
  • Store memories with timestamps, source and user id; filter by user before retrieval.
  • Extract facts with an LLM into a schema (entity, attribute, value, time); resolve coreference when writing.
  • Let users see and delete what is remembered.
  • Evaluate memory with questions about old facts, updated facts and facts never stated (should answer ‘unknown’).
from mem0 import Memory                       # or a simple vector store plus a facts table
m = Memory()
m.add("I moved the test rig to hangar 3.", user_id="ana")
hits = m.search("Where is the test rig?", user_id="ana", limit=3)
context = "\n".join(h["memory"] for h in hits["results"])
🐍 Exercise 1: windows, stores and recall scores
Write a token-budget window, a newest-wins key-value memory and a memory recall score.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Store with each memory…
  2. 2. Users should be able to…
  3. 3. Evaluate memory with…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Cost of full history. Resending the whole conversation each turn costs t·n(n + 1)/2 tokens over n turns of t tokens: quadratic. Any memory scheme bounds the per-turn context.

How each scheme forgets. A window of W turns recalls a fact if and only if its latest statement is within W turns. A summary rewritten R times keeps a detail with probability about keep^R and also evicts under a size cap. Retrieval recalls a fact if its latest statement ranks in the top k for the query; a statement that does not contain the query’s words (pronouns, paraphrases) is missed, and without timestamps the reader cannot tell an old value from a new one.

Writing versus reading. Structured stores do the hard work at write time: extract, resolve references, overwrite old values. Their accuracy is the extractor’s accuracy; reads are cheap and exact. Retrieval does the work at read time and keeps everything, including contradictions.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Full-history total cost grows…
  2. 2. Retrieval misses statements that…
  3. 3. Structured stores do the work at…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How well do windows, summaries, retrieval and structured stores recall old and updated facts in a long conversation?

History.

  • J. S. Park et al., “Generative agents: interactive simulacra of human behavior” (UIST 2023): memory stream with relevance, recency and importance.
  • C. Packer et al., “MemGPT: towards LLMs as operating systems” (2023).
  • A. Maharana et al., “Evaluating very long-term conversational memory of LLM agents” (LoCoMo, 2024).

Exercise 2. 400 turns of chit-chat containing 80 facts ‘entity attribute-type value’ (10 attribute types shared by all entities); each entity is also mentioned in 3 other turns.

  • Updates: 30 facts are later updated; 40% of updates say ‘it’ rather than the entity’s name.
  • Summary: rewritten every 10 turns, keeps each new fact with probability 0.9, holds at most 40 facts.
  • Retrieval: BM25 over turns with the query entity + attribute type, top 5; reader takes the newest hit (with timestamps) or a random hit (without).
  • Key-value store: an extractor (resolving ‘it’) writes each statement with probability 0.9; updates overwrite.
memory all facts updated facts facts older than 50 turns context tokens
window of 50 turns 0.125 0.200 0.000 600
running summary 0.500 0.700 0.443 400
retrieval, no timestamps 0.700 0.200 0.729 60
retrieval + timestamps 0.850 0.600 0.857 60
key-value store 0.938 0.967 0.929 10
  • Windows answered only facts stated recently.
  • The summary favoured recent facts and evicted old ones under its cap.
  • Retrieval recalled old facts well but returned stale values for updates it missed; timestamps fixed the cases where both values were retrieved.
  • The store handled updates because the extractor resolved ‘it’ at write time; its errors are the extractor’s misses.
  • Seeds 1 and 2: store 0.825 and 0.887; retrieval + timestamps 0.825 and 0.912; the store’s lead is not stable at 80 facts.
  • Caveat: the extractor and summary rates are assumptions; real extractors also invent or mis-attribute facts.
🐍 Exercise 2 (research): how well do windows, summaries, retrieval and structured stores recall old and updated facts in a long conversation?
Simulate a long conversation with updated facts and compare window, summary, retrieval and key-value memory.

Memory in AI, robotics and aerospace

  • Robots: spatial memory of where objects were last seen, updated as they move, is a key-value store keyed by object.
  • Operations assistants: remembering decisions and open issues across shifts; timestamps and sources are essential.
  • Personal assistants: user preferences and constraints, with visible and deletable memory for privacy.

References

  1. J. S. Park et al. (2023). “Generative agents: interactive simulacra of human behavior.” UIST.
  2. C. Packer et al. (2023). “MemGPT: towards LLMs as operating systems.” arXiv:2310.08560.
  3. A. Maharana et al. (2024). “Evaluating very long-term conversational memory of LLM agents.” ACL.
  4. W. Zhong et al. (2024). “MemoryBank: enhancing large language models with long-term memory.” AAAI.