Join our Newsletter — 33% off our NHI Course

Context Window Compaction

Context window compaction is a method for reducing the amount of conversation or task history kept in an AI model’s prompt. It summarises or prunes earlier messages, tool outputs, and state so the model can keep operating within its token limit while preserving the most relevant context.

How Context Window Compaction Works

context window compaction reduces the amount of history an AI model must carry forward by summarising, trimming, or restructuring prior messages, tool outputs, and state. The goal is to preserve the information that still matters while freeing tokens for the next turn.

This is not the same as simply deleting old content. Good compaction keeps the active task intact, retains unresolved decisions, and removes detail that no longer changes the model’s next action. When done well, it can extend long-running workflows without forcing a reset.

Why Compaction Matters for Long-Running AI Workflows

As conversations grow, the cost of carrying every prior turn increases: more tokens, more latency, and a greater chance that the model will drift toward stale detail. Compaction helps maintain continuity when an assistant is being used as a persistent work surface rather than a single-shot responder.

The practical value is strongest in multi-step analysis, agentic workflows, and tool-heavy sessions where the model must remember decisions, constraints, and open work. It can also help reduce the chance that irrelevant earlier material crowds out the most recent instruction or task objective. LLM Security Guide

What Must Be Preserved During Compaction

The main design challenge is deciding what can be compressed without losing meaning. High-value items usually include the user’s goal, active constraints, tool results that still affect the next step, and any state that would be expensive or dangerous to reconstruct later.

Low-value items often include repeated phrasing, settled sub-discussions, and verbose intermediate reasoning that no longer changes the plan. The best compaction schemes treat context as state, not as a transcript, and they preserve the facts that are operationally relevant while discarding the rest. AI Agent Memory Security Guide

Compaction Trade-Offs and Failure Modes

Compaction always creates a trade-off between efficiency and fidelity. Over-aggressive summarisation can omit a constraint, flatten nuance, or merge distinct decisions into one vague summary. That can lead to incorrect follow-up actions, inconsistent outputs, or a model that appears coherent while quietly losing important context.

The same mechanism can also amplify mistakes if earlier content was already wrong. Once a bad assumption is condensed into the retained state, it can persist longer than the original noisy exchange would have. For that reason, compaction works best when the retained summary is treated as a controlled artefact, not an unquestioned memory dump. MITRE ATLAS adversarial AI threat matrix

How Organisations Use Compaction Safely

Practitioners should decide what the model is allowed to remember, who or what can write to retained state, and how often summaries are reviewed for accuracy. Compaction is safest when it is paired with explicit state boundaries, clear retention rules, and a way to recover the original source material when needed.

It is also important to keep sensitive data out of compacted memory unless it is genuinely required for the task. That includes credentials, secrets, and other high-impact material that should not be preserved just because it is convenient. The more autonomous the workflow, the more important it becomes to separate durable task state from incidental conversation history. Model Context Protocol: Authorization specification

Risk and Threat Considerations

Compaction can create security exposure when it strips away the details needed to recognise poisoning, cross-session leakage, or an incorrect state transition. If a malicious or malformed earlier turn is summarised too aggressively, the model may continue operating on a corrupted version of reality.

Failure mechanism: Important context is lost, merged, or rewritten during summarisation, so later model behaviour reflects an incomplete or attacker-influenced state instead of the original task history.

Impact: The workflow can drift, sensitive information can be exposed across turns, and an AI system may continue executing with incorrect assumptions that are difficult to detect after the fact.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP Agentic AI Top 10 and OWASP Non-Human Identity Top 10 address the attack and risk surface, while NIST SP 800-53 Rev 5 sets the governance and control requirements practitioners need to meet.

Framework Control / Reference Relevance
OWASP Agentic AI Top 10 ASI06 — Memory & Context Poisoning Compaction changes agent memory handling and can preserve poisoned context.
Recommendation — Limit retained context and validate summaries to reduce memory poisoning risk.
OWASP Non-Human Identity Top 10 NHI-02 — Secret Leakage Compaction can accidentally retain or re-expose secrets in preserved context.
NHI-08 — Environment Isolation Context compaction must preserve separation between sessions and task state.
Recommendation — Exclude secrets from retained context and verify summaries do not leak them. Keep per-session context isolated so compaction does not blur boundaries.
NIST SP 800-53 Rev 5 AU-6 — Audit Review, Analysis, and Reporting Compacted context still needs reviewability for detected anomalies and state changes.
IA-5 — Authenticator Management Compaction should not preserve long-lived credentials or other sensitive authenticators.
Recommendation — Retain sufficient logs and review compressed state changes for anomalies. Prevent sensitive authenticators from entering retained conversational state.

Practitioner Guidance

Why practitioners should care: Compaction should be treated as a governed memory-control function, not as a purely cost-saving optimisation. In agentic or tool-using systems, the summary becomes part of the operating state, so its quality directly affects correctness, security, and recoverability.

What to watch for: Review whether the compacted state preserves open decisions, task boundaries, and any information that would change a later action. If summaries start becoming generic, lossy, or inconsistent with source history, the compaction policy needs tighter controls.