# 26.4 Compaction: by threshold and on demand (/building-ai-workers/context-engineering/compaction)

---
type: Document
title: "26.4 Compaction: by threshold and on demand"
description: "What a summary replaces and can lose, what goes outside the window first, and how to check a summary by threshold or on demand."
status: stable
order: 326.04
ksor:
  owner: team:panaversity
  audience:
    - public
  approval:
    by: process:panaversity
    at: 2026-11-30T08:16:23Z
chapter: '26'
part: III
expert_status: manager
concepts: [ "26.4" ]
last_verified: '2026-11-30'

sources:
  - id: anthropic-compaction-threshold
    title: Compaction at a token threshold (verified 2026-11-29)
    resource: https://platform.claude.com/docs/en/build-with-claude/compaction-threshold
  - id: anthropic-compaction-on-demand
    title: Compaction on demand (verified 2026-11-29)
    resource: https://platform.claude.com/docs/en/build-with-claude/compaction-on-demand
  - id: anthropic-compaction
    title: Compaction overview (Anthropic, verified 2026-11-29)
    resource: https://platform.claude.com/docs/en/build-with-claude/compaction
  - id: anthropic-managed-agents-reference
    title: Managed Agents reference, event types (Anthropic, verified 2026-11-30)
    resource: https://platform.claude.com/docs/en/managed-agents/reference
  - id: anthropic-memory-tool
    title: Memory tool (Anthropic, verified 2026-11-29)
    resource: https://platform.claude.com/docs/en/agents-and-tools/tool-use/memory-tool
generated:
  at: 2026-11-30T08:16:23Z
  by: esl-rewrite/1.2.0+ksor.1
trust_tier: unverified
build_id: sha256:d6ce3f28a2cf8e492a60bae1b426b7cc6466e35e83d70c3b9763db2c55009f4b
dirty: true
ksor_version: 0.0.60
---

**In everyday life.** Before an exam, a student copies a thick notebook onto one page and puts the notebook away. From then on, the student studies from the page alone. A formula that is also in the textbook is safe, because the textbook is still on the shelf. A hint the teacher gave once in class was written only in the notebook. If the page left it out, the hint is gone, and the student does not know that anything is missing. In this concept, the page is the summary, the textbook is the worker's instructions, and the hint is a rule that was said only in the conversation.

**What you will learn.** You practice this in Lab 26, Task 4.

- What does a summary replace, and what does it leave alone?
- What can a summary lose?
- What goes outside the window before a summary comes?
- What is the difference between "by threshold" and "on demand"?
- What must the summary's instructions say?
- How do you check a summary?

**Why it matters.** A summary is a lossy copy, a copy that loses detail, of whatever has no other home. The worker does not report what its summary dropped. It continues from the summary as if nothing were missing.

**At work.** In Friday's run a summary dropped six words, and Monday's trace placed the loss straight after it (26.2).

1. **You ask where that rule was.** "For each" was only in Dave's sentence (the overview). The instructions ask for a citation, and one anywhere in the answer passes them. Dave's sentence was in the history.
2. **The fix comes before any cut.** "For each" goes into the instructions. The worker writes each decision to the progress file. The summary is told what to keep. The next run passes the check on every invoice.

<!-- OPEN: the turn and the token count at the summary, how many invoices fail the check, and both runs' costs are blanks in the spec's worked run until the run is recorded (spec, raised point 8). If a live run does not lose the rule, the kit's trace is scripted and this page must say so. -->

### What does a summary replace, and what does it leave alone?

**It replaces the older turns of the conversation, and it leaves the instructions alone.** **Compaction** is the exam guide's word for it: "Compaction replaces the older turns of a conversation with a summary that Claude writes on the server".[^anthropic-compaction] The summary comes back as one block. After it, "The API automatically drops all content blocks prior to the `compaction` block". So the model reads the instructions, the tools, the summary and the turns that came later.[^anthropic-compaction-threshold]

So the instructions are as whole at turn 60 as at turn 1, and the history is not. In the run above, the worker never offered to pay an invoice after the summary, because that rule is in its instructions.

Brightline's hosted worker from Chapter 23 has had compaction on from the start. In Managed Agents, Anthropic's hosted platform, compaction is "Always on".[^anthropic-compaction-threshold] A short run may never reach it. When it does, the session's events show one of the type `agent.thread_context_compacted`.[^anthropic-compaction-threshold][^anthropic-managed-agents-reference]

### What can a summary lose?

**Anything that exists nowhere else.** Four things are known to be lost.

- *A rule said only in the conversation.* Dave's "for each" is one. So is the first message of the morning triage, "cite a policy line for each". So is a correction that the loop of 23.16 sends back after a failed check. Each is a message in the history.
- *A system message sent mid-run.* [25.2](../prompts-and-structured-output/system-or-user.md) gave it the authority of the system prompt. It lasts only as long as the history. On a summary on demand, such messages "are summarized too, so their text instructions stop applying once the block replaces them". The vendor's fix is to "state it again in a `role: "system"` message", sent right after the next new user turn.[^anthropic-compaction-on-demand]
- *Images and documents.* On a summary on demand, those inside the summarized turns "are gone once the block replaces them".[^anthropic-compaction-on-demand]
- *The model's earlier thinking.* It is not carried forward, "so the summary is all the model has of that earlier work".[^anthropic-compaction-threshold]

<!-- OPEN: do a mid-run system message, images and documents go the same way in a summary by threshold? The two lines are from the on-demand compaction page, and the saved threshold page has no line on any of the three, so this page states both for a summary on demand only. Check the threshold page on the day this page is written. If it says the same, drop "On a summary on demand" from both bullets. If not, keep it. -->

