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

# OpenCode

> Trace OpenCode sessions to Braintrust and access your Braintrust data through built-in tools or MCP

[OpenCode](https://opencode.ai) is an open-source AI coding assistant. You can connect it to Braintrust in two ways:

* **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](https://www.npmjs.com/package/@braintrust/trace-opencode) uses `bt` to trace your OpenCode sessions to Braintrust. The plugin also provides [built-in Braintrust tools](#built-in-braintrust-tools), independently of MCP.

### Set up tracing

> [!NOTE]
>
> If `@braintrust/trace-opencode` is already installed, follow [Update the tracing plugin](#update-the-tracing-plugin).

**1. Install OpenCode**

If you haven't already, install [OpenCode](https://opencode.ai/). The plugin requires OpenCode v1.2.25 or later.

**2. Install and authenticate bt**

Follow the [`bt` CLI quickstart](https://docs.braintrust.dev/reference/cli/quickstart) to install `bt` v0.19.3 or later, authenticate, and set your active context.

**3. Enable tracing**

Before starting OpenCode, run this command in a terminal:

```bash theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
bt trace enable opencode --project <your-project>
```

This command:

* Adds or updates [`@braintrust/trace-opencode`](https://www.npmjs.com/package/@braintrust/trace-opencode) in your global `~/.config/opencode/opencode.json`, using the `^2` version range.
* Writes `~/.config/opencode/braintrust.json`, which records whether tracing is enabled and which project receives traces.

The tracing file stores settings, not credentials. If `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`](https://docs.braintrust.dev/reference/cli/trace#bt-trace-enable).

**4. Restart OpenCode**

Restart OpenCode so it loads the plugin and tracing settings.

**5. Log a test trace**

Ask OpenCode to do something simple. When the session finishes, open your Braintrust project and confirm that the trace appears on the [\*\* Logs\*\*](https://www.braintrust.dev/app/~/logs) page.

### 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. With `bt` v0.28.0 or later, images that a tool returns stay images in the tool output.
* Subagent spans nested under the parent session's active turn when the parent is available.

Every span records its origin as `braintrust.plugin.opencode`, along with versions in `context.span_origin`: the plugin version in `version`, the `bt` version in `bt.version`, and the OpenCode version in `opencode.version`. These versions are captured when each span is created, and unknown versions are `null`. Each turn span and every span under it, including subagent spans, records the turn's span ID in `metadata.turn_span_id`.

### Common workflows

> [accordion] Use different tracing settings for one session
>
> Use [`bt trace run`](https://docs.braintrust.dev/reference/cli/trace#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`](https://docs.braintrust.dev/reference/cli/trace#bt-trace-enable) with the corresponding options. Running [`bt switch`](https://docs.braintrust.dev/reference/cli/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](https://docs.braintrust.dev/reference/cli/trace.md#tag-traces).

> [accordion] Resume an OpenCode session
>
> With plugin v2.1.1 or later, when you resume an OpenCode session, new turns are added to its existing Braintrust trace. Earlier versions start a new trace each time OpenCode restarts.

> [accordion] Stop tracing OpenCode sessions
>
> Use [`bt trace disable opencode`](https://docs.braintrust.dev/reference/cli/trace#bt-trace-disable) 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 call `bt` 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.

These tools are enabled by default. To keep tracing without registering them, set `enable_tools` to `false` in the global or project-local tracing file. `BRAINTRUST_OPENCODE_ENABLE_TOOLS` overrides that setting.

The tools run `bt` from your `PATH`. To run a different `bt` executable, set `BRAINTRUST_BT_BIN` to its path in the environment that launches OpenCode. This variable requires plugin v2.1.1 or later. Earlier versions read `BT_EXECUTABLE`, which v2.1.1 accepts as a deprecated fallback.

### Update the tracing plugin

To update the plugin, follow these steps:

> [!NOTE]
>
> If `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](#migrate-from-an-older-plugin-version).

**1. Update bt**

Follow the [CLI migration guide](https://docs.braintrust.dev/reference/cli/migrate.md) to update `bt` to the latest version and complete the applicable migrations.

**2. Update the tracing plugin**

```bash theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
bt trace update opencode
```

[`bt trace update`](https://docs.braintrust.dev/reference/cli/trace#bt-trace-update) updates the plugin's package specification in the global OpenCode configuration without changing your saved tracing configuration.

**3. Restart and verify OpenCode**

Restart OpenCode, run a short session, and confirm that its trace appears on the [\*\* Logs\*\*](https://www.braintrust.dev/app/~/logs) page in the intended Braintrust project.

### Migrate from an older plugin version

To migrate to the latest version of the `@braintrust/trace-opencode` plugin, follow these steps:

**1. Inspect your OpenCode plugins**

Inspect your active OpenCode configuration and find `@braintrust/trace-opencode` in the `plugin` list:

```bash theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
opencode debug config
```

Record the plugin's starting version before updating. Also record the profile, organization, project, metadata, and custom tracing settings you want to preserve from the global tracing file and any project-local `.opencode/braintrust.json`.

> [!NOTE]
>
> If the plugin is not installed, follow [Set up tracing](#set-up-tracing). If you cannot determine the installed version from its configured version range, complete both migration sections below.

**2. Install or update bt**

These steps require `bt` v0.19.3 or later. Check whether `bt` is installed:

```bash theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
bt --version
```

* If the command succeeds, [migrate to the latest version](https://docs.braintrust.dev/reference/cli/migrate.md) (unless you've already done so).
* Otherwise, [install `bt`](https://docs.braintrust.dev/reference/cli/quickstart), authenticate, and set organization and project context.

**3. Complete version-specific migration steps**

> [!NOTE]
>
> Complete each section for a version newer than your starting `@braintrust/trace-opencode` version, beginning with the oldest. If no sections apply, continue to the next step.

> [accordion] @braintrust/trace-opencode v1.0.0
>
> > [!NOTE]
> >
> > Complete this section if you started your migration from a plugin version earlier than v1.0.0.
>
> Before v1.0.0, the plugin handled authentication and trace delivery itself. Starting in 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`](https://docs.braintrust.dev/reference/cli/login). Keep credentials private and retain any settings needed by other integrations.

> [accordion] @braintrust/trace-opencode v2.0.0
>
> > [!NOTE]
> >
> > Complete this section if you started your migration from a plugin version earlier than v2.0.0.
>
> Before v2.0.0, the following environment variables overrode saved tracing settings. Starting in v2.0.0, the plugin no longer reads these overrides from the agent's environment. Record any values you want to preserve:
>
> * `TRACE_TO_BRAINTRUST`: Whether tracing was enabled.
>
> * `BRAINTRUST_PROFILE` and `BRAINTRUST_ORG_NAME`: The tracing account and organization.
>
> * `BRAINTRUST_PROJECT`: The project that received traces.
>
> * `BRAINTRUST_ADDITIONAL_METADATA`: Extra root-span metadata.
>
> > [!NOTE]
> >
> > In the next step, pass the recorded project with `--project`, account and organization with `--profile` and `--org`, and metadata with `--additional-metadata '<JSON>'`. This preserves values that previously overrode your saved configuration.
>
> v2.0.0 also changes how conflicting settings in `braintrust.json` are resolved:
>
> * Profile, organization, and project values in the `route` object 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_metadata` can still override `route.additional_metadata`.
>
> The next step moves your saved settings into the `route` object so you do not leave conflicting copies. Environment variables accepted by `bt` commands are separate from these plugin changes. See [`bt trace`](https://docs.braintrust.dev/reference/cli/trace).

**4. Apply your tracing settings**

Run this command with your intended project, using the project you recorded earlier to preserve your destination:

```bash theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
bt trace enable opencode --project <your-project>
```

Pass `--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_metadata` is preserved unless you pass `--additional-metadata`. If it is absent, `bt` migrates top-level `additional_metadata` instead.
* Existing `route.tags` is preserved unless you pass `--tag` or set `BRAINTRUST_TAGS`.
* `route.flush_mode` resets to `fire_and_forget`. Restore its previous value if you customized it.
* Restore any other custom tracing settings you recorded before updating.

The command also removes obsolete top-level destination and authentication keys from the global tracing file. It does not migrate project-local files. For each project-local file, use the `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.

**5. Restart and verify OpenCode**

Restart OpenCode, run a short session, and confirm that its trace appears on the [\*\* Logs\*\*](https://www.braintrust.dev/app/~/logs) page in the intended Braintrust project. Check each project where you changed local tracing settings.

**6. Remove obsolete settings**

> [!NOTE]
>
> Complete this step only if you started with a plugin version earlier than v2.0.0 and verified that traces reach the intended project in the previous step.

> [accordion] Settings used before @braintrust/trace-opencode v2.0.0
>
> The plugin now reads tracing settings from its configuration files. Environment variables you previously configured for tracing may no longer be needed.
>
> Check these locations for leftover settings:
>
> * Your shell configuration
> * The global tracing file and your project's `.opencode/braintrust.json`
>
> Remove the settings below if they were used only for tracing. Keep any that other `bt` commands, MCP connections, or integrations still need:
>
> * `TRACE_TO_BRAINTRUST`, `BRAINTRUST_PROFILE`, `BRAINTRUST_ORG_NAME`, `BRAINTRUST_PROJECT`, and `BRAINTRUST_ADDITIONAL_METADATA` in the agent environment
> * `BRAINTRUST_API_KEY`, `BRAINTRUST_API_URL`, and `BRAINTRUST_APP_URL` used by pre-v1.0.0 tracing
> * Top-level `profile`, `org_name`, `project`, and `additional_metadata` after preserving their values under `route`
> * Pre-v1.0.0 `api_key`, `api_url`, `app_url`, `log_to_file`, and `queue_size` keys
> * Pre-v1.0.0 `LOG_TO_FILE` and `BRAINTRUST_QUEUE_SIZE` environment variables
>
> Keep `BRAINTRUST_OPENCODE_ENABLE_TOOLS` if you use it to control the built-in tools.

### Troubleshooting tracing

First, run `bt trace doctor opencode`. Then, review the following:

> [accordion] The tracing plugin is missing or disabled
>
> Run `opencode debug config` and confirm that `@braintrust/trace-opencode@^2` is in the `plugin` list. If it is missing or broken, run:
>
> ```bash theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
> bt trace enable opencode --project <your-project>
> ```
>
> Restart OpenCode afterward.

> [accordion] Traces do not appear after setup
>
> If OpenCode was running when its plugin or configuration changed, restart it.
>
> Run `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](https://docs.braintrust.dev/reference/cli/trace.md#project-log-permissions).
>
> For authentication, tracing destination, or endpoint problems, see [`bt trace` troubleshooting](https://docs.braintrust.dev/reference/cli/trace#troubleshooting) and the [CLI migration troubleshooting guide](https://docs.braintrust.dev/reference/cli/migrate.md#troubleshooting).
>
> Tracing failures do not interrupt the OpenCode session.

> [accordion] OpenCode uses an unexpected configuration directory
>
> Check whether `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`.

> [accordion] Traces use unexpected settings
>
> Invocation settings supplied by `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`](https://docs.braintrust.dev/reference/cli/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.

**1. Choose authentication**

> [tab] OAuth (recommended)
>
> Use OAuth when your client supports it. Your client opens a Braintrust authorization page where you sign in and approve access. The server acts with the permissions of the account you sign in with.
>
> * No Braintrust API key required
> * No OAuth client ID or client secret to configure
>
> The server implements OAuth 2.0 with dynamic client registration and publishes its metadata at `/.well-known/oauth-authorization-server` on the same host as your MCP endpoint (see next step), so a client can register itself.

> [tab] API key
>
> Use an API key for clients without OAuth support or for programmatic connections. The server acts with the permissions of the account the key belongs to.
>
> 1. Create a key in \*\* Settings\*\* > [\*\* API keys\*\*](https://www.braintrust.dev/app/~/configuration/api-keys).
> 2. Configure your client to send this header on every request, replacing `YOUR_BRAINTRUST_API_KEY` with your key:
>
>    ```text theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
>    Authorization: Bearer YOUR_BRAINTRUST_API_KEY
>    ```

> [!NOTE]
>
> Your [`bt login`](https://docs.braintrust.dev/reference/cli/login) doesn't authenticate the MCP server.

**2. Choose your endpoint**

Use the endpoint for your organization's [data plane region](https://docs.braintrust.dev/admin/organizations.md#data-plane-region):

| Region | MCP endpoint |
| - | - |
| US | `https://api.braintrust.dev/mcp` |
| EU | `https://api-eu.braintrust.dev/mcp` |

Other connection details:

* **Self-hosted:** Copy the **MCP URL** from \*\* Settings\*\* > [\*\* Data plane\*\*](https://www.braintrust.dev/app/~/configuration/data-plane). This is your Braintrust API URL with `/mcp` appended.
* **Transport:** If your client asks you to choose a transport, choose HTTP (also called streamable HTTP). SSE-only clients can't connect.

**3. Add the server to OpenCode**

Follow the [OpenCode guide](https://opencode.ai/docs/mcp-servers/#enable) to add the remote server.

**4. Verify the connection**

To verify the connection, ask OpenCode to list your recent Braintrust projects. If it can't connect, see [Braintrust MCP troubleshooting](https://docs.braintrust.dev/integrations/developer-tools/mcp.md#troubleshooting).

**5. Start using Braintrust tools**

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."

See [what the Braintrust MCP can do](https://docs.braintrust.dev/integrations/developer-tools/mcp.md#what-the-mcp-can-do) for more workflows and example prompts.

## Next steps

* **Learn the CLI**: See the full [`bt trace` reference](https://docs.braintrust.dev/reference/cli/trace).
* **Run evaluations**: Follow the [evaluation guide](https://docs.braintrust.dev/evaluate/run-evaluations.md).
* **Explore MCP tools**: See the [Braintrust MCP tool reference](https://docs.braintrust.dev/integrations/developer-tools/mcp.md#tools).
* **Browse the source**: View the [coding-agent plugins repository](https://github.com/braintrustdata/braintrust-coding-agent-plugins).


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