- Trace OpenCode sessions to record session, turn, model call, tool, and subagent spans in Braintrust.
- Connect to Braintrust MCP to work with Braintrust projects, experiments, logs, and other data from OpenCode.
Trace OpenCode sessions
The@braintrust/trace-opencode plugin passes your interactions with OpenCode to bt, which in turn uses a background process to build and deliver the trace to Braintrust.
OpenCode continues to communicate directly with its model provider, and tracing failures do not stop your OpenCode session. The plugin also provides built-in Braintrust tools, independently of MCP.
Set up tracing
@braintrust/trace-opencode is already installed, follow Update the tracing plugin.Install OpenCode
Install and authenticate bt
bt CLI quickstart to install bt v0.19.3 or later, authenticate, and set your active context.Enable tracing
- Adds or updates
@braintrust/trace-opencodein your global~/.config/opencode/opencode.json, using the^2version range. - Writes
~/.config/opencode/braintrust.json, which records whether tracing is enabled and which project receives traces.
XDG_CONFIG_HOME is set, both configuration files are under $XDG_CONFIG_HOME/opencode/ instead. A project’s .opencode/braintrust.json can override the global tracing settings.For more information, see bt trace enable.Restart OpenCode
Log a test trace
What Braintrust traces
OpenCode traces in Braintrust include:- Session spans with the session ID, local username, and Git repository metadata.
- Turn spans with prompts, model names, and requested skills.
- Model call spans with conversation input, response output, errors, and token metrics.
- Tool spans with inputs and outputs, including skill loads.
- Subagent spans nested under the parent session’s active turn when the parent is available.
braintrust.plugin.opencode.
Common workflows
Use different tracing settings for one session
Use different tracing settings for one session
bt trace run to trace a single OpenCode invocation without changing the saved tracing configuration.This workflow launches OpenCode from the terminal.To change the saved destination, profile, or organization, rerun bt trace enable opencode with the corresponding options. Running bt switch does not change the saved tracing configuration.To add root-span metadata, pass --additional-metadata '<JSON>' to bt trace run for one invocation or to bt trace enable for future sessions. Standard session metadata takes precedence when keys conflict.With bt v0.19.3 or later, pass --tag ci --tag release-validation to either command to tag session spans.Stop tracing OpenCode sessions
Stop tracing OpenCode sessions
bt trace disable opencode to remove the tracing plugin from the global OpenCode configuration and remove its global tracing configuration. Saved bt profiles and credentials are preserved. Restart OpenCode afterward.Removing the plugin also removes its built-in Braintrust tools. To stop tracing while keeping those tools, set trace_to_braintrust to false in the effective tracing file instead.If you also registered the plugin in a project configuration, remove that registration to stop tracing there.Built-in Braintrust tools
The tracing plugin also registers tools that callbt to access Braintrust data, without an MCP connection:
braintrust_query_logs: Query logs with SQL.braintrust_list_projects: List projects.braintrust_get_experiments: List recent experiments.braintrust_log_data: Upload data to project logs.
enable_tools to false in the global or project-local tracing file. BRAINTRUST_OPENCODE_ENABLE_TOOLS overrides that setting.
Update the tracing plugin
To update the plugin, follow these steps:opencode debug config shows @braintrust/trace-opencode earlier than v2.0.0 in the plugin list, or you cannot determine its version, follow Migrate from an older plugin version.Update bt
bt to the latest version and complete the applicable migrations.Update the tracing plugin
bt trace update updates the plugin’s package specification in the global OpenCode configuration without changing your saved tracing configuration.Restart and verify OpenCode
Migrate from an older plugin version
To migrate to the latest version of the@braintrust/trace-opencode plugin, follow these steps:
Inspect your OpenCode plugins
@braintrust/trace-opencode in the plugin list:.opencode/braintrust.json.Install or update bt
bt v0.19.3 or later. Check whether bt is installed:- If the command succeeds, migrate to the latest version (unless you’ve already done so).
- Otherwise, install
bt, authenticate, and set organization and project context.
Complete version-specific migration steps
@braintrust/trace-opencode version, beginning with the oldest. If no sections apply, continue to the next step.@braintrust/trace-opencode v1.0.0
@braintrust/trace-opencode v1.0.0
bt handles both, and the tracing files store settings rather than credentials.Record project, org_name, and additional_metadata from ~/.config/opencode/braintrust.json and your project’s .opencode/braintrust.json. Environment variables took precedence over these files. Earlier versions did not use XDG_CONFIG_HOME. If the project was not configured, the default was opencode.If the old configuration contains api_key, api_url, or app_url, use the corresponding credentials and deployment when you authenticate with bt. Keep credentials private and retain any settings needed by other integrations.@braintrust/trace-opencode v2.0.0
@braintrust/trace-opencode v2.0.0
TRACE_TO_BRAINTRUST: Whether tracing was enabled.BRAINTRUST_PROFILEandBRAINTRUST_ORG_NAME: The tracing account and organization.BRAINTRUST_PROJECT: The project that received traces.BRAINTRUST_ADDITIONAL_METADATA: Extra root-span metadata.
--project, account and organization with --profile and --org, and metadata with --additional-metadata '<JSON>'. This preserves values that previously overrode your saved configuration.braintrust.json are resolved:- Profile, organization, and project values in the
routeobject now take precedence over their legacy top-level equivalents. Before v2.0.0, the top-level values took precedence. - Metadata is unchanged: top-level
additional_metadatacan still overrideroute.additional_metadata.
route object so you do not leave conflicting copies. Environment variables accepted by bt commands are separate from these plugin changes. See bt trace.Apply your tracing settings
--profile <profile> and --org <organization> if needed to preserve your tracing account and organization. Otherwise, the command uses your active bt context, not your saved tracing settings.The command updates your global tracing file, normally ~/.config/opencode/braintrust.json. Check the following settings in that file:- Existing
route.additional_metadatais preserved unless you pass--additional-metadata. If it is absent,btmigrates top-leveladditional_metadatainstead. - Existing
route.tagsis preserved unless you pass--tagor setBRAINTRUST_TAGS. route.flush_moderesets tofire_and_forget. Restore its previous value if you customized it.- Restore any other custom tracing settings you recorded before updating.
route object in the generated global file as a template while preserving that project’s intended destination, authentication, metadata, and tags. A project-level route object replaces the entire global route object. After moving any legacy values under route, remove their duplicate top-level keys so they cannot override it.Restart and verify OpenCode
Remove obsolete settings
Settings used before @braintrust/trace-opencode v2.0.0
Settings used before @braintrust/trace-opencode v2.0.0
- Your shell configuration
- The global tracing file and your project’s
.opencode/braintrust.json
bt commands, MCP connections, or integrations still need:TRACE_TO_BRAINTRUST,BRAINTRUST_PROFILE,BRAINTRUST_ORG_NAME,BRAINTRUST_PROJECT, andBRAINTRUST_ADDITIONAL_METADATAin the agent environmentBRAINTRUST_API_KEY,BRAINTRUST_API_URL, andBRAINTRUST_APP_URLused by pre-v1.0.0 tracing- Top-level
profile,org_name,project, andadditional_metadataafter preserving their values underroute - Pre-v1.0.0
api_key,api_url,app_url,log_to_file, andqueue_sizekeys - Pre-v1.0.0
LOG_TO_FILEandBRAINTRUST_QUEUE_SIZEenvironment variables
BRAINTRUST_OPENCODE_ENABLE_TOOLS if you use it to control the built-in tools.Troubleshooting tracing
First, runbt trace doctor opencode. Then, review the following:
The tracing plugin is missing or disabled
The tracing plugin is missing or disabled
opencode debug config and confirm that @braintrust/trace-opencode@^2 is in the plugin list. If it is missing or broken, run:Traces do not appear after setup
Traces do not appear after setup
bt trace doctor opencode and confirm that tracing is enabled and routed to the intended project. Also inspect any project-local tracing file, which can override the global settings.If authentication succeeds but traces do not appear, confirm that your account has permission to write logs to the selected project.For authentication, tracing destination, or endpoint problems, see bt trace troubleshooting and the CLI migration troubleshooting guide.Tracing failures do not interrupt the OpenCode session.OpenCode uses an unexpected configuration directory
OpenCode uses an unexpected configuration directory
XDG_CONFIG_HOME is set in the environment that launches OpenCode. When it is set, the global tracing file is $XDG_CONFIG_HOME/opencode/braintrust.json instead of ~/.config/opencode/braintrust.json.Traces use unexpected settings
Traces use unexpected settings
bt trace run take precedence over the saved tracing configuration. For normal sessions, project-local .opencode/braintrust.json settings override global settings. A project-level route object replaces the entire global route object, so include its destination and complete authentication settings.The plugin does not use TRACE_TO_BRAINTRUST, BRAINTRUST_PROFILE, BRAINTRUST_ORG_NAME, BRAINTRUST_PROJECT, or BRAINTRUST_ADDITIONAL_METADATA from the agent’s environment to override the saved tracing settings.If an older file still has top-level additional_metadata, migrate it to route.additional_metadata and remove the old key so it cannot override your intended metadata.For environment variables accepted by the bt commands themselves, see bt trace.Connect to Braintrust MCP
The Braintrust MCP server lets OpenCode query and update Braintrust data. MCP is configured independently from the tracing plugin and its built-in tools.Set up the MCP server
Install OpenCode
Add the Braintrust MCP server
Authenticate
bt login, which authenticates the CLI and tracing integration.Verify the connection
opencode mcp list to check the connection. Restart OpenCode, then ask it to list your recent Braintrust projects and confirm that it uses the Braintrust MCP server.Use MCP tools
Once connected, OpenCode can query logs, compare experiments, work with datasets, and use other Braintrust tools. For example:- “Show me my recent Braintrust experiments.”
- “Query the last 10 logged requests with errors.”
- “Compare accuracy scores between my two latest experiments.”
Troubleshooting MCP
Braintrust MCP tools do not appear
Braintrust MCP tools do not appear
MCP authentication or connection fails
MCP authentication or connection fails
opencode mcp auth braintrust to retry OAuth authentication. If you previously configured API-key authentication, remove the Authorization header and any oauth: false setting from the Braintrust server’s entry in your OpenCode configuration before switching to OAuth.For connection and OAuth diagnostics, use opencode mcp debug braintrust. See OpenCode’s MCP documentation.For authentication methods, regional and self-hosted endpoints, and network requirements, see the Braintrust MCP guide.Next steps
- Learn the CLI: See the full
bt tracereference. - Run evaluations: Follow the evaluation guide.
- Explore MCP tools: See the Braintrust MCP tool reference.
- Browse the source: View the coding-agent plugins repository.