Phase 10 · Agentic AI/
Lesson 10.4advanced3 h

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

  1. 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.
  2. Memory types: working (the context), episodic (past trajectories), semantic (facts/preferences), procedural (skills, code) — each maps to a different store.
  3. Compaction summarizes old turns while preserving decisions, constraints, open items and identifiers; keep raw transcripts on disk.
Show 4 more key ideas
  1. Structured notes and progress files outside the context survive compaction and sessions; just-in-time retrieval beats pre-loading everything.
  2. Sub-agents with fresh contexts do heavy reading and return condensed results, isolating the parent's context.
  3. MemGPT gives the model tools to edit its own memory; Generative Agents retrieve by recency × importance × relevance and consolidate via reflection.
  4. Persistent memory needs provenance, expiry and user control: wrong facts persist and injected instructions can poison future sessions.

Suggested path through this lesson