Install
Install with PiG
pig install npm:pi-foldInstall with Pi
pi install npm:pi-foldPackages run code with your permissions and influence agent behavior. Catalog inclusion is not a security audit. Review the source before installing a third-party package.
Pi manifest JSON
The pi field of package.json.
{
"extensions": [
"./extensions"
]
}README
Written by the package's publisher, shown as published in version 4.3.3 (README.md). PiG does not review it.
pi-fold
Lossless context folding for Pi. pi-fold rotates stale context out of the window and into short briefs, while the exact originals stay on disk one tool call away. It runs automatically and batches its rewrites so the prefix cache stays warm.

Install
pi install npm:pi-fold
Requires Node 22+ and Pi 0.83+ (Pi 1.0 needs Node 22.19+). MIT licensed.
It is meant to be paired with a long-term memory store such as pi-canon. Folding keeps the window lean; the store keeps what the session learned. That pairing is what the results below measure.
How it differs from compaction
Compaction replaces the transcript with a summary and discards the originals. It is simple, it works well, and it is a sound default for most sessions. Folding is an alternative shape: it replaces old spans with briefs and keeps the originals retrievable.
On a 64-stage coding assignment over the curl codebase, followed by a 16-question recall test about the whole session:
| Setup | Recall | Cost | Wall clock |
|---|---|---|---|
| Compaction | 4 / 16 | $151 | 6.9h |
| Compaction + memory store | 14 / 16 | $202 | 9.0h |
| pi-fold + memory store | 14 / 16 | $101 | 5.2h |
In this test the pairing matched the recall of compaction with a memory store at about half the cost. The savings come from the cache: folding batches its rewrites so the provider replays the cached prefix instead of rereading the session from scratch, and roughly 95% of input in the pi-fold run was served from cache.
This is a small number of runs on one task, aimed at two places where summary compaction looked weakest: cache cost and recall of detail. Read it as evidence on those two points, not as a claim that folding is the better approach overall.
Full write-ups, data and logs are on Zenodo (design, working memory, retrieval, rotation and retention), with friendlier versions on Medium and shaneconner.com.
What you get
- Automatic folding. Completed work is marked as it finishes and folded when the window fills up. The agent doesn't have to do anything.
- Lossless recall. Every brief links back to the original messages, verified by hash. The agent can
peekat them orexpandthem back in place. - A cache-friendly cadence. Marks cost nothing. All of them land together in one commit, so the cache is spent once per commit instead of on every change.
- A clean chat. Folded spans collapse into a single muted row in the chat, so your screen matches what the model actually sees.
- The fold bar. A compact usage bar above Pi's footer shows how full the window is and what's in it, from deeply folded to still raw.
Commands
| Command | What it does |
|---|---|
/fold-status | Detailed token and fold figures |
/fold | Commit every pending mark now |
/fold-editor | Interactive map of the window, for reading inside folds |
/fold-settings | Edit options live; saved to ~/.config/pi-fold/settings.json |
The agent gets one tool, pi_fold_context, with actions to status, peek, expand, refold, pin and unpin content, plus brief, reboundary and unmark for curating pending folds. It's all optional; a session that never calls the tool still folds.
Configuration
The defaults are the benchmarked setup, so there's nothing you need to tune. To change one, use /fold-settings or edit ~/.config/pi-fold/settings.json, which pi-fold reads when pi starts. providerInputBudget and blacklistAutoFoldTools aren't in the settings file; pass them from your own extension with registerPiFold(pi, options).
| Option | Default | What it controls |
|---|---|---|
thresholds | { maxTarget: 0.80, minTarget: 0.20, consolidateAfter: 10, minFoldChars: 8000 } | When a commit fires (maxTarget), how far it folds down (minTarget), when folds consolidate, and the smallest span worth folding |
providerInputBudget | from Pi's context window | The token budget folding works within |
blacklistAutoFoldTools | new Set() | Tools whose results should never be auto-folded |
toolFoldThreshold | 0.50 | Oldest share of the window where tool results are clipped in view (0 turns it off) |
preCommitNotice | false | Tell the agent when a commit is approaching |
noticeLeadShare | 0.10 | How early that notice arrives |
palette | { map: "batlow", start: 0, end: 1 } | Fold bar colours: batlow, viridis, plasma, magma or cividis |
How it works
Prefix caches are positional. A provider replays the longest byte-identical prefix of a prompt, so changing one early byte invalidates everything after it. pi-fold only rewrites the window at a commit; everything else is a pure append.
Marks, then commits. As each tool batch or chapter finishes, the runtime marks it as a fold candidate. A mark lives outside the window, so it changes nothing and costs nothing. When the window reaches maxTarget, a single commit folds the stalest marks into briefs until it's back down to minTarget, leaving recent work raw.
Peek and expand. peek appends a verified, plain-text view of a fold's originals (without thinking blocks or provider metadata) for one turn, then steps aside. expand restores the original messages in place until the fold is refolded.
Plays well with Pi. Pi's own compaction stays enabled underneath. Threshold compaction is skipped because folding gets there first, overflow is recovered by folding and retrying, and manual /compact still works as an escape hatch.
Status
pi-fold is shared as a working tool and a set of results. It is not in daily use by its author at the moment, so issues and pull requests are answered as time allows.
Prior work
pi-fold builds on lossless hierarchical compaction (LCM, arXiv:2605.04050) and agentic context management (Letta, arXiv:2510.11967); see also arXiv:2607.00692. Its contribution is the cache-aware commit: stage every fold as a free mark, then pay for the rewrite once.