Skip to main content
The Pi Coding Agent SDK (@earendil-works/pi-coding-agent) is a TypeScript SDK for running pi coding agents programmatically. Braintrust traces AgentSession.prompt() calls, Anthropic LLM calls, and tool invocations.
This page documents the @earendil-works/pi-coding-agent SDK. To trace interactive pi sessions with the Braintrust pi extension instead, see pi.

Setup

Install Braintrust alongside the Pi Coding Agent SDK, then set your API keys. Requires @earendil-works/pi-coding-agent v0.79.x.
1

Install packages

2

Set environment variables

.env

Auto-instrumentation

To trace Pi Coding Agent SDK runs without wrapping the SDK module yourself, initialize Braintrust normally, then run your app with Braintrust’s import hook to patch the SDK at runtime.
1

Initialize Braintrust and run a prompt

2

Run with the import hook

The auto-instrumentation example uses plain JavaScript so node --import can run the file directly. The Braintrust APIs work the same in TypeScript projects — compile your TypeScript to JavaScript, then run the compiled file with the import hook.
If you’re using a bundler, see Trace LLM calls for plugin and loader setup.
If you manage instrumentation hooks directly with @braintrust/auto-instrumentations, keep the Pi Coding Agent integration enabled.

Manual instrumentation

To trace the Pi Coding Agent SDK manually, wrap the imported module yourself with wrapPiCodingAgentSDK() before creating sessions.

What Braintrust traces

Braintrust patches AgentSession.prompt() and captures:
  • Task spans for each AgentSession.prompt() call.
  • Anthropic LLM spans nested under the prompt span, including model metadata, stop reasons, and token metrics.
  • Tool spans for tool invocations, including bash calls, tool names, inputs, outputs, and tool call IDs.
  • Pi Coding Agent metadata under keys such as pi_coding_agent.operation, pi_coding_agent.api, pi_coding_agent.model, pi_coding_agent.source, pi_coding_agent.stop_reason, and pi_coding_agent.tool.name.
  • OpenTelemetry GenAI tool metadata, including gen_ai.tool.name and gen_ai.tool.call.id.
  • Errors captured on task, LLM, and tool spans.

Resources