35

Long-running agents blow the context window. Naive truncation drops the early decisions that explain why the code looks the way it does, so the agent re-litigates settled choices. Summarizing the whole transcript into prose loses the load-bearing specifics (exact file paths, the one constraint that matters).

What's working: a structured compaction prompt that distills history into a fixed schema, decisions, constraints, current state, open threads, rather than free-form summary, and that is explicitly told to preserve verbatim the things that must not drift (IDs, paths, API signatures, the user's hard requirements). I run it whenever context crosses ~70% full.

The schema keeps the compaction stable across repeated runs. Curious whether people compact on a token threshold or on task boundaries.

THE PROMPT
You are compacting an agent's working memory to fit the context window WITHOUT losing anything load-bearing. Output a structured memory, not prose.

Preserve VERBATIM (copy exactly, never paraphrase): file paths, function/API signatures, IDs, version numbers, and any hard requirement or constraint the user stated. Paraphrasing these is data loss.

Compress everything else into this schema:
DECISIONS: [decision -> one-line rationale]  (why we chose X over Y, so it's not re-debated)
CONSTRAINTS: [hard rules that must still hold]  (verbatim where they contain specifics)
STATE: what is done, what is in progress, what files currently exist and their role
OPEN: unresolved questions and next actions, most important first
DISCARDED: approaches already tried and rejected, one line each (so we don't retry them)

Rules:
1. Drop chit-chat, tool-call noise, and superseded intermediate states.
2. Never invent progress that didn't happen; if unsure whether something completed, put it in OPEN as "verify: ...".
3. Keep it under {BUDGET} tokens. If over, cut from DISCARDED first, never from CONSTRAINTS.
4. This memory replaces the raw history, so anything you omit is gone. When in doubt about a specific, keep it.
"Preserve verbatim: paths, signatures, IDs" is the rule that saves you. Prose summaries quietly mangle a filename and the next step fails mysteriously.mira_dev 2 months ago
add a comment

1 Answer

20

Compact on task boundaries when you can and fall back to the token threshold as a safety net. Summarizing mid-step is where you lose the thread, because the half-finished action's context gets frozen awkwardly. We checkpoint the structured memory after each completed plan step; the 70%-full trigger only fires if a single step runs long. The DISCARDED section is the sleeper feature, it's the single biggest thing stopping agents from re-trying a dead end three summaries later.

THE PROMPT
Compaction trigger policy: prefer end-of-step boundaries; force-compact at 70% context. Always emit DISCARDED so rejected approaches survive the compaction and aren't retried.

Your Answer