Agent Memory and Context Management
Keep long agent runs focused by managing what goes into the context window and what lives outside it
A taste of a lesson
After compaction my agent ignores a constraint the user gave at the start: 'never email the customer directly'. How do I fix that?
Your summary is treating that rule as history rather than as a standing instruction. Two fixes. First, separate constraints from narrative: keep a pinned block of user rules that is never summarised, either in the system prompt or as a structured note re-added every step. Second, add a check to your compaction test set: after summarising, ask the agent what it is not allowed to do, and fail the test if the rule is missing. Also enforce it in code, since a permission check beats a remembered rule. Which other instructions in your agent deserve a pinned block?
Written by the teacher as an example. In your lesson the tutor answers your own questions, and like any AI it can be wrong.
What you will be able to do
- Explain how context length affects cost, latency and answer quality
- Apply trimming, compaction, external notes, retrieval and sub contexts deliberately
- Design running summaries that preserve goals, decisions and constraints
- Plan long term memory with consent, visibility, deletion and provenance
- Inspect traces to find what is wasting context in a real agent
Lesson plan
- 1 Context as working memory Understand what fills the context in a loop and how it affects cost and quality. Start
- 2 Trimming and compaction Shrink history without losing what the agent still needs. Start
- 3 External notes and scratch files Let the agent keep durable notes outside the context window. Start
- 4 Retrieval and sub contexts Fetch only what is relevant and push focused work into fresh contexts. Start
- 5 Long term memory across sessions Design memory that helps users without surprising them or storing what it should not. Start
- 6 Diagnosing context problems in traces Find and fix context waste and lost information in a real agent run. Start
Try asking
About this tutor
For engineers building agents that run for many steps, return to users over days, or juggle large documents. Context is the agent's working memory and it fills up fast with tool results, old turns and instructions. You will learn how context windows behave in practice, why performance can degrade before the limit, and the main techniques for keeping context useful: trimming and summarising, external notes and scratch files, retrieval of past information, and handing focused subtasks to fresh contexts. We also cover long term memory across sessions and its privacy questions.
Reviews
Students can review a tutor after a paid lesson. Nobody has yet.
About the teacher
I teach how AI agents are built: the loop, the tools, the memory, and when a plain workflow is the better choice
9 tutors 310 lessons taught Sample
I build and teach the inner workings of AI agents. Most of my working life has been spent on backend systems, so I approach agents the way I approach any distributed system: what runs, in what order, what can fail, and what it costs. I like to start every topic with a drawing of the loop on a whiteboard and...
See Hiroshi's profile and tutorsMore like this
Other tutors on the same or nearby topics.