Harpalsinh Jadeja

Context Compaction Techniques for Coding Agents

Long-running coding agents routinely generate tens or hundreds of thousands of tokens of history: file reads, tool outputs, error logs, failed approaches, and intermediate decisions. At some point the context window fills, quality collapses, and the agent begins re-exploring solved problems, forgetting file paths, or contradicting earlier choices. The difference between a reliable multi-hour session and an expensive failure is rarely the underlying model. It is how the system manages context.

The conversation is the small part. Every exchange also drags in tool results, file reads, error logs and instruction files, and none of it leaves. Once the window is full the model is not reasoning over a workspace, it is reasoning over a landfill.
Context management is the call about what enters the model's working memory, and what is summarized, written out or split off instead. Everything that follows is a way of making that call well.

This article examines the problem, the strategies currently in use, their measurable effects on agent output, and the practical patterns that produce better results.

What context actually is

One window, five tenants: instructions, history, tools, code and tests. They are not interchangeable, and they do not cost the same.

In a coding agent the context window contains far more than the latest user message. Typical contents include:

The window is a finite attention budget, not unlimited storage. Models degrade well before the advertised limit, and information in the middle of a long context is especially poorly recalled. Every low-signal token dilutes the high-signal ones.

Why context management determines agent quality

Twenty-one thousand tokens of test output push a nine-token rule out of the window. The agent breaks it on the very next exchange, unprompted.

Coding tasks are long-horizon by nature. A non-trivial feature or bug fix can require dozens of tool calls, multiple failed hypotheses, and careful tracking of which files have been modified. Unmanaged history produces several failure modes:

Factory's evaluation of real production sessions (debugging, code review, feature implementation) spanning more than 36,000 messages showed that the quality of compression directly affects whether an agent can continue productively.1 The optimization target is tokens per task, not tokens per request.

Core strategies

Curate

Decides what gets into the window in the first place.

Distill

Shrinks what is already in there.

Externalize

Moves durable state out to somewhere the window is not.

Delegate

Spends a fresh window on the subtask instead.

Four levers on the same window. They compose rather than compete: curating harder does not make compression redundant, and neither one saves you from needing somewhere to put the state that outlives the turn.

Most effective techniques fall into four complementary levers.

Curate: what enters the window

Each file holds more than the model needs. Pull in only the relevant piece and leave the rest unloaded: the cheapest token is the one never read, and a filter on the way in beats cleanup on the way out.

The cheapest improvement is never loading irrelevant material in the first place.

Prevention is more powerful than later cleanup. Approaches that combine targeted search with compact diffs can extend the useful life of a context window by 3-4x before any compaction is required.

Distill: shrinking what is already there

When history still grows, systems apply one of several compression families.

Summarization (rewriting)

When the window fills, older turns are lifted out, rewritten into a short structured summary, and placed back. Twenty-six thousand tokens of history become six hundred, the recent tail is kept intact, and the window drops well under its limit.

An LLM produces a condensed narrative of prior turns. Factory's structured, iteratively updated summaries, with anchored sections for intent, file modifications, decisions and next steps, retained more useful information than either OpenAI's high-ratio opaque compression or Anthropic's full-regeneration approach.1 On probe questions testing factual recall, artifact tracking and continuity, the structured method scored highest overall.

Verbatim, deletion-based compaction

A pass reads the window top to bottom and deletes the low-signal lines. What survives is left byte-for-byte and packs into a shorter stack: the install chatter goes, and the exact error, its location and the failed assertion come through unchanged.

Instead of rewriting, low-signal lines are simply removed. Morph's Compact, and the faster FlashCompact variant, operates at 33,000 tokens per second, achieves 50-70% reduction, and guarantees that every surviving line is byte-for-byte identical to the original.2 This preserves exact file paths, error messages and code fragments, the details that paraphrasing frequently loses. Because no new text is generated, hallucination risk from the compression step itself is zero.

Self-summarization as a trained skill