### What goes outside the window before a summary comes?

**Each rule goes into the instructions, and each decision goes into the progress file.** Then the summary has nothing to lose that matters.

*Rules.* A rule goes where every request carries it again. For a harness that is its instruction file, and [26.8](how-todays-tools-realize-it.md) gives each product's.

*The state of the work.* It goes in the progress file ([26.6](memory-outside-the-window.md)), which opens with the request, word for word.

One question stays open after a summary: what brings the progress file back into the window? Either the worker's instructions tell it to view the file after each summary, or your code sends the file. The vendor's page promises only that Claude "checks its memory directory before starting a task".[^anthropic-memory-tool] In Lab 26, Task 4, you test which one your run needs.

<!-- FIGURE 26.3: Two columns, with one arrow between them pointing left to right, labeled "the summary replaces the older turns". Left column, titled "The window before the summary", top to bottom: "instructions", "tools", "the request: ... with the policy line for each", "tool results", "the worker's turns: its decisions". Right column, titled "The window after the summary", top to bottom: "instructions (unchanged)", "tools (unchanged)", "one summary block", "the latest turns". An arrow labeled "unchanged" runs from the left "instructions" box to the right "instructions" box. No arrow runs from the left "request" box to anything on the right. To the right of the right column, a box outside the window titled "the progress file", showing two lines: "the request, word for word" at its top, then "decision 1, decision 2, ...". One arrow points from the progress file into the right column, labeled "read again: the worker views it, or your code sends it". Below: "Whatever has no other home is only as safe as the summary." The alt text must say that the instructions are unchanged, that no arrow runs from the old request, and that the request and decisions come in from the progress file. -->

*Figure 26.3. After a summary, the instructions are unchanged, and the request and the decisions come back from the progress file, not from the old turns.*

<!-- OPEN: who reads the progress file again after a summary is not verified: the worker, told by its instructions, or the builder's code. Figure 26.3's label names both until a run settles it. The full note is the OPEN comment at Lab 26, Task 4. -->

### What is the difference between "by threshold" and "on demand"?

**"By threshold" means the API or the harness decides when. "On demand" means your code or a person decides.** The size at which it happens is called the trigger.

| | By threshold | On demand |
| --- | --- | --- |
| Who decides the moment | "The API, when input tokens reach the trigger you set"[^anthropic-compaction] | "You, by sending a request"[^anthropic-compaction] |
| What your code does | Sends the conversation back as usual. The API drops what came before the block | Swaps the summarized messages for the block |
| Its risk | The summary comes at a moment nobody chose | A wrong swap that gives no error |

On demand, the swap is the one rewrite of the array that the API asks for. It still keeps the rule of 21.5. The block goes back unchanged ("Send it in future requests exactly as it came."), and nothing else in the past is edited. Two mistakes in the swap pass with no error. If the block is left out, "Claude gets no summary". If the summarized messages stay after the block, "the API sends them to Claude again".[^anthropic-compaction-on-demand]

A summary on demand and the clearing of [26.3](tool-output-pruning-and-context-editing.md) cannot share one request.[^anthropic-compaction-on-demand]

### What must the summary's instructions say?

**What to keep, and not to call tools.** Your own instructions replace the default prompt completely. So they must do its whole job: "say what the summary must retain and tell the model not to call tools."[^anthropic-compaction-on-demand] For the review, that is the request word for word, the invoices already decided, and where the progress file is. The second half matters because a summary "might fail when tools are defined". The model tries to call a tool, and the block comes back empty.[^anthropic-compaction-threshold]

These instructions are a second defense, and never the home of a rule. A summary that is told to keep a rule can still drop it. The worker's instructions always carry it.

### How do you check a summary?

**Check that it arrived, read it, and then test the worker after it.**

1. **Did it arrive?** A summary can fail to come back, so "check `stop_reason` before you look for the block".[^anthropic-compaction-on-demand] The stop reason is what shows a missing summary. The worker will not tell you.
2. **What did it keep?** The block "holds the summary as text you can read".[^anthropic-compaction-on-demand] Read it for the request, the rule and the decisions.
3. **Does the worker still hold the rule and the state?** Ask the session, after the summary, for its rule on policy lines and for the invoices it has decided.
4. **Does the result pass?** Run the end check on every invoice. Only this step is proof.

A summary has a price of its own. It is one more call to the model, which the vendor calls "an additional sampling step, which contributes to rate limits and billing".[^anthropic-compaction-threshold] Section 26.2 showed where the usage reports it. A run short enough to finish without one should have none. And when the instructions and the progress file are enough to start again, a fresh session may work better than a summary ([26.7](when-to-start-a-fresh-session.md)).

**The operating rule:** before a summary can come, give every rule and every decision a home outside the window. Then tell the summary what to keep, and check it with code.

**Do this now.** Open a long session in your coding agent. Send: "List every rule I gave you in this conversation. For each one, say whether it is in an instruction file or only in a message." Move each rule that is only in a message into the instruction file.

[^anthropic-compaction]: Compaction overview, Anthropic.
[^anthropic-compaction-threshold]: Compaction at a token threshold, Anthropic.
[^anthropic-compaction-on-demand]: Compaction on demand, Anthropic.
[^anthropic-memory-tool]: Memory tool, Anthropic.
[^anthropic-managed-agents-reference]: Managed Agents reference, event types, Anthropic.
