HN Debrief

How Compaction Works in Pi

  • AI
  • Developer Tools
  • Infrastructure
  • Open Source

The post lays out Pi’s approach to keeping long-running agent sessions alive once the model’s context window fills up. Pi keeps a chunk of recent turns, sends older history to a separate compaction step with a structured prompt, and replaces that old history with a lossy summary meant to preserve goals, decisions, and state. The article is practical rather than deep. It explains the basic mechanism, not a new algorithm.

If you are building agent workflows, compaction is not a solved UX problem. Treat it as a systems problem involving context policy, prompt caching, hardware, and tool-noise isolation, not just a better summary prompt.

Discussion mood

Mostly pragmatic and mildly dissatisfied. People accept compaction as necessary, but they see current approaches as lossy, expensive, and constrained more by prompt caching and hardware realities than by prompt design.

Key insights

  1. 01

    Prompt caching blocks clever compaction

    Prompt caching makes many elegant context-management ideas uneconomical. If you compact or prune aggressively, you keep forcing cache misses on the conversation prefix and pay again in latency and tokens. That shifts the design target away from “best summary” and toward “fewest history rewrites.”

    Measure cache hit rate before shipping any dynamic context scheme. A weaker compaction policy that preserves the cached prefix can beat a smarter one on both cost and responsiveness.

      Attribution:
    • pjm331 #1
    • skeledrew #1 #2
  2. 02

    Tool noise should be isolated first

    Most of the value is in preserving user intent, decisions, and active plan, not raw tool chatter. MCP traces, test runs, and codebase spelunking are the easiest tokens to throw away. Pi’s extension hooks and subagent pattern both point to the same architecture: keep noisy execution details in bounded side channels instead of mixing them into the main conversation.

    Separate tool transcripts from the primary agent state early. If your system dumps every tool artifact into the main thread, compaction quality will never fully save it.

      Attribution:
    • damsta #1
    • flexagoon #1
    • alansaber #1
  3. 03

    Local stacks can hide compaction latency

    Running your own inference stack opens tricks hosted APIs do not. You can pause generation, rewrite token regions, rebuild the KV cache, or even use a ping-pong setup with two KV caches so one summarizes while the other keeps moving. That does not remove the prefill hit, but it turns compaction from a blocking UX cliff into an engineering problem you can optimize.

    If long-lived local agents matter to your product, invest below the prompt layer. Harness-level KV cache management may buy more than another round of compaction prompt tuning.

      Attribution:
    • novaRom #1
    • storus #1
  4. 04

    Pruning can preserve intent better

    A full summary is not always the safest compression method. Replacing clearly bounded task regions with a detailed task summary, while keeping the rest of the transcript intact and optionally retrievable, can preserve conversational intent better than flattening everything into one narrative blob. The practical point is not “pruning versus compaction.” It is keeping irreversible loss localized.

    Prefer bounded replacement over whole-history summarization when your users revisit earlier reasoning. Preserve an escape hatch to the removed material if the model needs to recover detail.

      Attribution:
    • kierangill #1
    • spott #1
  5. 05

    There is only one live summary

    Pi’s setup is simpler than some readers expected. It does not keep an ever-growing stack of summaries in the active prompt. It keeps one current summary plus recent raw turns, and each new compaction folds the previous summary into a fresh one. That avoids summary-on-summary sprawl, but it also means the retained history is intentionally lossy and keeps getting rewritten.

    Do not assume compaction gives you archival memory. If some facts must survive intact, store them separately from the rolling conversation summary.

      Attribution:
    • randomblock1 #1
    • skeledrew #1 #2
    • alansaber #1

Against the grain

  1. 01

    Discarding the KV cache is needless

    Throwing away the whole KV cache during compaction was called out as a self-inflicted cost. The argument is that a model should be able to summarize inside the current session without needing a fresh system prompt and full cache reset. That challenges the default assumption that clean-room compaction is always worth the prefill penalty.

    Benchmark in-place summarization against separate compaction calls on your stack. Clean separation is not automatically the best trade if cache rebuild dominates cost.

      Attribution:
    • pornel #1
  2. 02

    Use provider-native compaction when available

    If Pi is often paired with OpenAI, the better answer may be to lean on OpenAI’s dedicated compaction endpoint instead of reproducing the behavior with prompts. That reframes Pi’s built-in method as a portability baseline, not the optimal path for every backend.

    When targeting a specific model provider, check for native memory or compaction features before building your own abstraction. You may get lower cost and better behavior by using the vendor path directly.

      Attribution:
    • searealist #1
    • brandall10 #1

In plain english

context window
The amount of text or other input a model can consider at one time while generating an answer.
KV cache
Key-value cache, the stored attention state that lets a language model generate long outputs efficiently but consumes a lot of memory.
MCP
Model Context Protocol, a way for AI models to interact with external tools and systems.
OpenAI
An artificial intelligence company that provides language models and related APIs.
prefill
The stage where a model processes the input prompt and context before it starts generating output tokens.
prompt caching
A pricing and performance feature where repeated prompt content is stored so later requests can be billed more cheaply and processed faster.
subagent
A secondary agent process spawned by a main agent to handle part of a task in parallel.

Reference links

Pi and related implementation references

  • Pi task compaction experiment
    Example project that marks task regions and replaces them with detailed summaries instead of compacting the whole history at once.
  • Pi compaction documentation
    Docs cited to show Pi supports extension hooks that can customize what gets compacted or preserved.
  • Pi compaction source code
    Source link used to summarize Pi’s default behavior of keeping recent turns and compacting older history with a structured prompt.