Compaction & recitation

system prompthistorytool outputobjective
Context window75% of budgetFilesystemlog_a.txtlog_b.txtsystemΣgoalΣrecenttodo.md
  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.
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