@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
1
Install pi
If you haven’t already, install pi:
2
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.
3
Set your Braintrust environment variables
Tracing is disabled by default. Enable it and provide your API key:Restart pi after changing environment variables.
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, Git remote origin URL (with credentials redacted), current Git branch, HEAD commit hash, 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). When the prompt includes explicit/skill:<name>requests, turn spans also recordloaded_skill_namesandloaded_skills. - LLM spans: One span per model response inside a turn. Spans record the model name, stop reason, token metrics (prompt, completion, cached, and reasoning tokens), and streaming timing metrics. When the model provides capability metadata, spans also capture
model_context_window,model_max_tokens,model_supports_reasoning,supported_thinking_levels, anddeferred_tools_mode. Provider request metadata includesprovider_request_tool_countandprovider_request_deferred_tool_count. - Tool spans: One span per tool execution. When pi reads a
SKILL.mdfile, the span is renamed toskill: <skill-name>and carriesskill_nameandskill_pathmetadata. If the skill was explicitly requested via/skill:<name>in the prompt, the span also includesskill_load_trigger: "explicit". - 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
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.