@braintrust/pi-extension package automatically traces pi sessions to Braintrust, including turns, model calls, and tool executions.
This page documents interactive pi sessions with the Braintrust pi extension. To trace
@earendil-works/pi-coding-agent SDK usage in your own application, see Pi Coding Agent SDK.Prerequisites
Install pi
If you haven’t already, install pi:
Install the Braintrust extension
Install the package with pi:For a one-off run without installing it permanently, load the extension directly:
This release supports pi v0.65.0 or later. On pi versions before 0.65.0, session switch and fork events are no longer traced.
Usage
Once enabled, the extension traces each pi session as a hierarchy of spans:- Session spans: One root span per pi session that produces at least one turn, with metadata capturing the session ID and name, hostname, username, operating system, working directory, repository, pi version, and extension version. At session close, the span records the total turn and tool-call counts and the close reason.
- Turn spans: One span per user prompt or agent run. When pi fires an input event, turn spans also capture
input_source,input_streaming_behavior,input_image_count, andraw_input(a truncated copy of the prompt text). - LLM spans: One span per model response inside a turn.
- Tool spans: One span per tool execution.
- Compaction spans: One span per context-window compaction, capturing the compaction reason, whether pi will retry, and the token counts before and after.
- Branch Summary spans: One span each time pi summarizes a branch while navigating the session’s history tree, capturing the entries being condensed and the resulting summary.
- A
Braintruststatus indicator in the footer while tracing is active - A trace widget below the editor with a shortened clickable Braintrust trace link when available
Configuration
You can configure the extension with environment variables or JSON config files. Configuration precedence is:- Defaults
~/.pi/agent/braintrust.json<pi-config-dir>/braintrust.json- Environment variables
Config file locations
- Global:
~/.pi/agent/braintrust.json - Project:
<pi-config-dir>/braintrust.json, where<pi-config-dir>follows pi’s configured project directory and defaults to.pi.
The project config directory follows pi’s
CONFIG_DIR_NAME setting, so a custom directory such as .custom-pi is used automatically. Older pi versions that don’t expose this setting always use .pi. The global config path (~/.pi/agent/braintrust.json) is not affected.Supported settings
| Config key | Env var | Default | Description |
|---|---|---|---|
trace_to_braintrust | TRACE_TO_BRAINTRUST | false | Enable or disable tracing |
api_key | BRAINTRUST_API_KEY | unset | Braintrust API key |
api_url | BRAINTRUST_API_URL | https://api.braintrust.dev | Braintrust API base URL. For EU organizations, use https://api-eu.braintrust.dev. For self-hosted deployments, use your data plane URL. |
app_url | BRAINTRUST_APP_URL | https://www.braintrust.dev | Braintrust app URL |
org_name | BRAINTRUST_ORG_NAME | unset | Organization name for project-scoped links |
project | BRAINTRUST_PROJECT | pi | Braintrust project that receives traces |
debug | BRAINTRUST_DEBUG | false | Enable debug logging |
additional_metadata | BRAINTRUST_ADDITIONAL_METADATA | {} | Extra metadata attached to session spans. When set via environment variable, pass a JSON object string. |
log_file | BRAINTRUST_LOG_FILE | unset | Write extension logs to a file. Set to true or auto to use the default log path. |
state_dir | BRAINTRUST_STATE_DIR | ~/.pi/agent/state/braintrust-pi-extension | Directory for extension session state and default logs |
parent_span_id | PI_PARENT_SPAN_ID | unset | Parent span ID for attaching pi traces under an existing trace |
root_span_id | PI_ROOT_SPAN_ID | unset | Root span ID to use when attaching to an existing trace |
Embed traces in parent traces
You can attach a pi session under an existing Braintrust trace. This is useful when pi is part of a larger workflow and you want the entire interaction to appear inside a parent trace. To attach a pi session under an existing span, setPI_PARENT_SPAN_ID:
PI_ROOT_SPAN_ID:
Troubleshooting
No traces are appearing
No traces are appearing
- Verify
TRACE_TO_BRAINTRUST=true. - Verify
BRAINTRUST_API_KEYis set in the shell where you launch pi. - Restart pi after changing environment variables or config files.
- Run at least one prompt. A session root span is only created for sessions that actually produce a turn.
pi shows 'Braintrust missing API key' or a config warning
pi shows 'Braintrust missing API key' or a config warning
- Check
echo $BRAINTRUST_API_KEY. - Validate the JSON syntax in
~/.pi/agent/braintrust.jsonor.pi/braintrust.json. - Make sure
BRAINTRUST_ADDITIONAL_METADATAcontains valid JSON if you set it as an environment variable.
I want debug logs
I want debug logs
Enable debug logging and restart pi:You can also force file logging explicitly:By default, logs are written under
~/.pi/agent/state/braintrust-pi-extension/.The trace link widget does not appear
The trace link widget does not appear
- The widget is only shown in interactive mode.
- Run at least one prompt so the extension has a trace to link to.
- If tracing is active but no link appears, enable debug logging and inspect the extension log file.
Next steps
- Explore the source: See the braintrust-pi-extension repository.
- Run evaluations: Check out the evaluation guide.
- Inspect traces: Open your Braintrust project in the Braintrust UI.