← Catalog

@twkrash/pi-headroom

Headroom compression for Pi tool results: each result compressed once and replayed byte for byte, so the prompt cache stays intact. Originals stay in the session. Works with every provider and with subagents.

Extension

Install

Install with PiG

pig install npm:@twkrash/pi-headroom

Install with Pi

pi install npm:@twkrash/pi-headroom
Review before installing

Packages 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": [
    "./src/index.ts"
  ]
}

README

Written by the package's publisher, shown as published in version 0.2.1 (README.md). PiG does not review it.

Pi Headroom

Smaller tool results for Pi, without breaking the prompt cache.

Headroom compression for Pi — every provider, every subagent, each result compressed once and replayed byte for byte.

npm version

CI

GitHub release

license: MIT

node: >=22.19.0

What is Pi Headroom?

Coding agents spend most of their input tokens re-reading tool output: logs, JSON, search results, file listings. Headroom shrinks that output algorithmically, with no LLM call. On real tool output from Pi:

Tool outputBeforeAfterHow
1,500-line application log with one FATAL line12,816 tokens47 tokensrepeated INFO lines folded; the FATAL line kept
400-row JSON array13,880 tokens5,732 tokensrewritten as a table, nothing lost

Before each model call, Pi Headroom sends every new large tool result through your local Headroom proxy, once, and records the result in the session. Every later call replays that recorded text byte for byte, while the session keeps the original. Anything Headroom dropped stays one tool call away: the model calls headroom_retrieve with the hash from the Retrieve more: hash=… marker and gets the original back, from the proxy or, once the proxy has let it go, from the session.

caveman level: LITE | headroom -47.5k tok (89%) · 2 results | jev: auto

Why another Headroom extension?

Pi already has Headroom extensions. They all compress in Pi's context hook, which runs before every model call: the conversation is compressed again on each request, and nothing is recorded. Provider prompt caches match on the exact bytes of the conversation prefix, so that approach relies on every pass producing byte-identical output for messages the provider has already cached, through proxy restarts, outages and Headroom upgrades.

Pi Headroom follows Headroom's own cache mode instead, which freezes what the provider has cached and compresses only new content, and makes that state part of the session:

  • Compress once. A tool result after the last assistant message has never been sent, so it is compressed now. The decision is stored with pi.appendEntry().
  • Replay forever. Every later request sends the stored text byte for byte, without calling the proxy. /resume, restarts and proxy restarts change nothing.
  • Never rewrite the past. A result that was already sent unchanged (Headroom saved too little, the proxy was down, the extension was installed mid-session) stays unchanged while the provider may have it cached.
  • Use the free moment. A compaction replaces older history with a summary, so the first request after it has nothing cached past the system prompt. On that request every kept result that is not yet compressed gets another chance.
  • Keep the original. The session stores the full tool result, so headroom_retrieve works for the life of the session, and uninstalling the extension brings every original back.

It hooks context_with_system, which sends the returned messages as they are; the plain context hook would rebuild the leading system message whenever the list changes.

(blank)@twkrash/pi-headroom@casualjim/pi-headroom@cltec/pi-make-headroompi-headroom
When it compressesonce per tool result, before the first call that sends itevery model callevery model callevery model call
Cache stabilitystored decisions, replayed byte for byterecompression must repeat exactlyrecompression must repeat exactlyrecompression must repeat exactly
What the session storesoriginal text, plus the compressed textoriginal textoriginal textoriginal text
Headroom proxyyours, never started for you; setup notice if missingyoursinstalled and run in a private venvinstalled with pip and run by the extension
headroom_retrieve tool✓, falls back to the session when the proxy's copy has expired✓——
Pi's truncation note ([Showing lines … Full output: …])kept aside, re-appended verbatim———
Subagentsone settings.json entry———
Headroom dashboardlisted as Pi (x-client: pi)———
Pi package namespace@earendil-works@earendil-works (also runs in omp)@earendil-works@mariozechner

The comparison reflects each package's published source as of 2026-11-25. A "—" means the feature was not found in that package's source, not that it cannot be configured. Pick the one that fits:

  • Pi Headroom if you want cache-stable savings across every provider and subagent and you run the Headroom proxy yourself.
  • @cltec/pi-make-headroom if you want the extension to install and manage Headroom for you.
  • @casualjim/pi-headroom if you also run omp and want one extension for both, or want its /headroom command to switch compression on and off.

Install

  1. Install and start Headroom:

    uv tool install --python 3.13 "headroom-ai[all]"
    headroom proxy --port 8787
    
  2. Install the extension:

    pi install npm:@twkrash/pi-headroom
    
  3. Start Pi. The footer shows headroom ready.

If the proxy does not answer at session start, Pi shows a notice with the install or start command, once per Pi process, and tool results pass through unchanged.

Subagents

pi-subagents starts a child without auto-discovered extensions whenever the agent has its own extensions list. Add Pi Headroom to subagents.defaultSubagentOnlyExtensions in ~/.pi/agent/settings.json so every child loads it:

"subagents": {
  "defaultSubagentOnlyExtensions": ["~/.pi/agent/npm/node_modules/@twkrash/pi-headroom/src/index.ts"]
}

Children share the same proxy, so headroom_retrieve works inside them too.

What gets compressed

  • A tool result whose content is one text block of at least 2,000 characters.
  • Not results that mix text and images, not shorter results, and never headroom_retrieve's own output.
  • Headroom decides what it can shrink. Output without a recognisable shape, such as plain seq lines, comes back unchanged and the original is kept.
  • A compression is kept only if it saves at least 200 tokens and at least a tenth of the result. A smaller saving is not worth a Retrieve more marker the model may follow back.
  • context-mode's ctx_execute, ctx_search and ctx_batch output is skipped without calling the proxy: it is already summarised. Without context-mode installed, no result carries those names.
  • Other extensions that shrink tool output in their own tool_result handler run first. Pi Headroom then sees their output and compresses it only if it is still at least 2,000 characters, so nothing is compressed twice.
Footer statusMeaning
headroom readyThe proxy answered; nothing compressed yet this session.
headroom compressing…A tool result is on its way through the proxy.
headroom -47.5k tok (89%) · 2 resultsTokens saved this session, the share of the compressed results, and their count.
headroom offline: <reason>Headroom is not installed, its proxy is not running, or the proxy returned an error.

Every request carries x-client: pi, so the Headroom dashboard at http://127.0.0.1:8787/dashboard lists Pi's savings under its own Pi agent, next to Claude Code or any other client using the same proxy.

Configuration

VariableDefaultPurpose
HEADROOM_URLhttp://127.0.0.1:8787Headroom proxy base URL.

Limits

  • The proxy keeps originals for 30 minutes (HEADROOM_CCR_TTL_SECONDS). After that, headroom_retrieve reads them from the session. Subagents run with --no-session, so theirs last as long as the subagent.
  • The session file grows by the compressed text of each compressed result, next to the original it already holds.
  • The "not installed" check looks for a POSIX executable named headroom on PATH.

Run from source

git clone https://github.com/twKrash/pi-headroom.git
cd pi-headroom
npm ci
npm run format:check && npm run lint && npm run typecheck && npm run knip
npm test
pi install ./

License

MIT