Python SDK configuration
Install the Braintrust Python SDK with OpenTelemetry support:.env
BraintrustSpanProcessor for simplified configuration:
opentelemetry-braintrust.py
BraintrustSpanProcessor:
api_key: The API key to use for Braintrust. Defaults to theBRAINTRUST_API_KEYenvironment variable.api_url: The URL of the Braintrust API. Defaults to theBRAINTRUST_API_URLenvironment variable orhttps://api.braintrust.devif not set.parent: The parent project or experiment to use for Braintrust. Defaults to theBRAINTRUST_PARENTenvironment variable.filter_ai_spans: Defaults toFalse. IfTrue, only AI-related spans will be sent to Braintrust.custom_filter: A function that gives you fine-grained control over which spans are sent to Braintrust. It takes a span and returns a boolean. IfTrue, the span will be sent to Braintrust. IfFalse, the span will be dropped. IfNone, don’t influence the sampling decision.
TypeScript SDK configuration
Starting with v1.0, OpenTelemetry functionality has been moved to the separate
@braintrust/otel npm package. This solves ESM build issues in Next.js (edge), Cloudflare Workers, Bun, and TanStack applications, and adds support for both OpenTelemetry v1 and v2. BraintrustSpanProcessor works with @opentelemetry/sdk-trace-base@1.x and v2 spans.If you’re upgrading from v0.x, see the upgrade guide for migration instructions.BraintrustSpanProcessor with NodeSDK:
opentelemetry-braintrust.ts
opentelemetry-braintrust.ts
BraintrustSpanProcessor:
apiKey: The API key to use for Braintrust. Defaults to theBRAINTRUST_API_KEYenvironment variable.apiUrl: The URL of the Braintrust API. Defaults to theBRAINTRUST_API_URLenvironment variable orhttps://api.braintrust.devif not set.parent: The parent project or experiment to use for Braintrust. Defaults to theBRAINTRUST_PARENTenvironment variable.filterAISpans: Defaults tofalse. Iftrue, only AI-related spans will be sent to Braintrust.customFilter: A function that gives you fine-grained control over which spans are sent to Braintrust. It takes a span and returns a boolean. Iftrue, the span will be sent to Braintrust. Iffalse, the span will be dropped. Ifnull, don’t influence the sampling decision.
OTel compatibility
Braintrust interoperates with OpenTelemetry at two levels: an OTel-compatible ID and export format, and compatibility mode.ID and export format
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. Support for the OTel-compatible format, including reading spans propagated through thex-bt-parent header or other distributed-tracing channels, differs by SDK:
- Python SDK: reads either format (auto-detecting) as of v0.3.0, and exports the OTel-compatible format by default as of v0.26.0. To export Braintrust’s legacy UUID-style format instead, set
BRAINTRUST_LEGACY_IDS=true. - TypeScript SDK: reads and exports only the legacy format by default. Enable compatibility mode with
@braintrust/otel’ssetupOtelCompat()to read either format (auto-detecting) and export the OTel-compatible one.
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.Compatibility mode
In addition to using the compatible format, compatibility mode stores the active span in OpenTelemetry’s context, so Braintrust and OpenTelemetry instrumentation contribute to the same trace within a process. Use it when running evals around OpenTelemetry-instrumented code. How you enable compatibility mode differs by SDK: in Python, set an environment variable; in TypeScript, call a setup function. The examples below enable it and send spans to Braintrust.- Python
- TypeScript
Set
BRAINTRUST_OTEL_COMPAT=true before importing Braintrust:Compatibility mode requires the
braintrust[otel] package, v0.3.0 or later. As of v0.26.0, the OTel-compatible ID and export format is the default, but to also get compatibility mode’s context sharing, set BRAINTRUST_OTEL_COMPAT=true.Python
Distributed tracing
You can do distributed tracing between services instrumented with the Braintrust SDK and OpenTelemetry, either to create OpenTelemetry spans as children of Braintrust spans or to create Braintrust spans as children of OpenTelemetry spans.These examples require the OTel integration: the
braintrust[otel] Python package (v0.3.5 or later) or the @braintrust/otel TypeScript package (v0.1.0 or later). The Python version requirement is higher than for compatibility mode (v0.3.0) because these examples use the distributed-tracing helper functions (context_from_span_export, add_span_parent_to_baggage, and parent_from_headers), which shipped in v0.3.5.These examples use
fetch and requests to make HTTP requests. The trace context can also be transmitted via message queue metadata, gRPC metadata, or any other inter-service communication mechanism that supports custom headers.Create OpenTelemetry spans as children of Braintrust spans
Export the Braintrust span context and use it to create an OpenTelemetry context.Create Braintrust spans as children of OpenTelemetry spans
Propagate the OpenTelemetry context using W3C Trace Context headers."project_name:my-project") rather than a span, use add_parent_to_baggage(parent) (Python) instead of add_span_parent_to_baggage(span).
OTLP configuration
If you are using a different language or want to use pure OTel code, you can set up the OpenTelemetry Protocol Exporter (OTLP) to send traces to Braintrust. Once you set up an OTLP exporter to send traces to Braintrust, we automatically convert LLM calls into BraintrustLLM spans, which
can be saved as prompts
and evaluated in the playground.
For JavaScript/TypeScript applications, you can use the BraintrustExporter directly:
The trace endpoint URL is
https://api.braintrust.dev/otel/v1/traces. If your exporter
uses signal-specific environment variables, you’ll need to set the full path:
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://api.braintrust.dev/otel/v1/tracesIf your organization is on the EU data plane, use
https://api-eu.braintrust.dev/otel instead.
If you’re self-hosting Braintrust, substitute your stack’s Universal API URL. For example:
OTEL_EXPORTER_OTLP_ENDPOINT=https://dfwhllz61x709.cloudfront.net/otelSee Data plane region.x-bt-parent header sets the trace’s parent project or experiment. You can use
a prefix like project_id:, project_name:, or experiment_id: here, or pass in
a span slug
(span.export()) to nest the trace under a span within the parent object.
To find your project ID, navigate to your project’s configuration page and find the Copy Project ID button at the bottom of the page.
Vercel AI SDK
The Vercel AI SDK natively supports OpenTelemetry and works out of the box with Braintrust, either via Next.js or Node.js.Next.js
If you are using Next.js, use the Braintrust exporter with@vercel/otel:
parent field.
When you call the AI SDK, make sure to set experimental_telemetry:
The integration supports streaming functions like
streamText. Each streamed call will produce ai.streamText spans in Braintrust.Node.js
If you are using Node.js without a framework, you must configure theNodeSDK directly. Here, it’s more straightforward
to use the BraintrustSpanProcessor.
First, install the necessary dependencies:
Manual tracing
If you want to log LLM calls directly to the OTel endpoint, you can set up a custom OpenTelemetry tracer and add the appropriate attributes to your spans. This gives you fine-grained control over what data gets logged.For attaching custom application metadata and tags to spans — like user IDs or request context — see Add metadata and tags.
GenAI attributes
Braintrust implements the OpenTelemetry GenAI semantic conventions. When you send traces with these attributes, they are automatically mapped to Braintrust fields.Braintrust attributes
You can also use thebraintrust namespace to set fields in Braintrust directly:
Fields mapped from
braintrust.* attributes are deleted and translated into Braintrust’s native format.
GenAI events
In addition to attributes, Braintrust also processes GenAI events on spans to extract input/output messages. These events follow the OpenTelemetry GenAI semantic conventions for events:
Events are processed in chronological order and combined with attribute-based messages to provide a complete view of the conversation flow.
Here’s an example of how to set up manual tracing:
Troubleshooting
Why are my traces not showing up?
There are a few common reasons why your traces may not show up in Braintrust:- Braintrust’s logs table only shows traces that have a root span (i.e.
span_parentsis empty). If you only send children spans, they will not appear in the logs table. A common reason for this is only sending spans to Braintrust which have atraceparentheader. To fix this, make sure to send a root span for every trace you want to appear in the UI. - Make sure the
OTEL_EXPORTER_OTLP_ENDPOINTmatches your organization’s data plane region. Organizations on the EU data plane should usehttps://api-eu.braintrust.dev/otel. Self-hosted deployments should use their custom API URL, for examplehttps://dfwhllz61x709.cloudfront.net/otel. - You must explicitly set up OpenTelemetry in your application. If you’re using Next.js, then follow the Next.js OpenTelemetry guide. If you are using Node.js without a framework, then follow this example to set up a basic exporter.
Why are some attributes missing from metadata?
For Braintrust-hosted organizations, source attributes such asai.prompt, gen_ai.input.messages, and llm.input_messages are removed from metadata once Braintrust has mapped them to a structured field (input, output, metrics, etc.). The data itself can be found on the structured field it was mapped into.
Self-hosted deployments behave the same way when the STRIP_OTEL_ATTRIBUTES_FROM_METADATA environment variable is set on the api-ts service.
To keep the raw attributes on metadata for a specific span, set the braintrust.otel.preserve_attributes attribute to true on that span.
For details, see Strip OTel attributes from metadata.