Source: Hacker NewsView original ↗
Copyright remains with the original source. This site only collects, translates, or reformats the material.
Analysis and impact
Pi 网络中的压缩机制:一篇技术解读
压缩机制在分布式存储系统中扮演着关键角色。当系统持续写入数据时,底层存储会累积大量已删除或已覆盖的旧版本记录,这些"垃圾数据"如果不及时清理,不仅浪费磁盘空间,还会拖慢读取性能。文章从 Pi 的存储引擎设计出发,逐步拆解了压缩的触发条件、执行流程以及与并发读写之间的协调方式。原文未提供 Pi 项目的完整背景介绍,但从技术语境来看,其讨论的压缩策略与 LSM-Tree(日志结构合并树)类存储引擎的通用设计有诸多相通之处。
从 Hacker News 的讨论热度来看,社区对这类底层存储机制的关注度较高。48 条评论中,用户围绕压缩策略的取舍展开了交流——例如压缩频率与写放大之间的权衡、压缩过程中如何避免阻塞前台请求、以及不同工作负载下压缩参数的自适应调整等问题。这些讨论反映出,尽管压缩是存储系统的"幕后工作",但其设计质量直接影响着系统的整体性能表现。
值得注意的是,文章的发布时机与近期 arXiv 上多篇关于 AI 系统推理与决策机制的论文形成了一种有趣的呼应。虽然这些论文(涉及 LLM 的约束推理、自反思机制、规则遵守等)与 Pi 的存储压缩在技术领域上并无直接关联,但它们共同反映了当前技术社区对"系统内部机制如何影响外部行为"这一问题的持续关注。对于 Pi 而言,压缩机制的设计同样关乎系统在长期运行中的稳定性与可预测性,这与 AI 系统中对推理过程可控性的追求在方法论层面有着微妙的相似之处。
References
- Hacker News ↗
- Hacker News 讨论 ↗
- LLMs Know the Constraint But Do Not Use It: Activation Bottlenecks in Pragmatic Constraint Reasoning ↗
- What Drives LLM Self-Reflection? A Controlled Ablation of Uncertainty Routing in Armed Conflict Forecasting ↗
- Why Do AI Agents Break Rules? How Framing, Context, and Social Signals Shape Compliance ↗
Original source text
How Compaction Works in Pi
Date: Thu, 13 Aug 2026
From: Earendil Engineering [rfc@earendil.com](rfc@earendil.com)
To: You
Subject: 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.
An LLM conversation
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.
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 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.
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
Handling context overflow
When we cannot continue with the existing conversation as-is, we have two choices.
We can start a new, empty conversation without the accumulated context. This discards the history, including prior decisions and unresolved work. It might still be a good idea to do, because the performance of LLM outputs decrease as the context size grows.
We can create a smaller representation of the conversation context, since we want to keep this conversation going. That is what compaction does.
Compaction
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.
[system][tools][compaction result][user] ^ new message
Pi's implementation
Let's look more closely at how Pi specifically implements compaction.
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]
The number of retained messages varies because Pi uses a configurable token budget. Pi's current default of 20 thousand tokens comes out to roughly 5 to 20 turns. All the messages before this cut point are extracted and serialized, and will be summarized.
Pi's compaction prompt
The ideal outcome of a good summarization for a coding agent is like a handoff briefing from one shift to the next. Pi's compaction prompt focuses on the fact that there is a lot in the existing context that is no longer relevant. We should only keep around what is still important context for the next LLM request.
Pi therefore sends a different request for compaction than for regular conversation.
The system prompt used in the standalone compaction request is different. Instead of telling the LLM "you are an expert coding assistant", we tell the LLM "you are a context summarization assistant."
The user message in the compaction request is also different. It requests "a structured summary of this conversation branch for context when returning later." The prompt specifies sections for goal, progress and key decisions.
It's a standalone request that doesn't use any of the existing conversation history, which means it can use a different LLM model without incurring any unnecessary cost.
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.
Pi stores the compaction summary as plain text in the session. This keeps the compacted context readable and portable, since we can switch models in Pi and continue using the summary.
Compaction and prompt caching
Prompt caching is used by LLM providers to make repeated requests in the same conversation less expensive. 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.
Experiment
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 a custom compaction prompt.