AI-ENGINEERING
    intermediate

    Context Compaction

    AI Engineer path · lesson 84 of 124
    The idea
    Context compaction is like a smart meeting note-taker: instead of transcribing every word, they capture decisions, action items, and open questions. When the notebook fills up, the transcript gets replaced by a crisp summary — you lose the verbatim banter but keep everything that matters for the next step.
    How to use
    Fill the context window and trigger compaction to see rolling summarization in action — the oldest turns collapse into a 500-token summary block. Then open the Strategies tab to compare all five techniques side-by-side.

    Add agent turns until the window fills. When usage passes the 75 % threshold, compaction kicks in and older turns are replaced by a summary.

    600 / 8,000 tok
    8% of context window
    0▲ compact threshold (75 %)8 k tokens
    Window contents
    system prompt (always kept)600 tok
    0
    compactions so far
    0
    turns in window
    0
    summaries in window

    Key insight

    Compaction fires at 75 % full rather than 100 % — leaving headroom for the model to think and for tool outputs to land. Waiting for a full window means the next turn overflows before compaction completes.

    Compact a long conversation

    Sequence

    Order the compaction pass.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    1. 1Drop a step here
    2. 2Drop a step here
    3. 3Drop a step here
    4. 4Drop a step here
    5. 5Drop a step here
    6. 6Drop a step here
    6 pieces left to place

    Test your knowledge

    20 questions
    Question 1 of 20
    Score: 0

    At what fill percentage do most systems trigger context compaction?

    The short version

    Context compaction is like a smart meeting note-taker: instead of transcribing every word, they capture decisions, action items, and open questions. When the notebook fills up, the transcript gets replaced by a crisp summary — you lose the verbatim banter but keep everything that matters for the next step.

    In more depth

    Why Context Compaction Exists

    Every LLM has a finite context window. As a conversation grows, costs rise linearly and latency climbs. Past a threshold (typically 75–80 % full), continuing becomes expensive and risky due to the "lost-in-the-middle" attention penalty. Compaction solves this by replacing older turns with a compact rolling summary.

    What Compaction Preserves

    A good compaction summary retains:

    The original task / goal
    Key decisions made and why
    Named entities (files, services, variable names)
    Errors encountered and their resolutions
    Current state of work in progress

    It drops:

    Verbose step-by-step reasoning
    Intermediate tool call dumps
    Repeated clarification back-and-forth
    Superseded plans and failed attempts

    A well-executed compaction can achieve a 90–97 % token reduction while preserving all decision-relevant information.

    Compaction Strategies

    Rolling summary — the default and most common. When the window exceeds a threshold, the oldest N turns are distilled into a ~500-token summary block. Simple and effective for most use cases.

    Sliding window — keeps the last k turns verbatim and discards everything older. Fast and predictable but loses important early context if the conversation spans many topics.

    Hierarchical / sub-agents — each sub-agent works in its own fresh context and returns a compact result to the orchestrator. Naturally bounded; each node never sees the full history.

    External memory + RAG — important facts are written to a vector store and retrieved on demand. Theoretically unbounded but adds retrieval latency and infrastructure complexity.

    Compaction + Prompt Caching

    The most cost-efficient architecture combines both:

    1.Stable prefix (cached): system prompt + tool definitions + few-shot examples — written once, cached at ~10 % read cost.
    2.Semi-stable block (cacheable between compactions): the rolling summary — byte-identical for every turn until the next compaction regenerates it, so it belongs inside the cached prefix and is read at cache price in between.
    3.Dynamic suffix (never cached): recent turns and the live query — always unique, but kept small by compaction.

    This yields cheap stable context and a bounded dynamic window simultaneously.

    Interview Questions

    Anthropic
    OpenAI
    Google
    Anthropic
    Cognition
    Anthropic
    OpenAI
    Anthropic
    Cognition
    Anthropic
    OpenAI
    Anthropic
    Cognition
    OpenAI

    Love this visualization?

    Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.

    100% free • No credit card required