Cursor trains its Composer model with compaction-in-the-loop.3 The model learns to produce its own concise summaries when a token threshold is reached; those summaries become part of the training trajectory and are rewarded or penalized according to downstream task success. The resulting self-summaries are shorter, roughly 1,000 tokens against more than 5,000 for a prompted baseline, and reduce compaction error by approximately 50%.

Multi-layer pipelines

Claude Code employs a practical three-tier system: a cheap micro-compaction that runs on every turn to trim tool-result bloat, a session-memory reconstruction that rebuilds state from disk without an extra model call, and a full legacy summarization only when necessary.4 A preserved tail of recent messages is re-linked after the summary so the active working set remains intact.

Observation masking and adaptive rules

Older tool outputs can be replaced by placeholders while the tool-call skeleton is retained.5 Self-evolving systems such as TACO discover and reuse compression rules from the agent's own trajectories, filtering low-value terminal observations while protecting task-critical signals.

Proactive folding

Research systems (AgentFold, Context-Folding) treat context as a dynamic workspace.6 The agent can branch into a sub-trajectory for a subtask and later fold it, collapsing intermediate steps into a concise outcome summary. This can keep the active context an order of magnitude smaller than a pure ReAct baseline while matching or exceeding performance on long-horizon benchmarks.

Externalize: keeping state outside the window

State that has to outlive the turn belongs on disk, each kind in its own file, not in the transcript. Writing it out frees the window, and when a later task needs it, it can be pulled back a slice at a time.

Information that must survive across turns or sessions is written to durable artifacts:

The agent, or the harness, can later re-inject only the relevant slice. This is the coding-agent analogue of human note-taking.

Delegate: fresh windows for subtasks

The sub-agent's exploration happens in a window that gets thrown away. Only the result crosses back, so a noisy 240-file search never becomes permanent history on the main thread.

Sub-agents receive a clean context containing only the information needed for their narrow responsibility. The parent retains only the final result. This prevents exploratory noise or large search results from permanently contaminating the main thread. Phase separation, research to plan to implement to review, in separate sessions achieves a similar isolation.

Effects on agent output

Sharper recall

File paths, error messages and past decisions stay exact.

Continuity

The agent states what comes next without re-deriving the whole history.

Longer horizons

Sessions that used to collapse after 20-30 turns keep going.

Lower cost

Fewer tokens per task, and each call carries less baggage.

Good context management pays off four ways at once: what the model recalls stays exact, it knows what to do next, it holds together for longer, and it costs less to run.

Good context management produces measurable improvements:

Poor management produces the opposite. Paraphrased summaries that lose critical details force the agent to re-fetch information, lengthening trajectories; JetBrains observed 13-15% longer runs under pure summarization. Opaque high-ratio compression can be even more damaging, because the lost information is no longer inspectable. Overly aggressive always-loaded instruction files dilute the very constraints they were meant to enforce.

Artifact tracking, meaning which files have been read or modified, remains a weak point across current methods and often requires dedicated indexing beyond pure summarization or deletion.

Practical recommendations

The four levers, in the order they usually earn their keep: curate on the way in, distill and externalize as the window fills, and delegate the subtasks that would flood it.

Emerging directions

Research is moving from reactive cleanup toward proactive, agent-controlled management. Models are being trained to decide when and how to fold or compress their own history. Self-evolving rule sets adapt compression to the specific workflow. Specialized fast compaction models make it practical to run deletion-based cleanup inline before every LLM call. Knowledge-graph and code-intelligence layers promise richer external memory that can be queried precisely instead of being stuffed into the prompt.

The central insight remains simple: for coding agents, context is not a passive log. It is an actively governed workspace. The systems that treat it that way produce more coherent, cheaper and more reliable results over the long horizons that real software engineering demands.

References

  1. 1.Factory: Evaluating compression
  2. 2.Morph: Context compaction
  3. 3.Cursor: Self-summarization
  4. 4.X: Claude Code's three-tier compaction (@himanshustwts)
  5. 5.arXiv: A Self-Evolving Framework for Efficient Terminal Agents via Observational Context Compression
  6. 6.arXiv: AgentFold: Long-Horizon Web Agents with Proactive Context Management