Published: 2026-09-21
Deep dive

Build an agent that compacts itself: three thresholds, a self-compact tool, and a note to self

Chapters / key moments (click to jump — plays here on the page)

Most agent tools let you set the percentage at which auto-compaction fires. The argument here is that a fixed threshold is the wrong control: your agent knows when it is at a natural stopping point and you do not. This is a build-along for an agent that owns its own compaction, and the ideas transfer to any harness you can modify.

Source video

"Self-Compact Pi Agent: ZERO HYPE Agentic Coding Devlog" by IndyDevDanWatch on YouTube →

The problem being solved

Long-running autonomous agents run out of context. Long context also degrades performance through context rot and burns cash. Auto-compaction at a fixed percentage fires wherever the counter happens to land — quite possibly mid-task, discarding the working state that mattered.

The motivating case is a swarm: tens to hundreds of agents running for hours toward a shared goal. At that scale nobody is watching to press compact, so the agents have to manage it themselves.

The four pieces

  • A dedicated self-compact tool. The agent can call it at any point, exactly like any other tool. This is the core move — compaction becomes a decision the agent makes rather than an event that happens to it.
  • Three threshold levels: notice, warning, and force. A soft notice suggests compaction, a warning presses harder, and force is the backstop. Spacing the first two gives the agent room to pick its own moment.
  • Custom compaction prompts at each level, replacing the harness default. The soft notice reads roughly: you may continue to use all tools; if a natural stopping point exists, consider self-compact.
  • Human-in-the-loop commands to inspect the thresholds and force compaction by hand — mainly for testing, but a sensible escape hatch.

The detail most people miss: the note to self

Self-handoff

When the agent compacts itself it passes its own handoff message alongside the normal compaction summary. The generic summary describes what happened; the note to self carries what the agent specifically wants to remember on the other side. Two different jobs — and only one of them is done for you by default.

What you can override, and where

Every agentic coding tool has a compaction prompt. As stated in the video: you can override it in the Pi coding agent, he believes you can override Codex's, and you cannot override Claude Code's. That constraint is the whole argument for harness engineering — the ceiling on what you can build is set by how much of the tool you are allowed to change.

A nice side observation from the build: he had different models write the compaction prompts, and found Fable 5.1 wrote noticeably better prompts than GPT-6 Astra when the task was prompt-engineering another agent.

Key Takeaways

  • Give the agent a self-compact tool so compaction becomes a judgement call at a natural boundary rather than a threshold trip.
  • Use at least two spaced warnings before forcing — the gap is where the agent gets to choose well.
  • Write your own compaction prompts. The built-in one was not written for your workload.
  • Pass a self-handoff note through the compaction; the auto-summary alone loses intent.
  • Check what your harness lets you override before designing around it — Claude Code's compaction prompt is fixed.
  • Keep a manual force-compact command for testing, even in a fully autonomous design.

More on keeping long sessions cheap and coherent: cost optimisation and context window.