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.
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.