Share context within a process
Compatibility mode stores the active span in OpenTelemetry’s context, so Braintrust and OpenTelemetry instrumentation contribute to the same trace within a process. For example, use it to nest OpenTelemetry spans under a Braintrust evaluation span.Call
setupOtelCompat() before creating any loggers or spans, and use AsyncLocalStorageContextManager to nest spans under their parent evaluation spans:Compatibility mode requires the
@braintrust/otel package, v0.1.0 or later.TypeScript
Propagate context across services
Pass the parent span’s context to the receiving service so its spans join the same trace. Choose the example that matches which SDK creates the parent span.These examples require
@braintrust/otel v0.1.0 or later and use fetch to pass context in HTTP headers.When linking Python and TypeScript services, align their ID formats.
Braintrust parent to OpenTelemetry child
Export the Braintrust span context and use it to create an OpenTelemetry context.OpenTelemetry parent to Braintrust child
Propagate the OpenTelemetry context using W3C Trace Context headers.ID format compatibility
ID format compatibility
Braintrust can represent span and trace IDs as OTel-compatible hexadecimal values and serialize them in a shared export format, so exported spans interoperate with OpenTelemetry.
For distributed tracing between Python and TypeScript services, make sure both services use the same format. Either set
BRAINTRUST_LEGACY_IDS=true on the Python service, or enable compatibility mode on the TypeScript service.The TypeScript SDK reads and exports only the legacy ID and export format by default. Enable compatibility mode with
setupOtelCompat() to read either format (auto-detecting) and export the OTel-compatible format, including when propagating spans through x-bt-parent or other distributed-tracing channels.