Harness Notes

Compaction

Updated 2026-11-24,

On this page

What each harness does when the conversation outgrows the context window: when it compacts, what it keeps, and how you can steer it.

When it happens

Questionpichecked at 1.2.0
Automatic compaction, and when

Yes. Pi compacts automatically when contextTokens > contextWindow - reserveTokens, with reserveTokens defaulting to 16384 (so a 200K-token model compacts above roughly 183.6K tokens). Context is counted from the last valid assistant response's reported usage (totalTokens, or input + output + cache read + cache write) plus a chars/4 estimate for any messages added since. Pi checks before each new prompt, after a tool batch finishes and before the next assistant response in the same run, and again when the run ends.

Right after a compaction, Pi ignores the old (pre-compaction) usage figures and estimates the rebuilt context from scratch until the next response arrives.

Change the trigger or turn it off

Set compaction.reserveTokens (default 16384) in ~/.pi/agent/settings.json or <project>/.pi/settings.json; a larger reserve makes compaction happen earlier. Use compaction.modelOverrides keyed by exact provider/modelId to set a different reserveTokens or keepRecentTokens for one model. Turn auto-compaction off with "compaction": { "enabled": false } or the Auto-compact toggle in /settings. /compact still works when it is off.

compaction.enabled applies to all models and cannot be overridden per model.

Turning off auto-compaction also turns off automatic overflow recovery.

Since 1.4.0, reserveTokens controls when compaction starts, not the summary output cap. Compaction and branch summaries use the model output limit; generateSummary/WithUsage retain but ignore the old reserveTokens argument.

Compact by hand

Run /compact, or /compact <instructions> to tell the summary what to focus on. Pi adds your text to the summary prompt as Additional focus: <instructions>. In RPC mode, send {"type":"compact","customInstructions":"..."}. Extensions can call ctx.compact({ customInstructions }).

/compact fails with "Nothing to compact (session too small)" or "Already compacted" when there is nothing to summarise.

1.4.0 fixes custom instructions being lost from split-turn summaries; publisher-reported fix, not a passing preservation probe.

See how full the context is

The interactive footer shows context use as <percent>%/<window>, followed by (auto) when auto-compaction is on. The figure turns to a warning colour above 70% and an error colour above 90%, and shows ? right after a compaction until the next response reports usage. /session shows session statistics; RPC get_session_stats and the extension call ctx.getContextUsage() return the same contextUsage estimate that compaction uses.

What it does

Questionpichecked at 1.2.0
What replaces the conversation

Pi asks the current session model to write a structured checkpoint summary (Goal, Constraints & Preferences, Progress, Key Decisions, Next Steps, Critical Context, plus lists of files read and modified). It sends the older messages as serialized text with thinking blocks removed and each tool result cut to 2000 characters. Pi then appends a compaction entry, and later requests get the system prompt, then the summary, then the recent messages kept verbatim. On repeat compactions, the previous summary is updated rather than rewritten from scratch.

When a single long user turn exceeds the kept budget, Pi writes two summaries (earlier history and the start of that turn) and merges them.

An extension can supply the summary instead, using any model it likes.

1.4.0 removes the old fixed summary output cap. The caller uses createSummarizationOptions without a reserve-derived maxTokens; inherited provider/model output limits apply. No model request was run.

What survives

The system prompt is kept in full: context files such as AGENTS.md, tool declarations and loaded skill descriptions. It is stored as a checkpoint in the compaction entry and replayed ahead of the summary. Roughly the last compaction.keepRecentTokens (default 20000) of messages stay verbatim; cuts never separate a tool result from its call. Everything older survives only as the summary, which carries forward cumulative <read-files> and <modified-files> lists. There is no separate todo or plan store to preserve.

Skill bodies the model loaded during the conversation are ordinary messages, so older ones are summarised like anything else.

Extension state stored in session entries follows the session tree and survives compaction.

Trimming without summarising

Partly

Pi does not prune or clear old tool outputs on its own. The only automatic trimming is that tool results are cut to 2000 characters inside the text sent for summarisation. Extensions can trim without summarising by appending context_edit entries, which omit or replace earlier messages in future requests while keeping them on disk, or by filtering messages in a context event handler.

During overflow recovery, Pi itself appends context_edit entries to drop the failed attempt from future context.

The full history afterwards

Yes. Sessions are append-only JSONL files under ~/.pi/agent/sessions/--<path>--/, and compaction only appends a compaction entry. Every original message stays on disk and in /tree, /export and /share. The summary is stored in that entry's summary field and appears in the transcript as a collapsible block showing the token count before compaction; click it to expand. In 1.6.0, when showCacheMissNotices is enabled, the transcript displays the reasons OpenAI dropped reasoning items after compaction if the dropped count increased over the previous response.

Shaping it

Questionpichecked at 1.2.0
Customise the summary or hook in

Yes, through extensions. session_before_compact fires before manual, threshold and overflow compactions and can cancel or return your own summary, firstKeptEntryId and details. session_compact fires after success and session_compact_failed after a failure or abort. session_before_tree does the same for branch summaries. The built-in summary prompt cannot be changed by a setting; use /compact <instructions> or an extension.

The event payload includes reason (manual, threshold or overflow) and willRetry. JSON and RPC streams emit compaction_start and compaction_end events.

When a request overflows anyway

If the provider returns a context-overflow error, or a response stops early with stopReason: "length", Pi drops the failed attempt from context, compacts and retries once. A second failure shows "Context overflow recovery failed after one compact-and-retry attempt. Try reducing context or switching to a larger-context model." If a successful response pushes past the window, Pi compacts without retrying.

Recovery is skipped when compaction.enabled is false, or when the error came from a different model than the one now selected.

Custom providers with unusual overflow messages must map them to context_length_exceeded for recovery to trigger.

Notes and sources

pi, 21 sources
Automatic compaction, and when, 3
Change the trigger or turn it off, 5
Compact by hand, 5
See how full the context is, 4
What replaces the conversation, 5
What survives, 3
Trimming without summarising, 3
The full history afterwards, 4
Customise the summary or hook in, 3
When a request overflows anyway, 3