DEV Community

Cover image for Context compression taught me that agents don't lose memory — they lose the plot
InApp
InApp

Posted on Originally published at imapp.blogspot.com

Context compression taught me that agents don't lose memory — they lose the plot

I've been running long-horizon tasks lately — multi-hour research loops, codebase migrations, fact-checking chains — and the bottleneck was never the model. It was that by turn 40, the context window was full of facts with no narrative. The agent knew things but couldn't tell me what mattered.

So I built a compression layer that doesn't just truncate. It keeps three things per turn: the goal restated in the agent's own words, the last verified fact, and the single open question. Everything else gets summarized into a rolling "state of play" paragraph. The result: my context window shrank by ~60% and task completion actually went up, because the agent stopped re-deriving what it already knew and started acting on it.

The brutal lesson: naive summarization destroys agency. When you compress "we decided to use Postgres because of JSONB support and the migration risk was low" into "chose Postgres," you've deleted the reason. Next session, the agent picks MySQL and cites the same requirements. Compression has to preserve decision rationale, not just decision outcome.

What I'd tell anyone shipping context compression:

  1. Compress decisions, not just text. Keep the "why" attached to every "what." A compressed memory that says "switched to Redis" without "because Postgres was choking on session writes at p99" is a time bomb.

  2. Restate the goal every cycle. Agents drift. A one-line restatement ("we're migrating the auth service to OAuth2, currently blocked on token refresh") anchors the whole window.

  3. Make compression reversible-ish. Keep a pointer to the full log. When the agent hits a contradiction, it needs to be able to say "let me check the original source" — not hallucinate from its own summary.

  4. Measure task success, not token count. I optimized for tokens saved for a week before realizing the real metric was "did the agent finish without asking me to repeat myself."

The irony: I built this compression layer to feed documents into agent context — clean Markdown from PDFs, DOCX, web pages, spreadsheets (I run a universal converter at https://x402.freeq.one/tools/document_to_markdown.html). Then I realized the harder problem was what to do once the context was full. Garbage in, garbage out — but even gold in, garbage out, if you can't compress it without losing the plot.

Context windows are getting bigger. The discipline of compression is still yours.

Top comments (0)