@mrclrchtr/supi-context19.0.0
Live context-window pressure and token-use reports for the Pi coding agent
Untested on omp
This is a pi package. No one has reported whether it works on omp yet. If you try it, add your result below.
Reports 0
No reports yet.
Readme
@mrclrchtr/supi-context — Context Window and Token Usage Monitor for Pi
Adds context window and token usage monitoring to the Pi coding agent.
Install
pi install npm:@mrclrchtr/supi-context
For local development:
pi install ./packages/supi-context

Two concepts, two surfaces
- A Context Pressure Snapshot is a small point-in-time capacity reading for deciding whether a session has room for another operation.
- A Context Usage Report is a diagnostic account of where the session's context is spent.
Human: Context Usage Report
In interactive TUI mode, pi gets these commands:
/supi-context— render a report with previews of section parts and tool declarations/supi-context full— render the same report with complete lists
The command appends a durable supi-context custom entry and uses a dedicated entry renderer. The report remains in the transcript but never enters LLM context.
Agent: Context Pressure Snapshot
context_report is agent-callable when enabled in configuration. It defaults to a one-line, constant-shape JSON Context Pressure Snapshot:
interface ContextPressureSnapshot {
modelName: string;
contextWindow: number | null;
usedTokens: number;
usagePercent: number | null;
compactionEnabled: boolean;
reserveTokens: number;
headroomTokens: number | null;
pressurePercent: number | null;
compacted: boolean;
approximationNote: string | null;
}
Use context_report({ mode: "full" }) only when diagnostic attribution is needed. Full mode returns { ...snapshot, attribution }. The attribution describes what the last request sent, replayed from the system messages Pi records in the session:
categories: token estimates for the system prompt, tool declarations, user and assistant messages, tool calls, tool results, and other messages, plusunattributed: the measured total minus all estimates. A positive remainder is provider overhead and remaining estimation error; a negative one means the estimates exceed the measurement. Without a measurement it is0.calibrationFactor: Pi estimates tokens as characters / 4, which current tokenizers undercount by up to 70 %. The factor is the measured input of the latest valid request divided by the estimate of what that request sent; every estimate is multiplied by it. It isnull(estimates unchanged) while Pi reports no measured total, for example before the first request or right after a compaction.estimatedTokens: the sum of the calibrated categories.promptSections: each system-prompt section (preamble,tools,rules,project_context,skills, extension sections such asshell) with its tokens, itssource, itsowner, andparts. Parts attribute the rules and the tools list per tool, skills per skill, and project context per file;(base)holds Pi's fixed text and framing.toolDeclarations:{ name, tokens, source, owner, activation }for each declaration as sent, including descriptions that codemode rewrites. Declarations that codemode hides from requests are left out.injectedFiles: file, turn, token, and line records of injected context files.
source is pi for Pi's own text and built-in tools, extension for sections and rules that an extension added, and otherwise Pi's sourceInfo.source of the resource that registered the tool or skill:
auto: auto-discovered from a skills or extensions directory, such as.agents/skills/.- a path such as
..: a local package listed by path insettings.jsonpackages, such as a development install of this workspace. npm:<package>orgit:<repo>: an installed package, for examplenpm:@mrclrchtr/supi-review.
/supi-context shows these as auto-discovered, local:<path>, and unchanged, under a from column with a legend; the JSON keeps Pi's raw values. owner names the package that defines a section or tool, such as supi-pi-patches for shell or supi-agent-runtime for agent_result, even when another package bundles it; the per-tool parts of rules and tools carry the owner of their tool. owner is null for Pi's own text, for skill, file, (base), and (custom rules) parts, and for packages that do not declare their sections and tools. /supi-context shows the owner in place of the source when one is declared.
activation says why a declared tool is active and how to turn it off:
{ kind: "default", causes: [] }: no SuPi setting gates the tool; Pi's registration default or tool selection (defaultTools,--tools) applies.{ kind: "gated", causes }: SuPi activated it. Each cause names theownerpackage, thesettingassection.keyin the SuPi config (for examplereview.agentToolEnabled), and thescopethat supplied its value:project,global, ordefault. A cause withsetting: nullis an owner that always needs the tool, such as Agent foragent_result.{ kind: "external", causes }: SuPi last turned the tool off, because a gating setting was off or a runtime condition such as a missing CLI failed, and something else, such as Pi's/toolsor a restored session, turned it on again.causesnames the settings that gate it.
Before the first request, the prompt appears as one (not yet sent) section and the tool declarations are estimated from the active tools.
If the complete compact result exceeds Pi's default output limits, the tool instead returns { ...snapshot, attributionFile: { path, bytes } }, with no inline attribution. The temporary file contains the exact complete focused result, and bytes is its UTF-8 byte length. The path is ephemeral; no retention is guaranteed.
The registered output schema covers all three shapes. Codemode receives native data through structuredContent; direct calls receive compact JSON for that same object. Scripts need not parse tool content or use rendering details:
const snapshot = await tools.context_report({});
text({ headroomTokens: snapshot.headroomTokens, pressurePercent: snapshot.pressurePercent });
const report = await tools.context_report({ mode: "full" });
if ("attribution" in report) text(report.attribution.toolDeclarations);
else if ("attributionFile" in report) text(report.attributionFile);
The tool TUI never shows raw agent JSON: concise results render as a dense snapshot that expands into its metrics; full results expand into the diagnostic report.
What the report shows
The Context Usage Report includes:
- model name, context-window size, used tokens, usage percentage, pressure percentage, and Headroom
- the effective Compaction reserve and factual presence of compaction on the active branch
- approximation or pending-usage notes when exact usage is not available
- a visual usage bar and attribution-category breakdown for system prompt, tool declarations, messages, tool calls, tool results, other messages, and the unattributed remainder
- system-prompt sections with their sources and parts, instruction/context files, tool declarations with sources, injected files, and registered provider sections
Configuration
The human /supi-context command needs no configuration.
To enable the agent-callable context_report tool, set agentToolEnabled to true in your supi config:
{
"context": {
"agentToolEnabled": true
}
}
Or toggle it via /supi-settings → Context → Agent Tool.
The tool is disabled by default. Toggling it takes effect in the running session without /reload.
Notes
- The extension caches the latest
systemPromptOptionsfrombefore_agent_startto attribute rules to tools and to leave out hidden declarations. Before the first prompt after a start or reload,/supi-contextuses Pi's base prompt options instead;context_reportalways runs within a turn. - Exact usage comes from Pi's current context-usage data when available. Otherwise the extension estimates usage and preserves an approximation note.
- The Active Context Limit is the auto-compaction threshold while auto-compaction is enabled, otherwise the raw model context window. Headroom and pressure use that limit.
- Compaction reserve reduces the auto-compaction threshold; it is not a summary output budget. Pi summary requests use the model's output limit, reduced when needed so input and output fit the context window (see installed Pi
docs/compaction.md§ When It Triggers).
Source
src/context.ts— surface registration and cached prompt-option handlingsrc/capacity.ts— shared capacity analysis and snapshot shapesrc/analysis.ts— category estimates and report data assemblysrc/prompt-attribution.ts— system-prompt and tool-declaration attribution replayed from the transcriptsrc/entry-renderer.ts— TUI-only custom-entry renderer for the human commandsrc/format*.tsandsrc/report-component.ts— Context Usage Report renderingsrc/snapshot-component.ts— compact, width-safe snapshot renderingsrc/tool/context_report/{spec,result-schema,guidance,result,render}.ts— agent-tool schema, guidance, native/direct result assembly, and human rendering
Tests live under __tests__/unit/.