aiml

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.

What are context and memory?

Context is the information assembled for one model request. Memory is retained information that may be selected for later requests. Selection, compaction, scope and deletion answer different questions: storing a fact does not mean the next request needs it.

Follow instructions, conversation turns and retrieved passages into one bounded request. Compare retained, lost and stale facts after compaction, then store scoped memories in SQLite and inspect corrections and deletion. The benchmarks measure their supplied cases rather than assuming that more stored text improves answers.

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 lessons · about 2 hoursAssemble and compare model context

Select instructions, history and passages for one request, then compare compaction and isolation choices.

  1. Assemble a model request within a context budgetRead

    Assemble instructions, tools, history and retrieved passages into one model request. Keep required sections whole and record optional content that does not fit.

    30 min readLabQuiz

  2. Build context compaction with information-loss testsRead

    Replace removed conversation turns with cited notes. Test retained, lost and stale facts instead of assuming a shorter context preserves every fact.

    28 min readLabQuiz

  3. Build isolated contexts for users, tasks, and agentsRead

    Scope context to the requesting app, user, task or agent. Compare a leaking shared list with a scoped store and test restricted worker handoffs.

    26 min readLabQuiz

  4. Build a context-selection benchmarkRead

    Compare four history-selection strategies on shared cases, budgets and graders. Inspect retained facts, stale facts and strategy-specific failures.

    27 min readLabQuiz

Module 2 · 4 lessons · about 2 hoursStore, retrieve and maintain memory

Store scoped facts with sources, retrieve them and handle corrections, conflicts and deletion.

  1. Build a memory service with explicit user and task scopeRead

    Store assistant memory in SQLite across restarts. Scope each read by user and task, then test that another scope cannot retrieve the stored facts.

    26 min readLabQuiz

  2. Build memory provenance and deletionRead

    Track memory sources, corrections and expiry in SQLite. Delete forgotten content while retaining a text-free record, and test scope isolation.

    30 min readLabQuiz

  3. Build working, semantic, and episodic memory retrievalRead

    Retrieve working notes, facts and past events with separate rules and token quotas. Compare this with a pooled ranking on the same memory tasks.

    28 min readLabQuiz

  4. Build memory consolidation with conflict testsRead

    Merge duplicate memories and resolve conflicts using source and recency rules. Retain superseded facts with records of the consolidation decision.

    28 min readLabQuiz

Module 3 · 2 lessons · about 56 minEvaluate memory compaction and usefulness

Compare which facts survive compaction and whether retrieved memories help the supplied tasks.

  1. Compare memory compaction with fact-retention checksRead

    Replay a conversation with changed and forgotten facts. Compare four context strategies under one budget and inspect current, stale and deleted facts.

    30 min readLabQuiz

  2. Build a memory usefulness evalRead

    Measure needed facts, unnecessary memory, stale lines and forgotten content sent with requests. Compare usefulness with recall and storage growth.

    26 min readLabQuiz