Setup
Install the Braintrust Java SDK alongside LangChain4j and its OpenAI module, then configure your API keys.Braintrust instruments LangChain4j 1.8.0 or later and requires Java 17 or later. The OpenAI Responses API models (
OpenAiResponsesChatModel, OpenAiResponsesStreamingChatModel) require LangChain4j 1.14.0 or later.1
Install packages
2
Set environment variables
.env
Auto-instrumentation
To trace LangChain4j calls without modifying your application code, attach thebraintrust-java-agent at JVM startup. The agent intercepts every LangChain4j OpenAI 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
BraintrustLangchain.wrap() is required. For Spring Boot, use ./gradlew bootRun instead.Manual instrumentation
To trace LangChain4j calls manually, get anOpenTelemetry instance from Braintrust, then wrap your OpenAI model with BraintrustLangchain.wrap(). The wrap instruments the model in place and returns it, so every call emits a span.OpenAiStreamingChatModel, OpenAiResponsesChatModel, and OpenAiResponsesStreamingChatModel are instrumented the same way: build the model, then pass it (or its builder) to BraintrustLangchain.wrap(). The Responses API models require LangChain4j 1.14.0 or later.For LangChain4j 1.8.0 through 1.13.x, import
dev.braintrust.instrumentation.langchain.v1_8_0.BraintrustLangchain instead. That module traces OpenAiChatModel and OpenAiStreamingChatModel, but not the Responses API models, which do not exist before 1.14.0.AiServices agents and tools
Pass anAiServices builder to BraintrustLangchain.wrap() to trace a declarative agent. Braintrust instruments the underlying model, every @Tool method, and each service method call, and keeps concurrent tool calls linked to the parent trace.<Interface>.<method> (for example, Assistant.chat), with a child LLM span for the model call and a tool span for each @Tool invocation.What Braintrust traces
Braintrust traces LangChain4j OpenAI models:OpenAiChatModel and OpenAiStreamingChatModel, plus OpenAiResponsesChatModel and OpenAiResponsesStreamingChatModel on LangChain4j 1.14.0 or later.Braintrust captures:- LLM calls as separate spans, with request messages (or the Responses API
input), model name, and serialized generation parameters in span metadata. - Response content, reconstructed by accumulating chunks on streaming calls.
- Token usage on LLM spans (
prompt_tokens,completion_tokens,tokens,prompt_cached_tokens,completion_reasoning_tokens). time_to_first_tokenon streaming responses, measured from the first chunk that carries generated output.- The OpenAI request ID (
x-request-id) from the response headers and the response object ID (response_id), so you can correlate a span with OpenAI’s API logs. AiServicesmethod calls as spans named<Interface>.<method>, with each@Toolinvocation as a nestedtoolspan that records its arguments and result.- Server-side tool calls in the Responses API (web search, file search, code interpreter, and MCP) as nested
toolspans. - Errors captured on the span.