// HACKER NEWS — CYBERSECURITY
How Compaction Works in Pi
If you have ever had a long coding session in a coding agent like Pi, Claude Code, or Codex, you will have triggered a compaction.
In this post we explain how compaction works and when Pi needs to compact.
Large language models (LLMs) have limited context windows.
The context window is what the model can "see" while producing a response.
The transformer architecture used by LLMs limits how much input they can process.
The input for a coding agent session includes all the previous messages and tool calls, and this keeps growing as you work.
Once it exceeds the context window, the LLM rejects the request.
When working interactively with a coding agent like Pi, the agent sends requests to an LLM and receives responses.
Each request includes a system prompt, loaded files such as AGENTS.md, tool definitions, and the conversation history.
A coding agent's first LLM request contains this initial context, along with a first user message.
This starts a turn.
The LLM may first return an assistant message containing tool calls.
The agent program executes them and sends a new request to the LLM containing the complete conversation, now including the tool results.
We get back another assistant message.
The turn is finished when the assistant has completed generating output.
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.
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.
Compaction replaces part of the history with a compressed representation, leaving room for additional messages and tool calls.
Let's look more closely at how Pi specifically implements compaction.