Install
Install with PiG
pig install npm:@prefactor/piInstall with Pi
pi install npm:@prefactor/piPackages 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/index.js"
]
}README
Written by the package's publisher, shown as published in version 0.1.2 (README.md). PiG does not review it.
@prefactor/pi
A pi extension that records pi sessions in Prefactor. Each pi session becomes one Prefactor agent instance, which Prefactor shows as a conversation of user turns, assistant turns, LLM calls and tool calls.
Sensitive data: the extension records prompts, completions and tool results as they are, without redaction. Coding sessions often contain source code, credentials and other secrets, so do not use it for sessions that handle data you cannot send to Prefactor.
Installation
pi install npm:@prefactor/pi
To try it for one run without adding it to your settings:
pi -e npm:@prefactor/pi
Connect pi to Prefactor
Run this inside pi:
/prefactor setup
It signs you in through Prefactor in the browser. pi opens the sign-in page and shows the link, which you can also open yourself, or on another device when pi runs over SSH or in a container, and the code to approve there. You then create an agent for pi, named "pi" unless you choose another name, or pick one of your account's existing agents, and choose the environment when the agent has more than one deployment. Setup creates an agent-deployment token for that agent and environment, checks it, and saves it to ~/.pi/agent/prefactor.json with file mode 0600.
The account token from the sign-in is kept in memory only. Setup revokes it once the deployment token is saved, or when setup stops early. If revoking fails, setup keeps the deployment token and tells you to revoke the account token under Account settings in Prefactor.
If the server does not offer browser sign-in, or you choose to, setup asks you to paste an agent-deployment token instead. The token is visible on screen while you paste it.
| Command | Does |
|---|---|
/prefactor setup | Connects pi to Prefactor. Asks first when credentials already exist. |
/prefactor status | Shows where the credentials come from, the API URL, the account and agent, and whether the last request to Prefactor succeeded. |
/prefactor logout | Deletes the credentials file. Credentials from code or the environment are not affected. |
The commands need dialogs, so they work in interactive and RPC modes. In print and JSON modes, use the environment variables below.
Credentials
The extension sends spans with a Prefactor agent-deployment token. When a session starts, it reads credentials from the first of these sources that supplies a token:
- Options passed to
prefactorExtension()in code. - The environment variables
PREFACTOR_API_URL,PREFACTOR_API_TOKENandPREFACTOR_AGENT_ID. - The credentials file written by
/prefactor setup,~/.pi/agent/prefactor.json. It follows pi'sPI_CODING_AGENT_DIRsetting when that is set.
The agent id comes from the same source as the token. When that source has no agent id, the extension reads it from the token with GET /api/v1/ping. The API URL comes from the token's source or a later one, and defaults to https://app.prefactorai.com.
The credentials file holds api_url, api_token (the deployment token), agent_id, environment_id, account_id and created_at. Setup with browser sign-in also saves agent_name and account_name. A pasted deployment token cannot read those names from Prefactor, so they are left out, and /prefactor status shows the agent and account ids instead. The file never holds an account token.
When no token is found, the extension records nothing and tells you once how to set Prefactor up, so pi keeps working. In interactive and RPC modes the notice appears in pi. In print and JSON modes it is written to stderr.
Agents built on pi's SDK
Agents built with @earendil-works/pi-coding-agent load the same extension in code through extensionFactories, and each session is recorded as one agent instance. Install the package alongside pi:
npm install @prefactor/pi @earendil-works/pi-coding-agent
import {
type CreateAgentSessionRuntimeFactory,
createAgentSessionFromServices,
createAgentSessionRuntime,
createAgentSessionServices,
getAgentDir,
SessionManager,
} from '@earendil-works/pi-coding-agent';
import { prefactorExtension } from '@prefactor/pi';
const createRuntime: CreateAgentSessionRuntimeFactory = async ({ cwd, sessionManager, sessionStartEvent }) => {
const services = await createAgentSessionServices({
cwd,
resourceLoaderOptions: {
extensionFactories: [
prefactorExtension({
apiUrl: 'https://app.prefactorai.com',
apiToken: process.env.MY_PREFACTOR_TOKEN,
agentVersion: '2.3.0',
}),
],
},
});
return {
...(await createAgentSessionFromServices({ services, sessionManager, sessionStartEvent })),
services,
diagnostics: services.diagnostics,
};
};
const runtime = await createAgentSessionRuntime(createRuntime, {
cwd: process.cwd(),
agentDir: getAgentDir(),
sessionManager: SessionManager.inMemory(),
});
try {
await runtime.session.bindExtensions({});
await runtime.session.prompt('What files are in the current directory?');
} finally {
await runtime.dispose();
}
prefactorExtension() takes these options. Each credential falls back to its environment variable, then to the credentials file, as described under Credentials.
| Option | Meaning |
|---|---|
apiUrl | Prefactor API URL |
apiToken | Agent-deployment token |
agentId | Prefactor agent id, read from the token when omitted |
agentVersion | Identifies the agent version recorded for each session. Defaults to the version of the pi framework. Prefactor identifies agent versions by this value alone; the extension sends no name or description for them. |
spanTypeSchemas | Span types for the agent's own spans, added to the schema version the extension registers. They cannot redefine pi:session or the $agent:* span types. |
qualitySchemas | Quality schemas for quality recorded on the session's agent instance |
alertSchemas | Alert schemas for alerts raised on the session's agent instance |
toolSchemas | Span type details by tool name: title, description, template, params_schema (the tool's arguments), result_schema (the structured details the tool returns, recorded as result.details) and data_risk. The tool keeps its $agent:tool:<name> span type, so Prefactor still shows it as a tool call, and a tool listed here gets that span type even when it is added after the session starts. |
externalIdentifier | External identifier of each session's agent instance, such as the id of the CI run that ran the session: a string, or a function (ctx) => string | undefined called at each session start. See External identifiers. |
onSessionStart | Called with the session's recorder when the extension starts recording a session. It may be async; pi does not wait for it, and a rejection is logged. See Adding to the record. |
The session lifecycle depends on two calls:
session.bindExtensions()emitssession_start, which starts the agent instance. Nothing is recorded until it is called.AgentSessionRuntime.dispose()emitssession_shutdown, which finishes the agent instance after the remaining spans are sent.AgentSession.dispose()on its own does not emitsession_shutdown, so withcreateAgentSession()the agent instance stays active in Prefactor. UseAgentSessionRuntimeas above when the instance should be finished.
The pi command always records pi's release version, because it loads the extension without options.
Adding to the record
Agents that run their own steps around pi can add spans, quality and alerts to the session's agent instance through the recorder that onSessionStart receives. Declare the span types, quality schemas and alert schemas in the options first, because Prefactor accepts only those declared in the agent's schema version.
import { type PrefactorRecorder, prefactorExtension } from '@prefactor/pi';
let recorder: PrefactorRecorder | undefined;
const extension = prefactorExtension({
agentVersion: '2.3.0',
spanTypeSchemas: [
{
name: 'signal:poll_source',
template: 'Poll {{ source }}',
params_schema: { type: 'object', properties: { source: { type: 'string' } } },
result_schema: { type: 'object', properties: { items: { type: 'integer' } } },
},
],
qualitySchemas: [
{ name: 'signal:run_quality', schema: { type: 'object', properties: { sources_failed: { type: 'integer' } } } },
],
alertSchemas: [
{ name: 'signal:monitor_broken', schema: { type: 'object', properties: { reason: { type: 'string' } } } },
],
onSessionStart: (sessionRecorder) => {
recorder = sessionRecorder;
},
});
// Later, while the session runs:
const poll = recorder?.startSpan({ schemaName: 'signal:poll_source', payload: { source: 'reddit' } });
poll?.complete({ items: 12 });
recorder?.recordQuality('signal:run_quality', { sources_failed: 0 });
recorder?.raiseAlert('signal:monitor_broken', 'error', { reason: 'every source failed' });
| Recorder method | Does |
|---|---|
startSpan({ schemaName, payload, parent }) | Starts a span and returns it; finish it with complete(result) or fail(result). It nests under parent, which is another span from the recorder or { toolCallId } for a running tool call (for example, from inside a custom tool's execute). Without a parent, it nests under the open assistant turn, or under pi:session between runs. Spans still open at shutdown are cancelled. |
recordQuality(name, payload) | Records quality on the agent instance. null removes the value recorded for that name. |
raiseAlert(name, severity, payload) | Raises an alert on the agent instance. Severity is critical, error, warning or info. The agent-deployment token can raise alerts, so no other token is needed. |
setFinishStatus(status) | Sets the status the agent instance finishes with at session shutdown: complete, failed or cancelled. The last call wins. Without a call, the instance finishes as complete. |
The recorder's methods never throw and never wait for the network. Failures are logged. After the session shuts down, they record nothing. onSessionStart is not called when no Prefactor token is found.
External identifiers
Prefactor requires an agent instance's external identifier to be unique per agent. A session whose identifier the agent has already used fails to register, and nothing is recorded for it; pi keeps running and shows that Prefactor could not record the session.
A plain string therefore suits a process that runs one session, such as one CI job. When a process runs several sessions, pass a function, which is called at each session start with pi's extension context and can return a different identifier for each session, or undefined for none:
prefactorExtension({
externalIdentifier: (ctx) => `${process.env.GITHUB_RUN_ID}:${ctx.sessionManager.getSessionId()}`,
});
A function that throws is logged, and the session registers without an external identifier.
What gets recorded
pi's work is recorded with Prefactor's canonical agent span types, the ones Prefactor's hosted agents use, so each session opens in Prefactor's conversation view.
| Span type | Covers | Recorded |
|---|---|---|
pi:session | One pi session, the root of every other span | Session id, working directory, mode, start reason and model; shutdown reason |
$agent:system_message | The system prompt | The prompt in effect when a run starts, recorded when it differs from the last one recorded |
$agent:user_turn | One user message | The number of the turn in the session |
$agent:user_message | A prompt, steering message or follow-up message, in its user turn | The message text |
$agent:assistant_turn | The assistant's work after a user turn | Stop reason of the last response and the token usage summed over the turn's responses |
$agent:llm_call | One assistant response, in its assistant turn | Provider, model and API; response id and model, stop reason, token usage (including cache read and cache write tokens), cost and times, or the error |
$agent:assistant_thinking, $agent:assistant_message | Each thinking and text block of a response, in its assistant turn | The block's text |
$agent:tool:<name> | One tool execution, in its assistant turn | Tool call id and arguments; the result content and details, or the error |
Each tool pi has when the session starts gets its own $agent:tool:<name> span type. A tool added later, for example by an extension during the session, is recorded as $agent:tool:pi:other with its name in tool_name. A tool call made by another tool, such as a codemode script, is a child of that tool's span.
The assistant turn finishes when the run ends, or when a steering message starts the next user turn while the run continues.
The agent version is pi's release version, or agentVersion for agents built on pi's SDK. The working directory on each session lets you tell apart sessions from different repositories that report to the same agent.
Long strings are truncated to PREFACTOR_MAX_INPUT_LENGTH and PREFACTOR_MAX_OUTPUT_LENGTH characters (10,000 by default).
Shutdown and Ctrl+C
The session appears in Prefactor as soon as it starts. Each span is sent as soon as it starts or finishes, and the agent instance is finished when pi runs session_shutdown.
Pressing Ctrl+C in print, JSON or RPC mode ends pi without running session_shutdown. Spans that had already finished are recorded. The open assistant turn, the session span and the agent instance stay active in Prefactor.
Examples
examples/pi/print-mode.tsrunspionce in print mode with the extension loaded from this repository.examples/pi/sdk-agent.tsbuilds an agent on pi's SDK and records its session.
Requirements
- Node.js >= 22.19.0
- pi (
@earendil-works/pi-coding-agent) 0.99.0 or later