Pi Coding Agent SDK (tracing)
Pi Coding Agent SDK (tracing)
The Pi Coding Agent SDK (@earendil-works/pi-coding-agent) runs pi coding agents programmatically from TypeScript. Use @respan/instrumentation-pi to trace every session.prompt() as a Respan trace: the user prompt, each LLM call with prompts, completions, token, cache and cost usage, each tool execution, and compactions. Nothing is written to disk; spans are batched in memory and sent to Respan.
This page covers the SDK used inside your own application. To trace interactive pi sessions in the terminal, see pi.
Set up Respan
Create an account at platform.respan.ai and grab an API key.
Use Respan Gateway
pi can also call models through the Respan gateway: register a provider with "baseUrl": "https://api.respan.ai/api" and "api": "openai-completions" in ~/.pi/agent/models.json or in your ModelRegistry. Every LLM call is then logged by the gateway with no SDK; the tracing integration on this page adds the agent-level structure (runs, tool calls, threads) on top.
Package README
@respan/instrumentation-piREADME — full option, configuration, and troubleshooting reference
Setup
Initialize and run
pi has no global patch point; wire the instrumentor into the session. The recommended way is an inline extension, which sees every pi event including the exact context sent to the model:
If you cannot control the resource loader, attach to an existing session instead:
Passing an explicit instrumentations list to Respan disables provider auto-instrumentation inside the pi process, so LLM calls are not traced twice. One tracer is created per session, so many sessions can run in one process.
View your trace
Open the Traces page. Each pi session is one trace and every session.prompt() adds a turn to it, as long as the session is persisted and reopened (SessionManager.open / continueRecent); all runs of a pi session also share the session id as thread id, so the Threads view shows the whole session as a conversation.
Zero-code alternative. pi’s default resource loader loads installed pi packages into SDK sessions too. On a machine where @respan/instrumentation-pi is installed as a pi package (pi install npm:@respan/instrumentation-pi, or respan integrate pi) and RESPAN_API_KEY is set, createAgentSession() is traced with no code changes. See pi for that setup.
Trace shape
Each agent run (one session.prompt() → agent_end) is one root agent span, pi.turn-<n>.agent, displayed as agent.turn-<n> — n is the prompt’s number within the pi session, so a resumed session continues at the next turn. Chat and tool spans hang directly off it.
Chat and tool spans are emitted the moment they complete, so an hour-long run streams into the dashboard while it is still running; the turn span arrives when the run ends. Every span carries the pi session id as its thread, session and trace-group identifier.
Captured data
Options
new PiInstrumentor(options) accepts:
One trace per run or per session
A pi session that is resumed over days or weeks (an email thread handled by an always-on agent, for example) can be viewed two ways:
traceScope: "session"(default): every run of the session joins one trace whose id is derived from the pi session id, so a session reopened in another process (SessionManager.open(file)) or after a week-long pause keeps adding turns to the same trace. Respan groups the runs as multiple roots under one synthetic root, ordered by time; the trace’s duration then spans from the first to the latest run. No span is ever held open across idle time. If you keep sessions in memory (SessionManager.inMemory()), every wake is a new session: pass your ownthreadIdentifier(for example the email-chain id) to keep them grouped in Threads.traceScope: "run": every prompt is its own trace with its own cost and latency, nested under an active OpenTelemetry span when there is one. All traces of the session still sharethread_identifier= the pi session id, so the Threads view shows the whole session in order.
Long-running sessions and volume
The instrumentation keeps only the state of the current run in memory and writes nothing to disk, so idle sessions cost nothing and a process that handles thousands of sessions a day stays bounded. Full context capture is quadratic in the number of LLM calls per run; for very high-volume deployments use promptCapture: "delta" (the full conversation is still reconstructable from the trace) or maxContentChars.
For at-least-once delivery across Respan outages and restarts, run the Respan Collector next to the application and set RESPAN_BASE_URL=http://127.0.0.1:4318. The collector’s queue is bounded and drains as soon as Respan acknowledges the data, so nothing accumulates on the machine.
Attributes
Use Respan attributes to group runs by user or conversation.
Resources
@respan/instrumentation-pion npm- Package README — full option reference
- pi — trace interactive pi sessions in the terminal
- Delivery guarantees & Collector
- Pi Coding Agent SDK on npm · pi documentation