Context and Memory Engineering for AI Agents
Assemble bounded model requests, compare context policies and maintain scoped memory with explicit provenance, retention and deletion rules.
- Prerequisites
- Python, JSON, the AI Fundamentals context-window lesson and the context budget manager (Trim history to a token budget in the LangGraph course). Token-counting labs require the pinned tokenizer and dependencies named in their READMEs; offline replay does not require model weights.
- Duration
- about 5 hours of reading
- Cost
- Free, no sign-up
What you can build now
The ten lessons progress from assembling one model request to maintaining and evaluating scoped memory. Each has a complete local lab. Token-counting exercises use a pinned tokenizer; answer checks replay saved model responses. A combined application capstone remains to be integrated.
What you'll learn
- Assemble, compact, isolate and compare model context.
- Store and retrieve memory with explicit user and task scope.
- Consolidate conflicts, retain provenance and propagate deletion.
- Evaluate retained facts and memory usefulness separately from token savings.
Lessons
Module 1 · 4 lessonsAssemble and compare model context
Build a context assembler for instructions, history, tools, and retrieved passagesRead
Assemble one model request from instructions, tool definitions, conversation history, tool results and retrieved passages. Keep required sections whole, fill the rest in a tested order, and record what was dropped.
Build context compaction with information-loss testsRead
Replace removed conversation turns with cited notes, then test which named facts survive. The probes report lost and stale facts instead of assuming that a shorter context still holds everything.
Build isolated contexts for users, tasks, and agentsRead
Scope every context item to an app, user, task or agent, take scope keys from the requester, and hand sub-agents only what they need. Tests show that a naive shared list leaks another user's item and the scoped store does not.
Build a context-selection benchmarkRead
Run four history-selection strategies on the same twelve cases, budget and grader. Record which required facts reach the request, which replaced facts slip in, and why each strategy fails where it does.
Module 2 · 4 lessonsStore, retrieve and maintain memory
Build a memory service with explicit user and task scopeRead
Store an assistant's memories in SQLite so they survive a restart, scope every read by user and task, and test that one user's or one task's memories never reach another.
Build memory provenance and deletionRead
Extend the SQLite memory service so every memory keeps its source, a correction supersedes the old fact without erasing its history, short-lived memories expire, and a forgotten memory is deleted from the file with only a text-free record left.
Build working, semantic, and episodic memory retrievalRead
Store an assistant's memories as working notes, facts and past events, and retrieve each kind with its own rule and token quota. A single pooled ranking misses the task state that the question never mentions; per-kind retrieval keeps it.
Build memory consolidation with conflict testsRead
Merge duplicate memories and resolve conflicting facts with explicit, tested rules. A user's own statement beats a model's inference, a newer statement beats an older one, and retired facts stay stored with a record instead of disappearing.
Module 3 · 2 lessonsEvaluate memory compaction and usefulness
Build a 50-turn memory-compaction benchmark with fact-retention checksRead
Replay one 50-turn conversation in which facts are stated, changed and forgotten, and check at five points which current facts reach the request and which stale or forgotten facts leak in, for four context strategies under one token budget.
Build a memory usefulness evalRead
Measure what a memory system sends with each request. Recall says whether needed facts arrive; usefulness says how much of the memory block was needed; stale and forgotten lines say what must never arrive; row counts show how storage grows.