Context Compression for Long-Running AI Agents Without Losing Decisions
Long agent sessions eventually outgrow their context windows. The answer is not a generic summary—it is a layered record that preserves decisions, evidence, and open loops.
Long-running agents fail in a strangely human way: they remember the conversation but forget the decision.
A generic summary might preserve that a database migration was discussed. It may omit that the team rejected option A because it breaks rollback, approved option B only for tenants under a feature flag, and still needs a production owner. When the raw transcript falls out of context, those omissions become new bugs.
Good compression is not shorter prose. It is selective preservation of future-relevant state.
Store Events Before You Summarize Them
Treat every tool call, result, decision, and state transition as an append-only event. The transcript is presentation; the event log is the durable record.
{
"event_id": "evt_0189",
"task_id": "migration_42",
"type": "decision.accepted",
"time": "2026-08-16T04:22:00Z",
"actor": "owner",
"payload": {
"choice": "shadow_write",
"constraints": ["feature_flagged", "rollback_under_10m"],
"evidence_refs": ["tool_771", "doc_14"]
}
}
Append-only events make compression reversible. If a summary looks suspicious, the system can recover the source evidence. Without those references, a fluent summary becomes an unauditable replacement for history.
Build Multiple Memory Layers
One blob cannot serve every horizon. Use at least four layers:
- The active window contains the current goal, immediate plan, recent evidence, and next action.
- The task snapshot contains durable state: decisions, constraints, artifacts, owners, and unresolved items.
- The event log contains the complete structured history.
- The knowledge layer contains reusable facts that outlive the task, with provenance and update rules.
The active window should be cheap to read on every turn. The event log should be cheap to append. Retrieval should pull only the relevant slices when an old decision becomes important.
Compress Around Invariants
Before summarization, define fields that cannot be silently dropped:
type TaskSnapshot = {
objective: string;
doneCriteria: string[];
authority: string[];
decisions: Decision[];
rejectedOptions: Rejection[];
artifacts: ArtifactRef[];
completedSteps: string[];
openLoops: OpenLoop[];
currentRisk: string[];
nextAction: string | null;
};
The rejectedOptions field is crucial. Agents often rediscover and retry an approach because the summary kept the winner but lost the reason alternatives failed. Recording the rejection condition prevents expensive loops.
Authority also belongs in durable state. If the user approved a staging deploy but not production, compression must not flatten that into “deployment approved.”
Use Evidence-Preserving Summaries
Every claim that could affect a future action should link to an event, tool result, document, or user message. A summary can say:
Decision: Use shadow writes for the first migration cohort.
Reason: Direct cutover failed rollback timing in staging.
Evidence: tool_771, test_run_22.
Approved by: owner message msg_881.
Scope: staging and 5% production cohort only.
This format is less literary and far more useful than a paragraph. It separates fact, inference, and authorization.
Trigger Compression by Risk, Not Only Token Count
Waiting until the context is nearly full creates a rushed handoff. Compress after meaningful boundaries: a plan is approved, a phase completes, a decision changes, an external write succeeds, or control passes to another worker.
Token thresholds still matter, but they should trigger proactive snapshots while enough raw evidence remains available to validate the result.
After compression, run checks:
- Every open loop has an owner or next action.
- Every material decision has scope and evidence.
- Every artifact reference resolves.
- The completed-step list agrees with tool results.
- No permission became broader during summarization.
Keep Summaries Replaceable
Summaries are derived data. Version them and regenerate when the schema improves. Do not let a stale summary overwrite the event log or become the only copy of a critical decision.
For very long tasks, use incremental compaction. Merge the last snapshot with new events, then validate the new snapshot against both. This avoids repeatedly summarizing summaries, which amplifies omissions like a low-quality photocopy.
The practical goal is continuity. A fresh model instance should be able to answer three questions immediately: What are we trying to finish? What has been proven? What action is safe next?
If compression preserves those answers—and the evidence behind them—the agent can run for days without pretending its context window is infinite.
> Want more like this?
Get the best AI insights delivered weekly.
By subscribing, you agree to our Privacy Policy. You can unsubscribe at any time.
> Related Articles
AI Agent State Snapshots: Resume Long Jobs Without Repeating Side Effects
Durable agents need more than chat history. Snapshot plans, tool results, permissions, and idempotency state so a crash can resume safely instead of replaying the world.
Embedding Model Migration: Change Vectors Without Breaking Search
Embedding upgrades change the geometry of your index. Use versioned vectors, dual writes, shadow queries, and measured cutover instead of mixing incompatible representations.
LLM Request Coalescing: Stop Paying Twice for the Same Answer
When identical LLM requests arrive together, single-flight execution can collapse them into one upstream call—if cache keys, streaming, failures, and tenant boundaries are designed correctly.
Tags
> Stay in the loop
Weekly AI tools & insights.