Every coding agent that runs long enough hits the same wall: the conversation outgrows the context window. The answers differ, and the differences are load-bearing for anyone who depends on an agent remembering what it was told four hours ago.
Pi's answer, documented on docs.pi.dev/latest/compaction and /latest/settings, is compaction: replace the older part of the conversation with a model-written summary and keep a recent tail verbatim. What makes the documentation worth reading is that it is specific about the boundary, the cut rules and the failure modes — and revealing about what it does not promise.
The trigger is free space, not fill percentage
The rule is one comparison:
contextTokens > contextWindow - reserveTokens
Default reserveTokens is 16,384. So compaction fires when fewer than about sixteen thousand tokens of room remain, whatever the window's size. That is deliberately not a percentage, and the consequence shows up immediately: on a one-million-token model, sixteen thousand tokens of headroom is a hair's breadth, and a single large tool result can overshoot it between checks.
The documentation's own worked example makes the point by overriding it — a 1M-context model configured with reserveTokens: 400000 compacts above 600K tokens. There is a settings key for exactly this, compaction.modelOverrides, keyed by exact provider/modelId. Its existence is an admission that one reserve value cannot be right across window sizes.
The check runs in four places: between turns of an agent run (after tool results are appended, before the next assistant response), before a new user prompt, and after the run as overflow recovery. A provider context-overflow error, or an early stopReason: "length", can trigger one compact-and-retry.
Four settings, not three
All nested under a compaction object, in ~/.pi/agent/settings.json or a project's .pi/settings.json. Project values override user values; values must be non-negative safe integers; no CLI flags are documented for any of them.
| Key | Default | What it does |
|---|---|---|
compaction.enabled | true | Turns automatic compaction on or off. Manual /compact still works when disabled. |
compaction.reserveTokens | 16384 | "Free context tokens below which automatic compaction starts." |
compaction.keepRecentTokens | 20000 | "Recent tokens retained without summarization." |
compaction.modelOverrides | none | Per-model reserveTokens and keepRecentTokens, keyed by exact provider/modelId. |
Branch summaries are a separate path with their own branchSummary.reserveTokens (16,384) and branchSummary.skipPrompt (false).
The boundary, and why it is not where you set it
The algorithm walks backwards from the newest message, accumulating token estimates until keepRecentTokens is reached. That point becomes firstKeptEntryId. Everything before it is summarized; everything from it onward goes to the model verbatim.
But the boundary cannot land anywhere. The documentation is explicit: "Valid cut points are" user messages, assistant messages, BashExecution messages and custom messages, and "Never cut at tool results (they must stay with their tool call)." So the retained tail is not 20,000 tokens — it is the nearest valid cut point at or before 20,000 tokens, which means a conversation full of large tool results keeps a different amount than one full of short exchanges.
There is a second-order case: when a single user-message span is itself larger than the budget, the cut lands at an assistant message instead, two summaries are generated, and they are merged.
The summary is written from an already-lossy transcript
This is the detail most easily missed. Before the summarizer sees the conversation, two things are stripped:
Thinking blocks are omitted entirely. Whatever reasoning justified a decision is not merely absent from the summary — it is absent from the summary's input.
Tool results are cut at 2,000 characters, with a marker showing how much was removed.
So a summary of a session that read a large file is written from a 2,000-character window onto that file. The output has a fixed shape — Goal, Constraints & Preferences, Progress, Key Decisions, Next Steps, Critical Context, plus <read-files> and <modified-files> lists — and the file lists accumulate across successive default compactions. They do not accumulate from extension-generated summaries.
Nothing in the documentation guarantees that any particular content survives. Not file contents, not the original task description, not tool results. The structured sections are a template the model is asked to fill, not a retention contract.
Who writes it, and what you cannot change
The documentation says only "Call LLM to summarize with structured format." It names no model. Summary requests "use the model's output limit, reduced when needed so input and output fit the context window," and they disable prompt-cache writes. The summarization prompt itself is not published.
There is no configuration key for a custom summarizer prompt or a custom summary model. The only override path is an extension: a session_before_compact hook can cancel compaction outright or return a custom compaction object, and the docs show substituting a different model via serializeConversation. Manual compaction takes optional instructions — /compact [instructions], which "focus the summary" — and an extension receives them as customInstructions. No default-instructions key is documented, and no example instruction text is given.
The claim the documentation does not support
A natural piece of advice follows from all this: put the rules that must never be lost into a persistent context file, because compaction cannot touch it. The appeal is obvious — a summary keeps the gist and drops the specifics, so anything that must survive verbatim should live outside the conversation.
Neither the compaction page nor the settings page mentions AGENTS.md, CLAUDE.md, or any other context file. Not where they are found, not their precedence, not whether they survive compaction or are reloaded afterwards. The only support is indirect: the "What the LLM sees" diagram shows system prompt, then summary, then messages from firstKeptEntryId — and even that is a diagram, not a sentence. The documentation never states in words that the system prompt is exempt.
The advice may well be correct. Configuration and Sessions pages exist and are not covered here. But it is an inference from a diagram, not a documented guarantee, and that distinction matters for anyone about to move their project's invariants into a file on the strength of it.
Stated failure modes
The documentation is unusually forthcoming here, and each line is a real operational edge:
"If recovery compaction fails or is cancelled, Pi keeps the omission edits, appends no compaction, and schedules no internal retry." A failed recovery leaves the session in the trimmed-but-unsummarized state, with nothing queued to try again.
"Compaction already in progress uses the model and settings captured for that operation." Changing settings mid-compaction does not take effect until the next one.
Omitted raw entries "remain stored but do not affect cut selection, summaries, checkpoints, or token estimates." They are on disk and out of the loop.
File lists are not carried forward from extension-generated summaries.
notices.compactionCosts, defaultfalse, will show what each compaction cost.
No typical reclaim figure, no latency, and no cost estimate appear anywhere.
The design choice worth comparing
Set against the other common approach to the same problem, the shape of this one is clear. Some harnesses handle oversized context by relocating it: a large tool result is written to a file, a preview and a path are left in the conversation, and the tool used to read files is explicitly exempted from eviction so the agent can go and fetch what was moved. The content stays addressable.
Pi summarizes instead. The raw entries are retained on disk but, per the documentation, do not affect summaries, checkpoints or token estimates, and no documented mechanism lets the agent retrieve them mid-session. The trade is legible: relocation preserves detail at the cost of a retrieval step and the care needed to keep the retrieval path alive; summarization gives a compact, structured handover and accepts that specifics are gone.
For anyone operating a long-running agent, the practical reading is narrower than the design debate. Raise reserveTokens well above the default on a large-window model, because sixteen thousand tokens of headroom is one big tool result away from an overflow error. Expect the retained tail to differ from keepRecentTokens, because tool results pin their call sites. Assume reasoning and long tool outputs are gone from the summary's input, not just from its output. And verify independently — not from the compaction documentation — whether your context files actually survive, because that page does not say.