Skip to main content
Spring AI is a framework for building AI applications in Java. Braintrust traces chat calls made to OpenAI and Anthropic through Spring AI.

Setup

Install the Braintrust Java SDK alongside the Spring AI module for each provider you use, then configure your API keys.
Braintrust instruments Spring AI 1.0.0 to 2.0.0 and requires Java 17 or later. Calls made with an unsupported Spring AI version still run, but produce no spans.
1

Install packages

2

Set environment variables

.env

Auto-instrumentation

To trace Spring AI calls without modifying your application code, attach the braintrust-java-agent at JVM startup. The agent intercepts every Spring AI chat model build and applies Braintrust instrumentation automatically.
1

Add the agent dependency

The agent is a separate artifact from the SDK. Add it as its own dependency configuration:
2

Run your app

Spring AI chat model builds in your application code are now intercepted automatically. No call to BraintrustSpringAI.wrap() is required. For Spring Boot, use ./gradlew bootRun instead.

Manual instrumentation

To trace Spring AI calls manually, build your chat model as usual, then wrap it with BraintrustSpringAI.wrap(), which instruments the model in place and returns it so every call() emits a span.
AnthropicChatModel is instrumented the same way: build it, then pass it to BraintrustSpringAI.wrap().
Spring AI 2.x builds its provider client inside build(), so you wrap the finished chat model. For Spring AI 1.x, import dev.braintrust.instrumentation.springai.v1_0_0.BraintrustSpringAI and wrap the builder before calling .build() instead.
Async and streaming calls are traced, and each LLM span is parented to the caller’s active span even when the request runs on a different thread. BraintrustSpringAI.wrap() is idempotent and returns the same model instance. Only OpenAiChatModel and AnthropicChatModel are instrumented; other Spring AI chat models are logged and skipped.

What Braintrust traces

Braintrust traces each Spring AI chat model call that routes to a supported provider backend. Spring AI 2.x delegates HTTP to the official OpenAI and Anthropic Java SDKs, so spans carry the same fields as those integrations.Braintrust captures:
  • Chat model call spans for OpenAiChatModel (OpenAI backend) and AnthropicChatModel (Anthropic backend), with the request messages, model, and parameters.
  • Response content for each call.
  • Token usage metrics, including prompt and completion tokens.

Resources