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
pgvector Can Return 4 of Your 10 Matches—Where Did the Rest Go?
A filtered vector search can quietly starve your AI app of evidence. pgvector has a fix, but builders need to measure recall and latency before trusting it.
Agent Memory Consolidation Scheduling: When to Merge, Summarize or Forget
Your agent doesn't need more memory — it needs a maintenance schedule. Here's how to build merge, summarize, and decay policies that keep long-term agents sharp instead of senile.
LLM Output Schema Evolution: Migrating Producers and Consumers Without a Flag Day
Your JSON schema will change. Here's how to evolve LLM output contracts safely — with dual readers, shadow validation, and staged rollouts instead of a risky big-bang cutover.
Most Read
- 1 Kimi K3.1 Pro: China's Open-Weight Model Just Caught the Frontier
- 2 OpenAI Is Buying the Bottleneck: Can Orchestra Bio Cut a 15-Year Drug Timeline?
- 3 1,068 Tokens Per Second: Can Mercury 3 Change How LLMs Generate Text?
- 4 Same Price, Better Coding: How Good Is DeepSeek V4.1 Flash 1017?
- 5 Used OpenCode? Should You Rotate Your API Key After the Console Breach?
Latest News
- Claude Code Left Secrets in 1,122 GitHub Files—Check This Setting
- Gemini Omni 1.2 Flash Makes 15-Second Video Cheaper—Then Comes the Edit
- GPT-6 Cyber Can Build Exploits—But Builders Face a Different Test
- 1,122 GitHub Files Exposed Secrets—What Did “Don't Ask Again” Save?
- Gemini Omni 1.2 Flash Offers $1.05 Clips—What Does That Buy You?
- GPT-6 Cyber Hits 48.5% on ExploitGym—Who Gets the Keys?
Tags
> Stay in the loop
Weekly AI tools & insights.