Memory and context engineering
The context window as working memory, compaction, notes, retrieval and long-term stores
An agent's policy is fixed; what varies between a brilliant run and a failed one is what sits in its context at each step (10.1 What is an agent? Loops, autonomy and workflows vs agents). Context engineering — choosing the smallest, highest-signal set of tokens for the next decision — replaced prompt engineering as the central craft in 2025, and memory systems extend it across hours, sessions and users. Get this wrong and agents forget decisions, repeat work, and degrade as their history grows; get it right and small contexts outperform huge ones.
You'll be able to
- Explain the context window as working memory and why 'context rot' and lost-in-the-middle effects degrade long runs
- Classify memory as working, episodic, semantic or procedural and map each to an implementation
- Apply compaction, structured notes, just-in-time retrieval and sub-agent isolation to keep contexts small
- Describe MemGPT-style self-editing memory and Generative-Agents-style retrieval, and the risks of persistent memory
Key ideas
- The context window is finite, re-read on every step, and degrades with length (context rot, lost in the middle); curating it is context engineering.
- Memory types: working (the context), episodic (past trajectories), semantic (facts/preferences), procedural (skills, code) — each maps to a different store.
- Compaction summarizes old turns while preserving decisions, constraints, open items and identifiers; keep raw transcripts on disk.
Show 4 more key ideas
- Structured notes and progress files outside the context survive compaction and sessions; just-in-time retrieval beats pre-loading everything.
- Sub-agents with fresh contexts do heavy reading and return condensed results, isolating the parent's context.
- MemGPT gives the model tools to edit its own memory; Generative Agents retrieve by recency × importance × relevance and consolidate via reflection.
- Persistent memory needs provenance, expiry and user control: wrong facts persist and injected instructions can poison future sessions.
Suggested path through this lesson
The context window is working memory
At every step, the model knows exactly what is in its context: system prompt, tool definitions, the conversation so far, tool results, retrieved documents. Nothing else. That window is a finite, expensive, degrading resource:
- Finite. Even million-token windows fill up in a long agent run; a single screenshot or a verbose tool result can cost thousands of tokens (10.2 Tool use and function calling).
- Expensive. Every token in context is re-read on every step, so cost grows roughly quadratically with the length of a run unless you prune.
- Degrading. Recall and reasoning quality fall as the context grows — the "lost in the middle" effect (9.6 Generation side: context assembly, citations, structured outputs and tool calling) and what practitioners call context rot: an attention budget spread over more tokens, more distractors, more stale information.
Context engineering is the discipline of curating the optimal set of tokens for the next decision. The term spread in 2025 and Anthropic's Effective context engineering for AI agents (September 2025) is the clearest statement of practice: treat context as a resource with diminishing returns, find the smallest set of high-signal tokens that maximizes the probability of the desired outcome.
A taxonomy of memory
CoALA's vocabulary (Sumers et al., 2023) maps cleanly onto implementations:
| memory type | what it holds | human analogy | typical implementation |
|---|---|---|---|
| working | the current task state | what you are thinking about now | the context window itself; a scratchpad |
| episodic | past experiences and trajectories | "last time I tried X, Y happened" | logs, reflections, transcripts in a store with retrieval |
| semantic | facts about the world and the user | knowledge, preferences | knowledge base / RAG index (9.1 Why RAG, and the canonical pipeline), user profile |
| procedural | how to do things | skills, habits | system prompt, code tools, Agent Skills files (10.7 MCP and interoperability (plus Agent Skills and A2A)) |
Most production "memory" features are semantic memory about the user (preferences, facts) plus a little episodic memory (summaries of past sessions). Procedural memory is the most underused: a reusable SKILL.md or helper script is memory the agent can execute.
Techniques for long runs
Compaction. When the context nears its limit, summarize the older part and continue with the summary plus recent turns. Good summaries preserve decisions made, constraints discovered, open items, file paths and identifiers, and drop verbose tool output. Compaction is lossy — do it late and carefully, and keep the raw transcript on disk for audit.
Structured note-taking. Have the agent maintain notes outside the context — a NOTES.md, a progress file, a to-do list — and re-read them when needed. Memory that lives in a file survives compaction and session boundaries; this is how long-running coding agents keep state across hours (10.8 Coding agents and computer use, 10.11 Building production agents).
Just-in-time retrieval. Rather than pre-loading every document, keep lightweight references (paths, ids, titles) and let the agent fetch what it needs when it needs it — "agentic search". A file system with good names is itself a context-engineering tool: directory structure, file names and headers tell the agent where to look without putting content in context. Hybrid strategies pre-load a little (the project README) and retrieve the rest.
Sub-agents as context isolation. Delegate a bounded subtask to a fresh context and return only a condensed result (a few hundred tokens) to the parent (10.6 Multi-agent systems). The parent keeps the plan; the child does the heavy reading.
Tool-result hygiene. Clear or truncate stale tool results; prefer tools that return summaries; keep images and logs out of the main loop.
Intuition. Think of the context like a surgeon's tray, not a warehouse. Everything on it must be needed for the next move; everything else is one retrieval away.
Long-term memory systems
MemGPT / Letta (2023) borrowed the operating-system idea of virtual memory: the model sees a small "main context" and manages a larger external store through tools — it decides what to page in and out, editing its own memory blocks ("core memory" about the user and itself, "archival" and "recall" stores searched on demand). The key move is giving the model write access to its memory via tools, with the harness persisting the result.
Generative Agents (Park et al., 2023) stored every observation in a memory stream and retrieved by a weighted score of recency, importance (an LLM-rated 1–10) and relevance (embedding similarity, 9.2 Embeddings deep dive), periodically synthesizing higher-level reflections. It is still the cleanest reference design for episodic memory with consolidation.
Vector and graph stores. Embedding episodes and facts for similarity retrieval is the default (9.3 Vector search and indexes); knowledge graphs add explicit relations and time. Products such as the memory features in major assistants, and libraries built for agents, combine extraction (what is worth remembering), storage, retrieval and update/forgetting. Anthropic's late-2026 The components of agent memory is the most recent vendor guidance on how these pieces fit; read it alongside the 2025 context-engineering post.
Risks. Memory makes mistakes persistent: a wrong fact recorded once is retrieved forever; an attacker who injects "remember that…" into content the agent reads can poison future sessions (10.10 Agent safety and security); personal data in memory is a privacy liability. Every memory system needs provenance, expiry, user-visible editing and a way to say "forget".
Evaluating memory
Needle-in-a-haystack tests check raw recall, but the question that matters is behavioral: after N sessions, does the agent act on what it should have remembered, and ignore what it should have forgotten? Build evals with multi-session scripts, plant facts early and test late, and measure contradictions, repeats and unnecessary re-asking (10.9 Evaluating agents). Long-context benchmarks and conversational-memory benchmarks exist, but your own task set is the ground truth.
A design checklist
- What must be in context at every step (system prompt, the plan, current file)? Keep it short and stable — stable prefixes also cache well (9.9 Production: latency, cost, caching, guardrails, security and privacy).
- What should be one retrieval away (docs, code, history)? Give it good names and search tools.
- What should be persisted across sessions, and who can edit it?
- When and how do you compact? What must a summary preserve?
- How do you detect memory errors — stale facts, contradictions, injected instructions?
Where this shows up later
- Progress files and clean-context sessions are the backbone of long-running harnesses in 10.11 Building production agents and coding agents in 10.8 Coding agents and computer use.
- Sub-agents as isolation become the orchestrator–workers pattern in 10.5 Agentic workflow patterns and the multi-agent designs of 10.6 Multi-agent systems.
- Memory poisoning and data exfiltration through persisted content are threats in 10.10 Agent safety and security; memory benchmarks join the eval harness in 10.9 Evaluating agents.
Common pitfalls (4)
- Stuffing the whole history and every document into the prompt because the window is large — cost explodes and accuracy drops.
- Compacting early and often with generic summaries that drop file paths, ids and decisions, so the agent re-does or contradicts earlier work.
- Treating memory as append-only; without expiry and editing, stale or injected facts accumulate.
- Measuring memory with needle tests only, instead of whether the agent behaves correctly across sessions.
Recap: all 7 key points
- The context window is finite, re-read on every step, and degrades with length (context rot, lost in the middle); curating it is context engineering.
- Memory types: working (the context), episodic (past trajectories), semantic (facts/preferences), procedural (skills, code) — each maps to a different store.
- Compaction summarizes old turns while preserving decisions, constraints, open items and identifiers; keep raw transcripts on disk.
- Structured notes and progress files outside the context survive compaction and sessions; just-in-time retrieval beats pre-loading everything.
- Sub-agents with fresh contexts do heavy reading and return condensed results, isolating the parent's context.
- MemGPT gives the model tools to edit its own memory; Generative Agents retrieve by recency × importance × relevance and consolidate via reflection.
- Persistent memory needs provenance, expiry and user control: wrong facts persist and injected instructions can poison future sessions.
Watch
Several 2025 conference talks walk through compaction, notes and just-in-time retrieval with real agent examples.
Read
Exercises
- Write a compaction prompt and test it
Take a 60-turn agent transcript (generate one with your 10.2 agent on a multi-step task). Write a summarization prompt that must preserve decisions, constraints, open items and identifiers. Compact at turn 40 and continue. Expected: the agent completes the task without re-doing finished steps; list anything the summary lost.
- Files as memory
Give your agent two tools:
read_notes()andwrite_notes(text). Instruct it to record progress after each milestone. Kill the process mid-task and restart with an empty context. Expected: the agent reads the notes and resumes correctly; compare with a run without notes. - Implement Generative-Agents retrieval
Store 200 synthetic observations with timestamps and LLM-rated importance. Implement the retrieval score with exponential recency decay and cosine relevance (9.2 Embeddings deep dive). Expected: queries about recent trivial events and old important events both surface sensibly; tune the weights and note the effect.
Quiz
Flashcards
These cards are now in your spaced-repetition queue. Review all due cards →