Home / Chapter 4 · Context and knowledge
    Last edited · 7 min read

    Use with AI

    Context compaction

    Compaction replaces the old part of a conversation’s history with a summary, so that a long agent session fits in the context window. It is cheap when the same model writes the summary on the same prompt prefix before the cache expires: the history is then read from the cache, and you pay mostly for generating the summary.

    In plain wordsNotes from a long meeting. They are easiest to write straight away, while someone who was at the meeting still has all the material in front of them. Anyone who wasn’t at the meeting, or who comes back to it after the weekend, first has to reread all the rough notes. The next person reads only the notes and learns nothing that isn’t in them.

    When the harness compacts

    How to compact without paying for the whole history

    What survives compaction

    Check yourself

    An agent session is nearing the window limit. When and how would you compact the history so you don’t pay for it all over again?

    Compact rarely and in large chunks: at a threshold close to the window limit, or by hand once a task is done, and between compactions only append to the history. Send the summarisation call on the same prefix as the conversation: the same model, tools, system prompt and history, with the summarisation instruction at the end, so the history is read from the cache. Compact before the cache expires, because after a longer absence the summary pays full price for the whole history. Replace the old part of the history with one summary instead of cutting pieces out of it. Keep rules that always apply in the project file, because a summary loses details.

    Po polsku

    Kompaktujesz rzadko i dużymi porcjami: przy progu blisko limitu okna albo ręcznie po skończonym zadaniu, a między kompakcjami tylko dopisujesz nowe wiadomości na końcu historii. Wywołanie streszczające wysyłasz na tym samym prefiksie co rozmowa: ten sam model, narzędzia, system prompt i historia, a polecenie streszczenia na końcu, więc historia jest czytana z cache’u. Kompaktujesz, zanim cache wygaśnie, bo po dłuższej nieobecności za całą historię w wywołaniu streszczającym płacisz pełną cenę. Starą część historii zastępujesz jednym streszczeniem, zamiast wycinać z niej kawałki. Zasady obowiązujące zawsze trzymasz w pliku projektu, bo streszczenie gubi szczegóły.

    Follow-up questions (3)
    You return to a session whose cache has expired. Compact straight away?
    Usually yes, if you will keep working. The first request reads the whole history without the cache anyway, and after compaction every later turn reads tens of thousands of tokens instead of hundreds of thousands. Claude Code on the Pro and Max plans offers this itself when you resume a large session.
    How does clearing old tool results differ from compaction?
    Clearing keeps calls and messages verbatim and replaces old results with a short placeholder, so you pay nothing for a summary. But from the first cleared result onwards everything has to be written to the cache again, and the context often stays larger than after a summary. That is why clearing is done rarely and in large batches, or left to the API.
    Server-side compaction or your own?
    Server-side, when the provider offers it. In the Claude API on-demand compaction also reads the encrypted thinking, accepts your own summarisation prompt and can keep the latest turns with their thinking, and you send the returned block back unchanged. Your own works with any provider and model but cannot see encrypted thinking: with Claude models that you send thinking back to, it should replace the whole history with one message or keep the latest turns without their thinking blocks.

    Sources

    Report an error · Suggest a fix