pi-context-guard-jev

Pi extension that removes tool output the agent no longer needs from its context. The Jev classifier keeps the needed lines verbatim; stale small outputs, unrelated old exchanges and old screenshots become one-line notes. Everything can be recalled.

Packages

Package details

extension

Install pi-context-guard-jev from npm and Pi will load the resources declared by the package manifest.

$ pi install npm:pi-context-guard-jev
Package
pi-context-guard-jev
Version
0.3.4
Published
Nov 8, 2026
Downloads
381/mo · 52/wk
Author
alandarev
License
MIT
Types
extension
Size
344.5 KB
Dependencies
0 dependencies · 2 peers
Pi manifest JSON
{
  "image": "https://raw.githubusercontent.com/alandarev/pi-context-guard-jev/master/assets/context-curve.png",
  "extensions": [
    "./src/index.ts"
  ]
}

Security note

Pi packages can execute code and influence agent behavior. Review the source before installing third-party packages.

README

pi-context-guard-jev

npm license Pi ≥ 1.0.3

Tool calls, their outputs and old off-topic conversation make up over 80% of the messages your Pi agent re-sends with every request (1,138 real sessions). This extension trims what the work no longer needs (old tool output, exchanges about earlier tasks, screenshots, long arguments): an estimated 25% fewer context tokens, for 1.6¢ per session.

pi install npm:pi-context-guard-jev

Needs an OpenRouter key (OPENROUTER_API_KEY or /login); the footer then shows 🛡 0 saved. Judged tool output is sent to OpenRouter → TypeSafe (what exactly).

Context per request in 127 real sessions of 200+ requests: with the guard 260k tokens at request 200, estimated 361k without it (−28%)

How it works

  1. When a run ends, and at checkpoints in long runs, Jev (a small, fast classifier by TypeSafe) judges each old tool output against the question, the answer and the ongoing work.
  2. Lines still needed stay verbatim; the rest becomes a one-line note. Unrelated old exchanges and screenshots older than 3 turns become notes too; long arguments of old tool calls (written files, heredocs) keep only their first and last lines (not on Claude Haiku, which misreads them). Edits that are not expected to pay back their prompt-cache rewrite are skipped.
  3. Every note ends with recall({"entryId":"…"}), so the model can fetch the original.
[context-guard] Distilled the output of bash `rg -n -i compact dist docs`: kept 141 of 431 lines (15.1k of 50.4k chars); the rest was judged not needed for the answer. Full output: recall({"entryId":"1704787f"}).

Measured on real work

Three weeks (October 2026) of one heavy user's everyday Pi sessions: Claude and GPT, main sessions and subagents. "Without the guard" = the same sessions with the edits undone (an estimate, not an A/B test).

Context sent Est. bill¹ Sessions
All −25% (10.8 billion tokens) −5.8% 1,138
Claude (Anthropic) −26% −6.1% 512
GPT (OpenAI Codex) −22% −5.1% 625
Under 50 requests −16% +1.7% 580
50–199 requests −22% −1.1% 365
200+ requests −26% −7.1% 193
Subagents (any length) −28% −10% 865

Estimated main-model bill by session length: under 50 requests +1.7%, 50–199 requests −1.1%, 200+ requests −7.1%

Where the money went $3,175 cheaper context − $2,210 prompt-cache rewrites − $18 Jev = $948 net on $15,459 spend
Items edited 60,005: 24.6k large outputs distilled, 31.5k small outputs and 566 exchanges omitted, 3.3k screenshots removed
Model fetched a removed item back 2.8% (1.2–4.0% by type)

¹ Estimated at each request's own token prices, net of cache rewrites and Jev. The rows are three splits of the same sessions. The data predates 0.3.4, which skips run-end edits that do not pay off. Raw aggregates · method and limits.

Use

Nothing to call. /guard shows each decision, /guard off turns it off, /tree to a point before an edit undoes it. Footer: 🛡 −4.2k · 3 = about 4.2k tokens kept out of context right now, by 3 edits.

Settings live in ~/.pi/agent/context-guard.json, e.g. { "excludeTools": ["edit", "write", "bash"] } (all keys).

Trade-offs

Third party Each pass sends your prompt, the answer or progress notes, short excerpts of earlier messages, tool arguments and the outputs being judged to OpenRouter → TypeSafe. Never sent: images, outputs under 400 chars, excluded tools (edit, write by default)
Lossy A removed line can be needed later. The note says what is missing and the model can recall it (it did for 2.8% of removed items)
Short sessions Under 50 requests: less context, but the cache rewrite costs more than it saves (+1.7% bill)
Latency 0.5–2 s per pass, 8 s at most

More

Settings · Results · Threshold replay · Design · MIT license