← Catalog

pi-context-slim

Compact redundant Pi tool prompt metadata without changing tool behavior

ExtensionPrompt

Install

Install with PiG

pig install npm:pi-context-slim

Install with Pi

pi install npm:pi-context-slim
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.1.2 (README.md). PiG does not review it.

pi-context-slim

A Pi extension that shortens the prompt lines of optional tools before each request, without removing tools or changing their schemas, arguments, results, or execution.

Pi builds its system prompt from named sections (preamble, tools, rules, docs, skills, project_context, cwd, plus any section an extension adds). Installed packages contribute long guideline lines to those sections: web research hints, goal and task guardrails, goal drafting rules. Every request pays for them, in every session. This extension replaces the longest ones with shorter equivalents that keep the constraints the model has to satisfy.

What it does

It hooks context_with_system, which Pi fires before each model request, after every other extension has contributed its prompt sections. Every system message carrying these sections is rewritten, so a section patch Pi appends mid-session (after a tool loadout change, for example) is covered too.

SectionChange
toolsReplaces the description line of 13 optional tools with a shorter sentence
rulesReplaces 7 guideline lines whose text contains a known upstream sentence

Matching is line-based: only a line starting with - <tool>: is replaced, and only the matching rules line is swapped. Everything else in the section — including Pi's <tools>/<rules> wrappers — is left untouched. Rewriting an already compacted section changes nothing.

Pi's own sections and sections owned by other extensions are left alone. In particular codemode_titles, which pi-codemode-render adds to ask the model for a script title, is not touched: the shortened variant saves about 30 tokens and loses the // <what this script does> format, the ordering of a // @options: line, and the reason the model complies.

Tool descriptions

ToolReplacement
web_searchSearch the web for current external information.
source_checkGather cited source evidence.
fetch_contentFetch content from a URL or file.
get_search_contentRead stored search or fetched content.
web_enableEnable web tools when current external information is needed.
get_goalRead goal state or omitted requirements.
create_goalCreate a goal only on explicit request.
update_goalUpdate goal status or continuation.
set_goal_tasksSet goal tasks with confirmation.
update_goal_taskUpdate goal task progress.
goal_questionAsk one goal-drafting question.
goal_questionnaireAsk goal-drafting questions.
propose_goal_draftPropose a goal draft for confirmation.

Tools the prompt does not declare are reported as missing by /context-slim, not as a failure: the replacement only applies when the line exists.

Rule lines

A rule line is rewritten when it contains one of these upstream sentences. The replacement is the whole line: it drops the explanation and keeps the constraints.

Matched upstream textReplacement
Never infer persistent goalsCreate goals only on explicit request; the objective preserves every requirement and ordered step; set a token budget only when supplied.
Use tasks only for useful milestonesUse goal tasks only for useful milestones; restructure only when requested or requirements change; the tree is flat, parent-linked, at most 50 tasks, and lightweight_subtasks applies to parents only.
start requires pendingTask transitions: start requires pending; complete requires evidence and finished non-lightweight children; skip requires a reason and explicit direction, never to avoid work; completed tasks are immutable.
Use only during /goalUse goal drafting tools only during /goal, /sisyphus, or /goal-tweak.
Clarify ambiguity before proposingClarify ambiguity before proposing a goal draft.
Set token_budget only whenSet token_budget only on explicit request, and confirm the draft.
Confirmation creates or revisesConfirming the draft creates or revises the goal.

The constraints that make calls fail when ignored stay in place: the shape and size limits of the task tree, the immutability of completed tasks, and the ban on skipping to avoid work.

Reporting drift

A rewrite is keyed on a sentence another package writes. When that package rewords the sentence, the rewrite stops applying and nothing says so — the long line is simply paid for again on every request.

The extension watches for that. When the prompt declares a goal tool but at least one goal rule rewrite matches nothing, it warns once per session, and /context-slim reports the last request: how many system messages were rewritten, which tool descriptions were shortened, and which rule sentences found no line. A tool that is simply not installed stays quiet — an absent tool is expected, not drift.

The tests in test/compact.test.ts hold the same line: fixtures captured from a live prompt fail when a needle stops matching.

What it does not change

  • The tool set, tool order, parameter schemas, and tool results.
  • Prompt sections other than tools and rules, including those added by other extensions.
  • Your session history. Pi deep-copies the message list before handing it to context_with_system handlers, so the rewrite touches only the request being built. The transcript keeps the original prompt, and the compaction is re-applied on every request instead of accumulating.

The rewrite is a deterministic function of the prompt sections, so the request prefix stays stable between turns, which keeps provider prompt caching intact.

Measured effect

On one prompt with pi-web-access and pi-goal-x loaded, 9 tool descriptions and 3 rule lines shrank the two sections from 22,731 to 22,027 characters: about 700 characters, roughly 175 tokens per request. The saving scales with how many long guideline lines are installed; a setup without these packages sees little or no change.

Requirements

Pi needs the context_with_system extension event, added in 0.87.0 (2026-09-21). The peer dependency asks for >=0.87.0; verified against Pi 1.5.1.

Install

pi install npm:pi-context-slim

One invocation only:

pi -e npm:pi-context-slim

Pi loads the package through its pi.extensions manifest. Start a new session or run /reload.

Remove

pi remove npm:pi-context-slim

Verify

/context-slim shows what the last request rewrote. For the full prompt a request carries, a throwaway extension hooking context_with_system or before_provider_request can log it:

import { getCurrentSystemPrompt } from "@earendil-works/pi-ai";

export default function logPrompt(pi) {
  pi.on("context_with_system", (event) => {
    console.log(getCurrentSystemPrompt(event.messages));
  });
}

Compare the tools and rules sections with the extension disabled: remove it (pi remove npm:pi-context-slim) and run the same logging extension again.

Limitations

  • Replacements match upstream substrings. If a package rewords its guideline the replacement silently stops applying — the drift report and the fixtures are there to make that visible, not to prevent it.
  • A tool description is replaced as a whole, so usage hints the upstream line carried beyond the first sentence (argument shapes, provider overrides) are removed on purpose. Review the table above before relying on it.
  • A forced system prompt (forceSystemPrompt, or a custom prompt) lives in the message content and has no sections, so there is nothing to compact.
  • No configuration. To compact a different set, edit src/compact.ts.

Development

npm ci --ignore-scripts
npm run check          # node --check: syntax only
npm run typecheck      # tsc --noEmit against the pinned Pi API
npm test               # node --test
npm pack --dry-run

Layout:

src/index.ts            the extension: hook, drift report, /context-slim command
src/compact.ts          the rewrite tables and the section transforms
test/compact.test.ts    fixtures captured from a live prompt

The package has no runtime dependencies. The development-only dependencies pin TypeScript, Node.js types, and @earendil-works/pi-coding-agent@1.5.1 so npm run typecheck checks the extension against a known Pi API while the peer dependency remains compatible with Pi >=0.87.0. npm ci installs the exact versions from package-lock.json. Pi provides @earendil-works/pi-ai for the prompt helpers used in the verification snippet. Node 24 runs the TypeScript sources directly, which is what the test script relies on.

Publishing, keeping the prefixed tag so it survives a rename:

npm version patch
git tag pi-context-slim-vX.Y.Z
npm publish

License

MIT