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 IndyDevDan — Watch 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.

What you can actually set up from this

Extracted from the video's own transcript — the specifics the original summary left out.

Configuration shown

Threshold defaults the speaker chose for a GPT-6 Astra swarm (as stated in the video)

soft notice: 225K tokens
hard warning: 250K tokens
force compact: 270K tokens
The flags accept percentages or K/M suffixes; the spec caps percentages at 90%.

'How you are graded' rubric idea (paraphrased from the video)

- Graded continuously on every completed bullet in the definition of done and every workflow step.
- Instant failure if you write project deliverables outside your working directory (the plan directory is the exception).
- Instant failure if you read any file inside another agent's folder.
- If you cause a failure by mistake, stop immediately and report it.

Template shown in the video. Adapt to your project — do not copy verbatim without reviewing each line.

Reproducible steps

  1. Write the plan as a spec before any agent runs

    The speaker drafts a plan file by hand with these sections: title, problem (long-running autonomous agents run out of context, which hurts performance and burns cash), solution, variables, workflow, definition of done, and how you are graded. The level of detail in the prompt is the level of detail you get back.

  2. Give the agent its own self-compact tool

    The central piece is a tool the agent calls to compact its own context. It also lets the agent leave a note to itself, which is added as an extra user prompt during summarisation.

  3. Use three threshold levels instead of one auto-compact point

    A notice level (soft, a gentle prompt), a warning level (a sterner prompt to compact soon) and a force level (hard cutoff). Each level has its own user prompt, and a context bar in the UI shows cached tokens, uncached tokens, free context and the three markers.

  4. Replace the built-in compaction prompt

    The build overrides the harness's default compaction prompt with the speaker's own. His point: you can take full control of the compaction prompt when you control the agent harness, rather than accepting the default from Codex or Pi.

  5. Run the same spec on several harnesses

    Project root and similar values are variables so one spec can be handed to three different harnesses and models, each working in its own sub-application folder, then compared.

  6. Keep a human-in-the-loop fallback

    The spec asks for commands to show the threshold limits (self-compact info), to force a compaction by hand (self-compact now) for testing, and leaves the usual /compact untouched.

Gotchas

  • The speaker's reason for 270K: he says GPT model pricing roughly doubles past that mark, so the force level fires before costs jump. That is his claim; check OpenAI's pricing page for the current long-context threshold before copying the numbers.
  • Definition of done is how the agent knows when to stop; he now writes the end state there and leaves out deliverable lists. He ties this to agents that have no way to stop on their own.
  • The 'how you are graded' section is his own prompting habit, based on the idea that models are graded during training; it is optional and not a documented feature of any harness.
  • This is a custom Pi coding agent extension, not a setting in Claude Code or Codex. Most coding tools only let you set the level at which auto-compaction fires.