liveBook · Manning

Compaction

As explained in Context Engineering

A process that summarizes an agent conversation and discards lengthy details while preserving high-level task information.

Compaction replaces a long agent conversation with a shorter continuation that preserves task-relevant state. It is a form of context compression: instead of simply deleting history, it transforms verbose material into a representation the agent can use in later decisions. section 7.4 section 11.3.8

Compaction is useful when history remains relevant but no longer fits comfortably in the active context. It complements context selection, which chooses what to include; context writing, which stores recoverable state outside the prompt; and isolation, which separates work across contexts. section 7.4 section 7.1

Figure 7.1 Context is managed through four complementary techniques: selection chooses relevant tokens for the active prompt, writing persists state to external storage, compression condenses verbose history, and isolation divides complex tasks across separate context windows
context management v6

The context-management pipeline shows how compression fits alongside selection, writing, and isolation. section 7.1

A coding-agent compaction flow can work as follows:

  • Trigger a secondary summarization pass as the conversation approaches a context limit; an 80% window threshold is one example. section 5.2.2
  • Summarize modified files, command outcomes, architectural decisions, unresolved bugs, and key implementation details; discard lengthy command logs while retaining the task goal. section 5.2.2 section 11.3.8
  • Continue with the shorter representation, recovering other useful details from notes or external storage when available. section 11.3.8

A return-policy decision

A support agent can replace twelve ticket messages with four lines while retaining the purchase date and plan tier. If compaction drops the plan tier, the agent may deny a return that a premium-plan exception permits, even though the summary is shorter and still relevant. section 12.1.6

Test what survives, not just how many tokens disappear. Check exact identifiers, file paths, error codes, and unresolved next actions alongside high-level facts. Compare downstream task quality and token counts; also watch for lost qualifications, citations, or state variables. section 7.9.1 section 12.1.3 section 12.1.6

Listing 7.2 Python example for context compression using LLMLingua
from llmlingua import PromptCompressor

conversation = build_sample_conversation() #1

compressor = PromptCompressor( #2
    model_name="microsoft/llmlingua-2-xlm-roberta-large-meetingbank",
    use_llmlingua2=True,
    device_map="cpu",
)

compressed = compressor.compress_prompt( #3
    conversation,
    rate=0.5,
    force_tokens=["\n", "?", "#", ":"],
)

compressed_text = compressed["compressed_prompt"] #4
required_facts = ["Maya owns rollback", "deployment is Friday"]
for fact in required_facts:
    assert fact.lower() in compressed_text.lower()

This Python example uses LLMLingua to compress a conversation and assert that selected facts survive. section 7.9.1

Repeated summarization can compound small distortions into context drift. Treat summaries as editable artifacts, validate them against user actions, and periodically re-anchor them with recent verbatim context. Set compaction thresholds with task-specific accuracy measurements in mind, rather than relying only on the model’s maximum window size. section 5.2.2 section 12.3.1

How Context Engineering covers it

How deep it goes
Explains triggering, retained state, drift mitigation, and fact-preservation checks, with an implementation example for compression.
Examples
Uses a return-policy ticket and a coding session with modified files and command outcomes to illustrate retained state.
What you can do after
Apply context compression with LLMLingua and check retained facts using assertions.
How it connects
Connects compaction with context selection, context writing, context isolation, and memory compression.

Also explained in

Compare how 10 books explain compaction →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Context Engineering. The section links point to the parts of the book it draws on.