Compaction & recitation
system prompthistorytool outputobjective
- Tool outputs saturate context; the goal is lost in the middle.
- Offload: tool outputs become restorable file pointers.
- Compaction: older turns become a structured summary.
- Recitation: todo.md returns to the recency peak.
Notes & references
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
- Effective context engineering for AI agentsAnthropic
- Context engineering: lessons from building ManusManus
- CompactionAnthropic
- Context editingAnthropic
- Lost in the MiddleLiu et al.