Context Compaction
Description
Reduce an agent's active context when it approaches a configured budget while preserving the information required to continue correctly. Compaction must preserve active objectives, constraints, decisions, unresolved work, tool dependencies, authoritative evidence, and user-provided requirements while discarding redundant or safely recoverable history.
When to Use
- Long-running agent sessions approach context limits.
- Tool-heavy workflows accumulate large outputs.
- Repeated context windows cause cost or latency pressure.
- An agent must continue work after a context boundary without restarting.
Inputs / Outputs
| Area |
Contract |
| Inputs |
Conversation state, active task, completed actions, pending actions, evidence, dependencies, and context budget. |
| Outputs |
Compact continuation state plus a deterministic record of what was retained, discarded, or made recoverable. |
| Invariant |
The compacted state must not claim completion for unfinished work. |
Procedure
- Detect context pressure before the hard limit.
- Identify active objective, constraints, current state, dependencies, evidence, and pending actions.
- Preserve user requirements and facts needed to verify continuation.
- Collapse redundant tool output and stale narration.
- Prevent self-referential summaries from re-expanding the context.
- Validate the compacted state against the active task before resuming.
- If critical state cannot be preserved, stop and recover from an authoritative checkpoint instead of guessing.
Runnable Example
state = {"task": "deploy", "status": "in_progress", "evidence": ["tests-pass"]}
summary = {"active_task": state["task"], "status": state["status"], "evidence": state["evidence"]}
assert summary["status"] != "done"
print(summary)
Failure Modes
- Repeated compaction with no net reduction.
- Loss of active task or user constraints.
- Dropped tool dependency causing incorrect continuation.
- Summary claims a step is complete when it is only planned.
- Full transcript repeatedly re-sent to the compression model, creating unnecessary cost.
- Compaction failure silently continues with an over-budget context.
Recovery Boundary
Compaction is a state-preservation operation, not a license to infer missing facts. Missing critical state must produce BLOCKED or trigger an authoritative recovery mechanism.
Evidence
Public 2026 agent issues document repeated compaction failures, context bloat, lost checkpoints, and substantial token/cost waste in long-running agent workflows. These are direct demand signals for reliable context-management behavior.
Related
- short-term-memory.md
- working-memory.md
- long-term-memory.md
- state-machine.md
- retry-backoff.md
1---2name: context-compaction3description: Compress long agent context while preserving active task state, user constraints, tool dependencies, and evidence required for correct continuation.4metadata:5 source: https://skillmd.com/skills/samotech/context-compaction6 upstream: skills/03-memory/context-compaction.md7 category: 03-memory8 version: "v1"9---1011# Context Compaction1213## Description14Reduce an agent's active context when it approaches a configured budget while preserving the information required to continue correctly. Compaction must preserve active objectives, constraints, decisions, unresolved work, tool dependencies, authoritative evidence, and user-provided requirements while discarding redundant or safely recoverable history.1516## When to Use17- Long-running agent sessions approach context limits.18- Tool-heavy workflows accumulate large outputs.19- Repeated context windows cause cost or latency pressure.20- An agent must continue work after a context boundary without restarting.2122## Inputs / Outputs23| Area | Contract |24|---|---|25| Inputs | Conversation state, active task, completed actions, pending actions, evidence, dependencies, and context budget. |26| Outputs | Compact continuation state plus a deterministic record of what was retained, discarded, or made recoverable. |27| Invariant | The compacted state must not claim completion for unfinished work. |2829## Procedure301. Detect context pressure before the hard limit.312. Identify active objective, constraints, current state, dependencies, evidence, and pending actions.323. Preserve user requirements and facts needed to verify continuation.334. Collapse redundant tool output and stale narration.345. Prevent self-referential summaries from re-expanding the context.356. Validate the compacted state against the active task before resuming.367. If critical state cannot be preserved, stop and recover from an authoritative checkpoint instead of guessing.3738## Runnable Example39```python40state = {"task": "deploy", "status": "in_progress", "evidence": ["tests-pass"]}41summary = {"active_task": state["task"], "status": state["status"], "evidence": state["evidence"]}42assert summary["status"] != "done"43print(summary)44```4546## Failure Modes47- Repeated compaction with no net reduction.48- Loss of active task or user constraints.49- Dropped tool dependency causing incorrect continuation.50- Summary claims a step is complete when it is only planned.51- Full transcript repeatedly re-sent to the compression model, creating unnecessary cost.52- Compaction failure silently continues with an over-budget context.5354## Recovery Boundary55Compaction is a state-preservation operation, not a license to infer missing facts. Missing critical state must produce BLOCKED or trigger an authoritative recovery mechanism.5657## Evidence58Public 2026 agent issues document repeated compaction failures, context bloat, lost checkpoints, and substantial token/cost waste in long-running agent workflows. These are direct demand signals for reliable context-management behavior.5960## Related61- short-term-memory.md62- working-memory.md63- long-term-memory.md64- state-machine.md65- retry-backoff.md