Lesson 2 of 4 Context Management and Agent Memory

Compacting Context Without Losing Important Facts

This lesson describes compaction techniques, which reduce context in long conversations and runs while keeping the information the agent will need, when to trigger them and how to avoid losing important facts.

Learning outcomes

After completing this lesson, you will be able to:

  • Order compaction techniques from least to most lossy.
  • Trigger compaction by token threshold or stage boundary.
  • Apply safeguards, such as pinning, against information loss.

Compaction techniques

Several techniques reduce context, each with a different risk of losing information. The following list describes them from least to most lossy.

  • Tool result pruning: remove raw tool outputs after their relevant facts have been extracted into state.
  • Pinned state: keep a structured object of key entities, decisions and constraints that is always injected and never summarized.
  • Summarization: replace older turns with a summary produced by a model, generated from the original turns.
  • Sliding window: keep only the last N turns; safe only for content that is pinned or retrievable elsewhere.

The following diagram orders the techniques by how much they lose.

Compaction techniques from least to most lossy. Tool result pruning removes raw tool outputs once their key facts are in state. Pinned state keeps a structured object of key entities, decisions and constraints that is always injected and never summarized. Summarization replaces older turns with a model-written summary. A sliding window keeps only the last N turns, which is safe only for content that is pinned or retrievable elsewhere.
Combine techniques: pin, prune, then summarize.
Put in order

From least to most lossy

Exam skills 2.2.1

Arrange the compaction techniques from the least lossy to the most lossy.

Log in to try this activity Interactive activities are free with an account.

Triggering compaction

Compaction is triggered by a token threshold, for example when the context reaches 60-70% of the window, or at natural boundaries such as the end of a workflow stage. The application counts tokens from the usage returned by each call. Compaction runs before the next model call, and the compacted context is stored so that later calls use it. Frameworks provide hooks for this: Agent Framework lets applications control the messages passed to each run and supports context providers, and custom code can apply the same logic.

def compact(messages, state, limit_tokens=60_000):
    if count_tokens(messages) < limit_tokens:
        return messages
    old, recent = messages[:-8], messages[-8:]                 # keep the latest turns verbatim
    summary = summarize(old, keep=["order_ids", "amounts", "deadlines", "commitments"])
    return [system_message(state), {"role": "user", "content": f"<summary>{summary}</summary>"}] + recent
Spot the bug

A compaction function that loses the conversation

Exam skills 2.2.1

Find the lines that make this compaction function behave wrongly.

Log in to try this activity Interactive activities are free with an account.

Avoiding information loss

Compaction causes the failure modes covered later in the course: facts lost by sliding windows and details changed by repeated summarization. Safeguards include pinning identifiers, numbers, dates and commitments in structured state; summarizing from original turns rather than from earlier summaries; instructing the summarizer to preserve specified fields verbatim; and testing compaction with long conversations that check recall of early facts.

Select all that apply

Safeguards for compaction

Exam skills 2.2.1 · 3.2.2

Pick the safeguards that stop compaction from losing or changing important facts.

Log in to try this activity Interactive activities are free with an account.
Worked example

A long support session

A technical support agent handles sessions that last up to two hours. Near the end of long sessions, it forgets the customer's device model and the troubleshooting steps already tried, and repeats them.

The agent probably uses a sliding window. The fix is to pin the device model and a list of steps already tried in structured state, summarize older turns from the original messages when a threshold is reached, and keep recent turns verbatim. Simply increasing the window size would postpone the problem and increase cost.

Check yourself

What should be preserved word for word when a conversation is summarized?

Show answer

Identifiers, such as order numbers and account IDs, because a summary that changes them causes errors later.

Exam Tips

Remember for the exam

  • Prune tool results first, pin key facts, then summarize; use sliding windows last.
  • Trigger compaction at a token threshold or stage boundary.
  • Summarize from source turns and preserve identifiers verbatim.