Start studying
Module 11 · AI Agents Module contents
Module 11  ·  Card 11 / 17intermediate

Context Compaction

A long agent run fills its context with tool output, most of which stops mattering quickly.

Compaction replaces the older part of the history with a summary and keeps the recent turns intact.

What must survive is the goal, the decisions already taken, and anything that would be repeated otherwise.

Compact too aggressively and the agent forgets a constraint and cheerfully redoes finished work.

Writing durable facts to a file or a memory store is safer than trusting a summary to keep them.

Learn in depth ↓
Module 11 · AI Agents · 17 concepts
In depth  /  15 lines

Context Compaction, in more detail

An agent's context is a transcript, and every tool call appends its full result to it.

A directory listing, a file read or a search result can each be thousands of tokens.

After twenty steps the window is mostly output that stopped being relevant fifteen steps ago.

Two things go wrong at once as it fills up.

Cost and latency rise on every subsequent call, because the whole transcript is reprocessed.

And relevant details get lost in the middle, where attention is weakest.

Compaction is the maintenance step that keeps the run going without simply truncating.

The oldest portion of the history is summarised and replaced by that summary in place.

Recent turns are kept verbatim, since the agent needs precise detail about what it just did.

What the summary must retain is not the narrative but the state.

The original goal, the constraints given, the decisions already made, and the work already completed.

Drop the last of those and the agent will confidently redo a task it finished twenty minutes ago.

Drop a constraint and it will violate an instruction it was given at the start.

Because summaries are lossy, durable facts are better written outside the context entirely.

A scratchpad file or a memory store survives compaction intact and can be re-read on demand.

Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
What must a context compaction step preserve above all?
AI Agents

17 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close