-
Install dependencies and build the plugin:
./install.sh
-
Set required environment variables:
export BRAINTRUST_API_KEY='your-api-key' export BRAINTRUST_PROJECT='your-project-name' # traces will be logged here export BRAINTRUST_DEBUG=true # optional: enables debug logging
You can configure the plugin using a config file or environment variables.
Create a braintrust.json file in one of these locations:
.opencode/braintrust.json- Project-level config~/.config/opencode/braintrust.json- Global config
{
"trace_to_braintrust": true,
"project": "my-project",
"debug": true
}| Config Key | Env Var | Type | Default | Description |
|---|---|---|---|---|
trace_to_braintrust |
TRACE_TO_BRAINTRUST |
boolean | false |
Enable/disable tracing |
project |
BRAINTRUST_PROJECT |
string | "opencode" |
Project name for traces |
debug |
BRAINTRUST_DEBUG |
boolean | false |
Enable debug logging |
api_key |
BRAINTRUST_API_KEY |
string | API key for authentication | |
api_url |
BRAINTRUST_API_URL |
string | "https://api.braintrust.dev" |
API URL |
app_url |
BRAINTRUST_APP_URL |
string | "https://www.braintrust.dev" |
App URL |
org_name |
BRAINTRUST_ORG_NAME |
string | Organization name |
Configuration is loaded with the following precedence (later overrides earlier):
- Default values
~/.config/opencode/braintrust.json(global config).opencode/braintrust.json(project config)- Environment variables (highest priority)
Environment variables override config file settings. This is useful for per-run overrides:
TRACE_TO_BRAINTRUST=true opencodeBoolean environment variables accept true, TRUE, 1 (case-insensitive) as truthy values.
bun testRun a simple opencode command to generate a trace:
./install.sh && TRACE_TO_BRAINTRUST=true BRAINTRUST_DEBUG=true opencode run -m anthropic/claude-3-haiku-20240307 "say hello and nothing else"Trigger a session error by using an invalid API key:
./install.sh && ANTHROPIC_API_KEY=invalid-key TRACE_TO_BRAINTRUST=true BRAINTRUST_DEBUG=true opencode run -m anthropic/claude-3-haiku-20240307 "say hello" 2>&1 || trueCheck the debug logs for session.error handling:
grep "session.error\|Handling session error\|Session error handled" ~/.local/share/opencode/log/*.log | tail -10Use the Braintrust MCP tools to query the project logs. First resolve the project ID:
braintrust_query_logs: SELECT DISTINCT project_id FROM logs LIMIT 1
Then query for recent root spans (traces):
braintrust_query_logs: SELECT id, created, span_id, root_span_id, is_root, span_attributes FROM logs WHERE is_root = true ORDER BY created DESC LIMIT 5
To see all spans in a specific trace, use the root_span_id:
braintrust_query_logs: SELECT id, created, span_id, span_parents, span_attributes, input, output, metrics FROM logs WHERE root_span_id = '<root_span_id>' ORDER BY created ASC
To find spans with errors:
braintrust_query_logs: SELECT id, created, error, span_attributes FROM logs WHERE error IS NOT NULL ORDER BY created DESC LIMIT 5
src/index.ts- Plugin entry point, hooks registrationsrc/tracing.ts- Tracing hooks implementation (session, turn, tool spans, error handling)src/client.ts- Braintrust API clientsrc/tools.ts- Braintrust query tools exposed to OpenCode
The plugin creates spans in this structure:
Session (root span)
- Turn 1 (task span for user message)
- Tool call (tool span)
- Tool call (tool span)
- Turn 2
- Tool call
...
OpenCode writes debug logs to ~/.local/share/opencode/log/. When BRAINTRUST_DEBUG=true, the plugin logs tracing events there which can help diagnose issues.