How Compaction Works in Pi Pi, a coding agent from pi.dev, implements compaction to manage LLM context window limits by summarizing older conversation content while preserving recent messages. Compaction triggers automatically when the context nears its limit or manually via the /compact command, and it helps reduce request costs and context rot. If you've ever had a long coding session in a coding agent like Pi https://pi.dev , Claude Code, or Codex, you will have triggered a compaction. That is because large language models LLMs have limited context windows https://en.wikipedia.org/wiki/Context window . The context window is what the model can "see" while producing a response. Transformer architecture https://en.wikipedia.org/wiki/Transformer deep learning limits how much input an LLM can process. The input for a coding agent session includes all the previous messages and tool calls. Hence LLMs reject requests that exceed the context window. In this post, we will discuss when compaction is needed, and how it works in Pi. Compaction is also a useful tool for managing the size of the context window size, which both help reduce the cost of LLM requests and reduce context rot https://www.trychroma.com/research/context-rot . When working interactively with a coding agent like Pi, the agents and LLM exchange messages. Each request to an LLM contains initial context including a system prompt, as well as some additional input. This is typically files loaded into the context such as AGENTS.md , and tool definitions. A coding agent's first LLM request contains this initial context, along with a first user message. request 1: system tools user This starts a turn. The LLM may first return an assistant message containing tool calls. The agent program executes them and sends their results back to the LLM, which can then return another assistant message. The turn is finished when the assistant has completed generating output. after request 1: system tools user assistant: tool call tool result assistant <------------------- ^ <--------- returned by LLM | returned by LLM | produced by the agent We continue working, and send another message. request 2: system tools user assistant: tool call tool result assistant user ^ new user message Each turn expands the conversation. Eventually, the history exceeds the context limit. The next request then returns an error such as Request exceeds the maximum size . system tools user assistant .... tool result user ^ exceeds context window When we cannot continue with the existing conversation as-is, we have two choices. In theory, there are many ways to implement compaction. For example, we can write a deterministic function which keeps some of what is in the conversation and discards the rest. In practice, though, implementations of compaction use an LLM request to summarize the conversation history. Regardless of the method, after compaction, the context should have been compressed such that we have room for many new messages and tool calls. system tools compaction result user ^ new message Let's look more closely at how Pi specifically implements compaction https://pi.dev/docs/latest/compaction summary-format . When conversations grow too long, Pi uses compaction to summarize older content while preserving recent work. Compaction is triggered when the context limit is nearing the total size of the context window. It can also be manually triggered using the /compact command. Pi checks for auto-compaction after a turn ends. Until then, each request extends the existing prompt and can reuse its cached prefix. Pi may also compact mid-turn, if it encounters a context overflow error. When compacting, Pi retains some number of recent messages unchanged. before compaction: system + tools older turns recent retained messages How many messages are retained vary by session, but it's determined by a configurable number of tokens https://pi.dev/docs/latest/compaction when-it-triggers . Pi's current default of 20 thousand tokens comes out to roughly 5-20 turns. All the messages before this cut point are extracted and serialized, and will be summarixed. To keep the compaction request within the context limit, Pi truncates tool call results in the history to 2,000 characters. If we didn't somehow reduce some of the history, we would already be above the context limit. Tool outputs are a reasonable place to cut because they have a more intermediate nature. The compaction request that Pi sends differs from regular conversational requests. The result of the compaction is appended to the Pi session as a compaction entry, and the session can now continue. After the compaction request, the context has been compressed. after compaction: system tools summary recent turns new user message There is now room in the conversation context for many more messages. Prompt caching https://earendil.com/posts/prompt-caching is used by LLM providers to make repeated requests in the same conversation more cost efficient. In an active coding session, we pay less for the context that has already been generated by the model. This caching requires an exact prefix match, so compacting a session will break the prompt cache. cached before compaction: system tools older history recent retained turns <-------------------- cached prefix -------------------- first request after compaction: system tools summary recent retained turns new user message <-- reusable -- ^ | first changed token | +-- everything after this point must be recomputed The retained turns contain the same tokens, but they now follow a different prefix. Their previous cached state therefore cannot be reused. New requests after compaction will benefit from prompt caching again. Since Pi is extensible and malleable, you can replace its compaction with your own. To test a different compaction mechanism, ask Pi to create an extension with your own custom compaction prompt.