# LangChain4j Trace LangChain4j model calls with cetra from Java through OpenInference's model listener: messages, tool definitions, tokens and cost. cetra traces LangChain4j model calls through OpenInference's LangChain4j instrumentation, registered as a model listener on the chat model. ## Install ```xml dev.langchain4j langchain4j 1.23.0 dev.langchain4j langchain4j-open-ai 1.23.0 com.arize openinference-instrumentation-langchain4j 0.1.9 io.opentelemetry opentelemetry-sdk-extension-autoconfigure io.opentelemetry opentelemetry-exporter-otlp ``` 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 The SDK comes from OpenTelemetry's autoconfigure, which reads the standard variables: ```bash export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:8080 export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer sk-cetra-…" export OTEL_SERVICE_NAME=my-app export OTEL_METRICS_EXPORTER=none export OTEL_LOGS_EXPORTER=none ``` The 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: ```java 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 One `AiServices` call with one tool round: - an `llm` span named `generate` for each model call, with the provider `openai`, the model, the input and output messages, the tool definitions, the token counts and the cost computed from the built-in prices. ## Limits - Register the model listener only. The instrumentation also offers AiService listeners (`createAiServiceListeners`); the spans they create report the model as `unknown`, 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 `TraceConfig` built by `TraceConfig.builder()` and passed to `LangChain4jInstrumentor.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](https://cetra.sh/docs/reference/attributes/). ## Next - [Send OpenTelemetry](https://cetra.sh/docs/get-started/opentelemetry/): the endpoint and the headers cetra expects. - [Traces](https://cetra.sh/docs/tracing/traces/): read the trace as a tree and a timeline. - [Cost and tokens](https://cetra.sh/docs/tracing/cost-and-tokens/): how cost is computed from the token counts.