LangChain4j
cetra traces LangChain4j model calls through OpenInference’s LangChain4j instrumentation, registered as a model listener on the chat model.
Install
Section titled “Install”<dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j</artifactId> <version>1.23.0</version></dependency><dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-open-ai</artifactId> <version>1.23.0</version></dependency><dependency> <groupId>com.arize</groupId> <artifactId>openinference-instrumentation-langchain4j</artifactId> <version>0.1.9</version></dependency><dependency> <groupId>io.opentelemetry</groupId> <artifactId>opentelemetry-sdk-extension-autoconfigure</artifactId></dependency><dependency> <groupId>io.opentelemetry</groupId> <artifactId>opentelemetry-exporter-otlp</artifactId></dependency>Import the OpenTelemetry BOM 1.68.0 and its alpha BOM 1.68.0-alpha in dependencyManagement: the alpha BOM versions the incubator classes the instrumentation depends on.
Set up
Section titled “Set up”The SDK comes from OpenTelemetry’s autoconfigure, which reads the standard variables:
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:8080export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer sk-cetra-…"export OTEL_SERVICE_NAME=my-appexport OTEL_METRICS_EXPORTER=noneexport OTEL_LOGS_EXPORTER=noneThe last two lines turn the metrics and logs exporters off: autoconfigure turns both on by default, and each would try to reach an OTLP endpoint.
Register the listener OpenInference creates, and flush before the process ends:
import com.arize.instrumentation.langchain4j.LangChain4jInstrumentor;import dev.langchain4j.agent.tool.P;import dev.langchain4j.agent.tool.Tool;import dev.langchain4j.model.openai.OpenAiChatModel;import dev.langchain4j.service.AiServices;import io.opentelemetry.sdk.OpenTelemetrySdk;import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import java.util.List;import java.util.concurrent.TimeUnit;
public class Main {
public interface Assistant { String chat(String message); }
public static class WeatherTools { @Tool("Get the current weather for a city") public String get_weather(@P("city") String city) { return "Sunny in " + city + ", 21 C"; } }
public static void main(String[] args) { OpenTelemetrySdk otel = AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk(); LangChain4jInstrumentor instrumentor = LangChain4jInstrumentor.instrument(otel.getTracerProvider()); OpenAiChatModel model = OpenAiChatModel.builder() .apiKey(System.getenv("OPENAI_API_KEY")) .modelName("gpt-4o-mini") .listeners(List.of(instrumentor.createModelListener())) .build(); Assistant assistant = AiServices.builder(Assistant.class) .chatModel(model) .tools(new WeatherTools()) .build(); System.out.println("answer: " + assistant.chat("What is the weather in Lyon? Use the tool.")); otel.getSdkTracerProvider().forceFlush().join(10, TimeUnit.SECONDS); otel.close(); }}Set baseUrl on the model builder, with the /v1 prefix, when your provider is OpenAI-compatible and is not OpenAI itself.
What you get
Section titled “What you get”One AiServices call with one tool round:
- an
llmspan namedgeneratefor each model call, with the provideropenai, the model, the input and output messages, the tool definitions, the token counts and the cost computed from the built-in prices.
Limits
Section titled “Limits”- Register the model listener only. The instrumentation also offers AiService listeners (
createAiServiceListeners); the spans they create report the model asunknown, and with both kinds registered each model call is recorded twice. - There are no agent or tool spans: the model listener sees the model calls only.
- With no other span open, each model call is a trace of its own.
- Content is captured by default. Hide it with a
TraceConfigbuilt byTraceConfig.builder()and passed toLangChain4jInstrumentor.instrument(tracerProvider, traceConfig); there is no environment variable for it. - The capture sent no Authorization header; the header setting above is the library’s documented option, not part of the captured run.
- The fixture was captured with LangChain4j 1.23.0, OpenInference Java 0.1.9 and OpenTelemetry Java 1.68.0.
- If a field is missing, look at the span’s Attributes tab and compare it with Span attributes.
- Send OpenTelemetry: the endpoint and the headers cetra expects.
- Traces: read the trace as a tree and a timeline.
- Cost and tokens: how cost is computed from the token counts.