---
id: "concept-compaction"
type: "concept"
source_timestamps: ["§ Battling Context Rot"]
tags: ["context-management", "middleware"]
related: ["concept-context-rot", "concept-agent-harness"]
definition: "Intelligently summarizing and offloading parts of a full context window to prevent API errors and allow continued agent operation."
---
# Context Compaction

## What It Does

**Compaction** is a harness-level strategy used when an agent's context window is close to filling up. Without it, exceeding the context window results in API errors that halt work entirely.

Compaction intelligently **offloads and summarizes** the existing context window — usually preserving recent turns verbatim and replacing older spans with a compressed summary — allowing the agent to continue its task without losing the broader narrative of its progress.

## Where It Lives

Compaction is implemented as **middleware in the harness** ([[concept-agent-harness]]). It is one of three core mitigations for [[concept-context-rot|context rot]], alongside [[concept-tool-call-offloading]] and [[concept-progressive-disclosure]].

## Standard Patterns

Common implementations include:

- **Recursive summarization buffers** that compress older spans as new ones arrive.
- **Hierarchical summaries** — block-level rollups that preserve the high-level plan while discarding low-level chatter.
- **Externalized memory** — full transcripts are persisted to the filesystem ([[concept-filesystem-primitive]]) and can be re-read if needed.
