@braintrust/trace-opencode

中文文档
2.1.1发布于 2026/10/21MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 4034

Automatically trace OpenCode conversations to Braintrust. Captures user messages, assistant responses, and tool calls for observability.

原始使用文档
文档目录10 节
  1. Quickstart
  2. Compatibility
  3. Configuration
  4. Settings
  5. Disable Braintrust tools
  6. Add root metadata
  7. Trace structure
  8. Manage tracing
  9. Runtime architecture
  10. Development

@braintrust/trace-opencode

Trace OpenCode sessions in Braintrust. The plugin sends events to the local bt daemon, which builds and uploads traces. OpenCode keeps running if tracing fails.

  • Session spans: Root span for each OpenCode session with metadata (workspace, hostname, etc.)
  • Turn spans: Captures each user-assistant interaction
  • LLM spans: Records completed model messages with available inputs, outputs, and usage
  • Tool spans: Records individual tool executions with inputs and outputs

Quickstart

Install OpenCode and the Braintrust CLI, then run:

bt login
bt trace enable opencode --project my-coding-agent
opencode

This registers the npm plugin and saves its configuration. Use --profile or --org to choose a profile or organization. Restart OpenCode if it is already open.

For one invocation without changing global tracing configuration:

bt trace run --project my-coding-agent opencode -- run "summarize this repository"

Historical import and live attach are not supported for OpenCode.

Compatibility

The package declares @opencode-ai/plugin and @opencode-ai/sdk peers at >=1.2.25. CI checks package installation at that minimum and at latest, and runs real-agent integration tests with the latest OpenCode CLI.

Configuration

Settings load from these files, with project settings overriding global settings:

  • Global: $XDG_CONFIG_HOME/opencode/braintrust.json, or ~/.config/opencode/braintrust.json if XDG_CONFIG_HOME is unset
  • Project: .opencode/braintrust.json

bt trace run overrides tracing settings for one invocation. Files are merged at the top level: a project route replaces the global route.

{
  "trace_to_braintrust": true,
  "enable_tools": true,
  "route": {
    "auth": { "profile": "work", "org_name": "acme" },
    "destination": { "type": "project_logs", "project_name": "my-project" }
  },
  "debug": true
}

Settings

Config keyDefaultPurpose
trace_to_braintrustfalseEnable tracing
enable_toolstrueRegister Braintrust tools; override with BRAINTRUST_OPENCODE_ENABLE_TOOLS
route.auth.profile_idunsetSaved profile ID written by bt setup
route.auth.profilecurrent bt profileSelect a profile by name
route.auth.org_nameprofile defaultSelect an organization
route.destinationproject logs in opencodeSelect the trace destination
route.additional_metadataunsetAdd root-span metadata
route.flush_modefire_and_forget in the default routeControl delivery flushing
debugfalseEnable debug logging; override with BRAINTRUST_DEBUG

Older files can still use top-level profile, org_name, project, and additional_metadata. For new files, use bt trace enable or the nested route format above. Include a destination when supplying a route.

Only enable_tools and debug read environment variables directly. Tracing settings come from these files or bt trace run.

Disable Braintrust tools

Set enable_tools to false to trace OpenCode sessions without registering Braintrust-branded tools (braintrust_query_logs, braintrust_list_projects, braintrust_log_data, braintrust_get_experiments):

{
  "trace_to_braintrust": true,
  "enable_tools": false,
  "route": {
    "destination": { "type": "project_logs", "project_name": "my-project" }
  }
}

Or use the environment variable:

BRAINTRUST_OPENCODE_ENABLE_TOOLS=false opencode

Add root metadata

Set route.additional_metadata to add fields to the root span:

For one invocation without changing the persistent configuration, use bt trace run --additional-metadata '{"ci": true, "run_id": "abc-123"}' opencode -- run "do the thing", or set BRAINTRUST_ADDITIONAL_METADATA before that command (bt trace run still accepts it; a plain opencode session's plugin does not).

You can also set it via the config file:

{
  "route": {
    "destination": { "type": "project_logs", "project_name": "my-project" },
    "additional_metadata": { "team": "platform" }
  }
}

The value must be a JSON object. Built-in session metadata wins if keys conflict.

Trace structure

The daemon reconstructs sessions, turns, model messages, and tool executions:

Session (task)
├── Turn 1 (task)
│   ├── Model response (llm)
│   ├── Tool execution (tool)
│   └── Model response (llm)
└── Turn 2 (task)

Model usage and metadata are included when OpenCode provides them. Child sessions are attached to their parent's active turn when that relationship is available to the translator.

Manage tracing

bt trace doctor opencode
bt trace status
bt trace update opencode
bt trace disable opencode

Runtime architecture

Tracing sends events to the local daemon over JSON-RPC. The four optional Braintrust tools call bt commands. Both use bt for credentials and API access.

Development

From the monorepo root, run make validate-opencode to build, check, and test the package. See the contribution guide for development instructions. The npm package contains compiled entrypoints; source changes belong in the monorepo.

版本与发布记录

@braintrust/trace-opencode 共发布 14 个版本,首次发布于 2026/02/10;过去 12 个月发布了 14 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.1.1latest2026/10/2118 天前
2.1.02026/09/161 个月前
2.0.12026/09/101 个月前
2.0.02026/09/022 个月前
1.0.02026/08/132 个月前
0.1.02026/07/094 个月前

关于 @braintrust/trace-opencode 的常见问题

@braintrust/trace-opencode 是什么?

Automatically trace OpenCode conversations to Braintrust. Captures user messages, assistant responses, and tool calls for observability.

如何安装 @braintrust/trace-opencode?

在项目目录运行 npm install @braintrust/trace-opencode,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@opencode-ai/sdk(>=1.2.25)、@opencode-ai/plugin(>=1.2.25)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@braintrust/trace-opencode 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@braintrust/trace-opencode 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@braintrust/trace-opencode 可以商用吗?

@braintrust/trace-opencode 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@braintrust/trace-opencode 的下载量有多少?

根据 npm 官方下载统计,@braintrust/trace-opencode 在 2026/10/09 – 2026/11/07 这 30 天内被下载 4,034 次,日均约 134 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@braintrust/trace-opencode 还在维护吗?

最新版本 2.1.1 发布于 2026/10/21(18 天前);至今共发布 14 个版本,其中过去 12 个月发布了 14 个;首次发布于 2026/02/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。