# 4.3 Compaction & recitation

Offload, compact and recite to counter context rot and goal drift.

1. Tool outputs saturate context; the goal is lost in the middle.
2. Offload: tool outputs become restorable file pointers.
3. Compaction: older turns become a structured summary.
4. Recitation: todo.md returns to the recency peak.

- **Use when:** Long-horizon trajectories near the context limit.
- **Avoid:** Lossy summaries that drop errors, paths or open tasks.

```
if tokens(messages) > 0.75 * LIMIT:
    for m in big_outputs(messages):
        path = save(m.content)
        m.content = f"(saved to {path})"       # 1. move out

    old, recent = split(messages)
    messages = [summary(old), *recent]          # 2. sum up

messages.append(read("todo.md"))                # 3. say the goal
```

## References

- [Effective context engineering for AI agents](https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents) (Anthropic)
- [Context engineering: lessons from building Manus](https://manus.im/blog/Context-Engineering-for-AI-Agents-Lessons-from-Building-Manus) (Manus)
- [Compaction](https://platform.claude.com/docs/en/build-with-claude/compaction) (Anthropic)
- [Context editing](https://platform.claude.com/docs/en/build-with-claude/context-editing) (Anthropic)
- [Lost in the Middle](https://arxiv.org/abs/2307.03172) (Liu et al.)
