> ## Documentation Index
> Fetch the complete documentation index at: https://typesafe-sdk-go.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Use with Braintrust

> Automatic tracing of typesafe-sdk-go with braintrust-sdk-go

[`trace/contrib/typesafe`](https://github.com/atharvamhaske/braintrust-sdk-go/tree/feat/typesafe-tracing/trace/contrib/typesafe) is an HTTP middleware for [braintrust-sdk-go](https://github.com/braintrustdata/braintrust-sdk-go) that traces every `POST /v1/systemone` call made through this SDK. It wraps the `*http.Client` passed to `typesafe.WithHTTPClient`, so no manual span code is needed in application code.

<Warning>
  This package lives on the `feat/typesafe-tracing` branch of a fork and is not merged upstream yet. The install step below points at that branch directly.
</Warning>

## Install

```bash theme={null}
go get github.com/atharvamhaske/braintrust-sdk-go/trace/contrib/typesafe@feat/typesafe-tracing
```

## Usage

```go theme={null}
tp := trace.NewTracerProvider()
defer tp.Shutdown(context.Background())
otel.SetTracerProvider(tp)

bt, err := braintrust.New(tp, braintrust.WithProject("my-project"))
if err != nil {
    log.Fatal(err)
}

client, err := typesafe.NewClient(
    typesafe.WithHTTPClient(bttypesafe.Client()), // reads TYPESAFE_API_KEY
)
if err != nil {
    log.Fatal(err)
}

// every SystemOne call is now traced automatically
resp, err := client.SystemOne(ctx, state, questions)
```

## Span shape

The middleware matches the span shape the official Python and JS TypeSafe integrations use, not the generic `llm` shape used for chat completion providers elsewhere in braintrust-sdk-go.

| Field                             | Value                                                        |
| --------------------------------- | ------------------------------------------------------------ |
| Span name                         | `typesafe.systemOne`                                         |
| `braintrust.span_attributes.type` | `question`                                                   |
| `braintrust.input_json`           | `state`, plus `questions` as an id-tagged list               |
| `braintrust.output_json`          | `answers` as an id-tagged list                               |
| `braintrust.metadata`             | `provider: "typesafe"`, `model`                              |
| `braintrust.metrics`              | `prompt_tokens`, `completion_tokens`, `tokens`, from `usage` |

<Card title="Full runnable example" icon="github" href="https://github.com/atharvamhaske/typesafe-sdk-go/tree/main/examples/braintrust">
  `examples/braintrust/main.go`. It has its own `go.mod`, so `braintrust-sdk-go` and OpenTelemetry stay out of the core SDK's dependency graph.
</Card>

## Custom TracerProvider

```go theme={null}
httpClient := bttypesafe.Client(bttypesafe.WithTracerProvider(tp))
client, err := typesafe.NewClient(typesafe.WithHTTPClient(httpClient))
```

## Any other OpenTelemetry backend

The middleware sets standard OpenTelemetry span attributes, so it works with Datadog, Honeycomb, or a raw OTel collector too. Only the `braintrust.*` attribute names are Braintrust-specific.
