Skip to content
mentoor

Topic 5 of 5 · 5 min read

Built-in automatic compaction and context editing

Goal: Predict what an automatic compaction will lose and add instructions or notes that protect the details that matter.

Many tools compact a long session for you, but the summary is written by a model that does not know which details you care about, so you must steer it and protect what matters.

Check yourself · 1 of 3

Which detail is most likely to be lost in an automatic summary?

Choose one answer

Why it matters

Chat apps and coding agents often handle a full window on their own. When the session gets close to the limit, they replace older messages with a summary and carry on. This saves you from a hard stop. The risk is silent loss: a rule, a number or a decision that was important to you but looked minor to the summariser can disappear without any warning.

What exists

  • Automatic compaction. The tool summarises older turns when the window fills. Some coding agents also offer a manual command that compacts on request, often with a hint for what to keep.
  • Clearing. A command that wipes the conversation and starts clean, keeping project instruction files that reload each session.
  • Context editing in APIs. Developers can have old tool results or thinking blocks cleared from a request while recent turns stay. Anthropic documents this, and other vendors have similar session-trimming helpers.
  • API compaction. The API can produce the summary for you, and you can supply your own instructions for what it must keep.

What a summary tends to lose

Exact values (an error code, a version, a price), the reason behind a decision, rules given early in the session, and the file or document names. It tends to keep the broad story. So protect the exact and the early.

How to steer it

  1. Put lasting rules in a file that reloads each session, such as a project instruction file, not in the chat. A file is read again after compaction; a chat message may be summarised away.
  2. Compact on your terms. Do it at a natural break, with a hint: "Keep the decision list, the failing test name and the exact API error text."
  3. Write notes before it happens. Update the progress file, then compact.
  4. Check afterwards. Ask the assistant what it now knows about the goal and the decisions. Fix any gap straight away.

Before and after

Before: Let the tool compact at the limit. The summary says "we fixed the login problem". The exact cause and the rule "never edit the legacy table" are gone.

After: Before compacting: "Update progress.md with the cause of the login bug and the rule about the legacy table." Then compact with the hint "keep progress.md content and the open test failures".

What goes wrong

  • Relying on automatic compaction as the only memory.
  • Putting rules only in chat, where compaction can drop them.
  • Never checking what the summary kept.

Examples

A compact command with a hint

/compact Keep the list of decisions, the name of the failing test (test_refund_rounding) and the exact error text. Drop everything about the earlier CSS discussion.

The hint names what to protect and what to drop. The exact command name differs by tool, so check yours.

Context editing settings in an API request

{
  "context_management": {
    "edits": [
      { "type": "clear_tool_uses_20250919", "keep": { "type": "tool_uses", "value": 3 } }
    ]
  }
}

This shape clears old tool results and keeps the latest few. Field names change between releases, so read the current docs before copying it.

Key points

  • Automatic compaction replaces older turns with a model-written summary, so details can vanish silently.
  • Exact values, reasons for decisions, early rules and file names are the details most likely to be lost.
  • Lasting rules belong in a file that reloads each session, not only in chat.
  • Compact at a natural break with a hint about what to keep, after updating your notes.
  • After any compaction, ask what the assistant now knows and fix gaps right away.

Common mistakes

  • Treating automatic compaction as a safe backup for important rules.
  • Keeping rules only in the chat instead of a reloaded file.
  • Compacting in the middle of a delicate step instead of at a natural break.
  • Never checking what the summary kept.

Check yourself · 2 of 3

Where should a rule such as "never edit the legacy table" live so it survives compaction?

Choose one answer

Check yourself · 3 of 3

You are about to compact a long coding session. What is the best order?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic