跳到正文
DEV Community· InApp·· 4 小時前AI 評分52

上下文壓縮讓我明白,智能體不是失去記憶,而是失去主線

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

AI 導讀

InApp 為長時程智能體任務建立上下文壓縮層,保留每輪目標、最後核實的事實和一個待解問題,令上下文窗口縮小約 60%,任務完成表現亦提升。其他內容則整理成持續更新的狀態摘要。作者建議保留決策理由、每輪重述目標及完整記錄的連結,並以任務能否完成而非節省多少 token 衡量效果。

正文

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

InApp

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.

來源:DEV Community · dev.to