Compaction, or folding, is the act of rewriting older history into a shorter representation so a long run keeps working inside a fixed context budget. Every implementation of it is lossy: after the fold, the agent reasons from a shorter record, and what survives depends entirely on the strategy you picked (Haystack compaction docs). That single sentence explains why trigger design matters more than strategy design. A good strategy fired at the wrong moment still loses the one line that mattered.
The triggers you will meet in production fall into three families: turn count, token milestone, and tool result. Each family has a condition that justifies folding, a condition that does not, and a distinct failure mode when you call it at the wrong time. The rest of this article works through the three, then gives you a decision table you can paste into a runbook.
A useful frame before the details: the context window is working memory, not storage. It holds exactly what goes into this call, it has a hard limit, and it is wiped when the call returns. Anything the agent needs beyond that must be written somewhere persistent and deliberately loaded back (context engineering for multi-turn agents). A fold is a move from one representation of working memory to another, so the question is never whether to lose information, but which information you can afford to lose.



