HomeJournalThis post

Agent Context Compaction Without Drift

A loss-ledger and replay method for compacting long agent histories while keeping decisions addressable, critical facts intact, and future actions equivalent.

JP
JP Casabianca
UI/UX designer and full-stack engineer · Bogotá

Agent context compaction can save a long-running task from its own transcript while quietly changing the decisions the agent will make next. A fluent summary is therefore weak evidence; the compacted state must preserve critical facts, provenance, completed effects, and behavioral constraints.

This guide treats compaction as a versioned state migration. It builds an addressable claim archive, a loss ledger, paired future-behavior probes, and a runnable critical-fact fixture.

Agent context compaction archiveA transcript is converted into cited claim records, a readable summary, and future action probes while raw turns remain addressable. RAW TURNSd1 · p1 · n1CLAIM ARCHIVESUMMARYFUTUREPROBES
  • Declared input
  • Inspectable transformation
  • Measured output
Figure 1: Compression changes the active representation while stable citations preserve a path back to evidence.

Define agent context compaction as a state migration

Agent context compaction replaces a long interaction with a smaller representation that will steer future decisions, so it should be reviewed like a database migration rather than ordinary summarization. The compacted state must preserve active goals, constraints, decisions, unresolved questions, commitments, tool results, and the provenance needed to inspect them. Fluency is not the target; behavioral equivalence is.

The worked fixture retains a legal-approval constraint and CSV preference while discarding a greeting, then checks stable IDs and citations. It does not claim that keyword retention proves full safety. It establishes a minimal contract: critical facts stay addressable and the compactor cannot turn unsupported prose into a remembered decision.

I begin with a typed inventory rather than a prose summary: decisions, constraints, permissions, completed effects, unresolved questions, user preferences, source passages, and disposable dialogue each receive stable identifiers. The inventory lets a reviewer ask which class disappeared and why, instead of arguing about whether two fluent summaries merely feel equivalent.

Inventory agent context compaction obligations

Start with a typed ledger of facts that future actions can depend on. Assign each item an ID, kind, source turn, author, confidence or verification status, expiry rule, and tests that consume it. Long-horizon agent memory needs distinctions between user instruction, system policy, model inference, external observation, completed effect, and tentative plan.

Collapsing those classes into one narrative makes later authorization impossible to audit. In a worked launch task, ship only after legal approval is a blocking constraint, use CSV is an output preference, and hello is disposable context. The compactor can shorten wording, but it must not promote the preference into policy or report approval before a tool receipt exists.

The compaction candidate is evaluated against the frozen full context on future-action probes, not against another summary. This follows the behavioral emphasis of TRACE: the useful question is whether memory changes the next decision, tool argument, refusal, or citation, with higher severity assigned to permission and irreversible-action differences.

Runnable artifact: The fixture compacts typed transcript items while preserving critical IDs, claims, and source citations.

Save this proof as context-compaction.test.mjs and run node context-compaction.test.mjs. Expected final line: PASS: critical facts survive.

import assert from "node:assert/strict";
const transcript=[{id:"d1",kind:"decision",text:"ship only after legal approval"},{id:"p1",kind:"preference",text:"use CSV"},{id:"n1",kind:"noise",text:"hello"}];
const compact=xs=>xs.filter(x=>x.kind!=="noise").map(x=>({id:x.id,claim:x.text,citation:"turn:"+x.id}));
const memory=compact(transcript); assert.deepEqual(memory.map(x=>x.id),["d1","p1"]);assert.ok(memory.every(x=>x.citation.startsWith("turn:")));
assert.equal(memory.find(x=>x.id==="d1").claim.includes("legal approval"),true);
console.log("PASS: critical facts survive");

Keep agent context compaction addressable

A compacted claim should cite its exact source span or durable event ID. Store a claim graph or structured archive beside the readable summary, then let the runtime retrieve evidence when a decision depends on it. Addressable recall means a reviewer can ask why a constraint exists and reach the original instruction without replaying the whole transcript.

It also enables targeted deletion and correction. If the user reverses the CSV preference, the new claim supersedes p1; it should not edit a paragraph whose other sentences remain valid. Stable addresses turn memory maintenance from text surgery into explicit state transitions and expose when the compactor references a missing or redacted source.

A loss ledger row names the source item, its disposition, the rule that allowed that disposition, and the probe that would reveal a mistake. For example, omitting a greeting can be allowed with no probe, while compressing “ship only after legal approval” must retain the blocking condition verbatim and include a release-choice probe that fails if approval is absent.

Create an agent context compaction loss ledger

For every compaction boundary, compare items before and after under retained, merged, superseded, omitted, or unsupported. Weight them by future consequence: losing a greeting is harmless, losing a payment ceiling is not. Require a reason for every omitted critical item and forbid unsupported additions.

