> ## Documentation Index
> Fetch the complete documentation index at: https://traceroot.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# pi SDK

> Trace embedded pi coding-agent sessions with traceroot

Automatically capture agent runs, LLM calls, tool executions, and token usage from [pi](https://pi.dev) coding-agent sessions you drive with `@earendil-works/pi-coding-agent` — the library form of pi, where you run an `AgentSession` from your own Node process instead of the interactive CLI.

> [!NOTE]
>
> Running the interactive `pi` CLI instead? Use the
> [traceroot pi extension](https://github.com/traceroot-ai/traceroot-pi-extension).

## Install

> [tab] TypeScript
>
> ```bash theme={null}
> npm install @traceroot-ai/traceroot @earendil-works/pi-coding-agent
> ```

## Set up

Call `TraceRoot.initialize()` before creating any session:

```typescript theme={null}
import * as pi from '@earendil-works/pi-coding-agent';
import { TraceRoot } from '@traceroot-ai/traceroot';

TraceRoot.initialize({
  instrumentModules: { piCodingAgent: pi },
});
```

## Usage

Create and run sessions as usual — every session created after initialization is traced automatically:

```typescript theme={null}
import { createAgentSession, ModelRegistry, ModelRuntime } from '@earendil-works/pi-coding-agent';

// pi 0.80's model + credential surface. Created async (it reads the on-disk
// model catalog), then handed to createAgentSession. Injecting OPENAI_API_KEY
// as a runtime credential authenticates from env alone — no `pi auth login`.
const modelRuntime = await ModelRuntime.create();
await modelRuntime.setRuntimeApiKey('openai', process.env.OPENAI_API_KEY);

const modelRegistry = new ModelRegistry(modelRuntime);
const model = modelRegistry.find('openai', 'gpt-4o-mini');

const { session } = await createAgentSession({ model, modelRuntime });
try {
  await session.prompt('Write add.js exporting add(a, b), then write and run a test for it.');
} finally {
  session.dispose();
}
```

Each `session.prompt()` becomes a nested trace, with tool spans under the LLM span that requested them:

```
AgentSession.prompt              # AGENT span — one per prompt() call
└── gpt-4o-mini                  # LLM span — provider, tokens
    ├── write: add.js            # TOOL span — arguments + result
    ├── write: test-add.js
    └── bash: node test-add.js
```

## Verify

Run your code and confirm the trace arrives:

1. **In the traceroot UI**: Navigate to **Tracing > Traces** in [traceroot Cloud](https://app.traceroot.ai). You should see a trace with root span `AgentSession.prompt`.
2. **Via the CLI**: Run `traceroot traces list` to inspect your recent traces from the terminal.

## What gets captured

| Attribute | Description |
| - | - |
| Agent runs | One span per `session.prompt()` call |
| Model | Model id for each LLM call |
| Messages | Prompt and assistant response, when content capture is enabled |
| Tool calls | Each tool execution with arguments, result, and error status |
| Tokens | Input, output, and cache read/write counts |
| Latency | Duration per span |

`captureContent` and `captureToolIo` (both default `true`) control whether prompt/response text and tool arguments/results are attached to spans. Pass them through the `{ module, config }` form:

```typescript theme={null}
TraceRoot.initialize({
  instrumentModules: {
    piCodingAgent: { module: pi, config: { captureContent: false, captureToolIo: false } },
  },
});
```

`captureContent` can also be a function that redacts, truncates, replaces, or omits each prompt and response before it is recorded. See [Data Capture and Privacy](https://traceroot.ai/docs/tracing/data-capture.md) for a working redaction example.

## Limitations

* `@earendil-works/pi-coding-agent` is ESM-only and requires Node >= 22.19.

## Run the example

Clone the repo and run a complete session end-to-end.

> [card] [TypeScript](https://github.com/traceroot-ai/traceroot/tree/main/examples/typescript/pi-agent-sdk)
>
> Run the TypeScript example

## Next steps

> [card] [TypeScript SDK Reference](https://traceroot.ai/docs/tracing/typescript-sdk.md)
>
> Explore the full TypeScript SDK reference and configuration options.

> [card] [Cost Tracking](https://traceroot.ai/docs/tracing/cost-tracking.md)
>
> Learn how traceroot calculates token costs and tracks model spend.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.