Class MicrometerMetricsChatModelListener

java.lang.Object
dev.langchain4j.micrometer.metrics.listeners.MicrometerMetricsChatModelListener
All Implemented Interfaces:
dev.langchain4j.model.chat.listener.ChatModelListener

@Experimental public class MicrometerMetricsChatModelListener extends Object implements dev.langchain4j.model.chat.listener.ChatModelListener
A ChatModelListener that uses a Micrometer MeterRegistry to collect metrics about chat model interactions following OpenTelemetry Semantic Conventions for Generative AI.

This listener records token usage metrics (input and output tokens) when a chat model response is received, using a DistributionSummary consistent with the OpenTelemetry Semantic Conventions for gen_ai.client.token.usage.

Histogram publishing and bucket boundaries are not configured by this listener. Users can enable histograms and set bucket boundaries through their MeterRegistry configuration (e.g., via Spring Boot properties or MeterFilter).

Note: The MicrometerMetricsChatModelListener must be instantiated separately (e.g., via Spring Boot auto-configuration or manual instantiation).

  • Constructor Details

    • MicrometerMetricsChatModelListener

      public MicrometerMetricsChatModelListener(io.micrometer.core.instrument.MeterRegistry meterRegistry)
      Parameters:
      meterRegistry - the MeterRegistry to register metrics with
  • Method Details

    • onRequest

      public void onRequest(dev.langchain4j.model.chat.listener.ChatModelRequestContext requestContext)
      Specified by:
      onRequest in interface dev.langchain4j.model.chat.listener.ChatModelListener
    • onResponse

      public void onResponse(dev.langchain4j.model.chat.listener.ChatModelResponseContext responseContext)
      Specified by:
      onResponse in interface dev.langchain4j.model.chat.listener.ChatModelListener
    • onError

      public void onError(dev.langchain4j.model.chat.listener.ChatModelErrorContext errorContext)
      Specified by:
      onError in interface dev.langchain4j.model.chat.listener.ChatModelListener