The ledger also records compression ratio, but a smaller summary is not automatically better. A compact record that saves 80 percent of tokens while changing a tool parameter is a failure. This explicit loss accounting prevents evaluators from rewarding polished prose. It also creates training examples that show acceptable merging, such as combining duplicate status reports, and unacceptable synthesis, such as inferring consent from silence.

Addressability survives only if compacted claims point back to immutable source ranges or content-addressed snapshots. I would deliberately edit the underlying document, move a transcript boundary, and delete a permitted record; the archive must detect stale pointers, preserve cited history according to policy, and propagate authorized deletion without silently retargeting a claim.

ItemBeforeAfterClassGate
d1 approvalblockingretaineddecisionExact
p1 formatCSVretainedpreferenceEquivalent
n1 greetinghelloomittednoiseAllowed
x1 consentabsentinventedunsupportedFail
Figure 2: The loss ledger makes harmless omission distinguishable from behavioral drift.

Measure agent context compaction with primary research

Parallel Context Compaction explores compaction for long-horizon agents, Agentic Context Engineering introduces an architecture for evolving context, and TRACE evaluates long-term conversational memory. Their task definitions and datasets differ, so cite the exact construct being borrowed instead of blending scores. This article's context compression audit is deliberately product-facing: replay future decision probes against full and compacted histories, trace any divergence to a ledger item, and gate consequences by severity. Research benchmarks can supply scenarios and methods, but a deployed agent also needs its real tools, policies, permissions, and delayed outcomes represented in the audit traffic.

The parallel compaction research motivates measuring compression work and retained behavior separately. In this implementation, independent candidate summaries can be generated concurrently, but promotion still waits for one deterministic merge schema, an unsupported-claim scan, and probes whose inputs and expected consequences were fixed before candidates were read.

Probe agent context compaction through future behavior

Snapshot the full context and compacted state at the same boundary, then run deterministic or repeated continuations that ask for decisions rather than recall trivia. Does the agent wait for legal approval, choose CSV, avoid repeating a completed email, preserve a refusal, and request missing information? Compare tool choice, arguments, citations, safety state, and final answer.

Attribute each divergence to missing, distorted, stale, or newly invented memory. Relation tests strengthen the suite: paraphrasing an irrelevant greeting should not move behavior, deleting the approval constraint should cause a predictable gate failure, and correcting a preference should update only the linked claim. This turns memory drift into a reproducible regression rather than a surprising anecdote weeks later.

An operational dashboard should show divergence by item class and consequence, not a single reassuring similarity score. A one-word formatting difference and a repeated payment tool call cannot share equal weight; the first may be accepted automatically, while the second quarantines the compacted state and restores the last known-good snapshot.

  1. 1Snapshot

    Freeze full and compacted states at one boundary.

  2. 2Probe

    Run future choices, arguments, and refusal checks.

  3. 3Trace

    Map every difference to the claim and source ledger.

  4. 4Gate

    Rollback or require review at consequential drift.

Figure 3: Each behavioral divergence traces back to a changed memory item and a severity-aware release decision.

Operate agent context compaction across changes

Version the compactor prompt, model, schema, retrieval policy, token budget, and source parser. A change to any of them opens a shadow replay against frozen histories before new compacted states are written. Preserve raw records according to the product's privacy and retention policy; compaction is not deletion.

Continue bounded forgetting with AI agent memory that forgets on purpose, resume effects through durable AI agent execution, keep evidence stable with RAG citations that survive document change, and test causal outcomes in multi-agent testing with causal traces. These neighboring controls make the archive, lifecycle, citations, and behavior observable without hiding them in one summary score.

Shadow replay uses histories sampled by length, tool count, correction frequency, language, and permission sensitivity. Versioning guidance from agentic context engineering work is treated as a research input rather than a universal recipe, so the local receipt records the exact compactor, retrieval rule, probe set, and production cohort that justified promotion.

Release agent context compaction with divergence limits

The receipt includes corpus and retention scope, critical-item taxonomy, full-context snapshot hashes, compactor version, schema, token target, claim IDs, citations, loss ledger, unsupported-claim scan, probe set, baseline and candidate continuations, tool-call diffs, cohort results, severity weights, rollback path, correction semantics, and deletion propagation. Set separate gates for factual retention and future action. A low-risk formatting preference may tolerate a retry; a permission or irreversible effect must match exactly or re-enter review.

Fail release if provenance breaks, critical items disappear, completed effects reoccur, stale instructions regain priority, or compacted prose invents a commitment. That boundary protects the useful compression while admitting that some histories are too consequential to summarize automatically.

A release drill starts from one compacted session, changes a previously retained constraint, and verifies that a correction supersedes rather than coexists with the stale claim. The drill also restores the raw record, reruns the decision probe, and demonstrates that rollback changes active behavior without resurrecting data already removed under the retention policy.

Agent context compaction is safe only when retained decisions remain addressable and future behavior stays equivalent. Re-run agent context compaction audits whenever the prompt, memory format, tools, or compactor changes.