Skip to main content
This page outlines common issues when setting up and using the Braintrust .NET SDK and how to resolve them.
Initialize Braintrust with Braintrust.Get() before you create your provider client. Braintrust.Get() sets up the OpenTelemetry pipeline that exports spans to Braintrust, so calls made before it runs are not traced.Make your AI calls on the wrapped client returned by the integration, not the original client:
#skip-compile
var braintrust = Braintrust.Sdk.Braintrust.Get();
var activitySource = braintrust.GetActivitySource();

// Trace calls on `client`, not on a separately constructed client
var client = BraintrustOpenAI.WrapOpenAI(activitySource, apiKey);
By default, the SDK manages OpenTelemetry and registers a process-exit hook that flushes traces when your app terminates, so even short-lived processes export correctly with no extra work.If you opt out of auto-management and wire Braintrust into your own TracerProviderBuilder (via OpenTelemetryEnable), that hook isn’t registered. In that case, force a flush before the process exits:
#skip-compile
using Braintrust.Sdk.Trace;

BraintrustTracing.ForceFlush();
ForceFlush returns true when the flush completes within the timeout (10 seconds by default).
Spans route to BRAINTRUST_DEFAULT_PROJECT_NAME, which defaults to default-dotnet-project when unset. Set the project in the environment:
BRAINTRUST_DEFAULT_PROJECT_NAME="My project"
Or pass it through configuration:
#skip-compile
using Braintrust.Sdk;
using Braintrust.Sdk.Config;

var config = BraintrustConfig.Of(("BRAINTRUST_DEFAULT_PROJECT_NAME", "My project"));
var braintrust = Braintrust.Sdk.Braintrust.Get(config);
The SDK looks for your API key in the BRAINTRUST_API_KEY environment variable, then in a .env.braintrust file searched upward from the working directory. Set the variable:
BRAINTRUST_API_KEY="your-api-key"
Create an API key in API key settings if you don’t have one.
Enable debug logging to surface configuration and export issues, and console trace logging to confirm spans are being created before they’re exported:
BRAINTRUST_DEBUG=true
BRAINTRUST_ENABLE_TRACE_CONSOLE_LOG=true