Lesson 04.09 · Phase 04 · Memory & Context Engineering
Context compaction
When a long run stops fitting, shrink it without losing the facts it still needs. Clearing is free and forgets; a handoff note costs a call and keeps only what someone wrote down.
Learning objectives
- Implement two compaction strategies for agent runs: clearing old tool results, and replacing the middle of the history with a handoff note.
- Keep a compacted history valid: no tool results without their calls, the task pinned, recent steps verbatim.
- Measure size, validity and recall of needed facts for each strategy, and explain why "it fits" is not success.
- Decide when to compact automatically, and in what order.
Lessons 2 and 3 dealt with chat: short user turns and short replies. Agent runs are different. A debugging agent reads a config file (40 lines), runs a job (60 lines of log), reads ten source files (45 lines each), greps five packages. After 20 steps the history is about 14,600 tokens, most of it file contents the agent looked at once.
Loading the full lesson…