Class MicrometerMetricsChatModelListener

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

@Experimental public class MicrometerMetricsChatModelListener extends Object implements 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(ChatModelRequestContext requestContext)
      Description copied from interface: ChatModelListener
      This method is called before the request is sent to the model.
      Specified by:
      onRequest in interface ChatModelListener
      Parameters:
      requestContext - The request context. It contains the ChatRequest and attributes. The attributes can be used to pass data between methods of this listener or between multiple listeners.
    • onResponse

      public void onResponse(ChatModelResponseContext responseContext)
      Description copied from interface: ChatModelListener
      This method is called after the response is received from the model.
      Specified by:
      onResponse in interface ChatModelListener
      Parameters:
      responseContext - The response context. It contains ChatResponse, corresponding ChatRequest and attributes. The attributes can be used to pass data between methods of this listener or between multiple listeners.
    • onError

      public void onError(ChatModelErrorContext errorContext)
      Description copied from interface: ChatModelListener
      This method is called when an error occurs during interaction with the model.
      Specified by:
      onError in interface ChatModelListener
      Parameters:
      errorContext - The error context. It contains the error, corresponding ChatRequest, partial ChatResponse (if available) and attributes. The attributes can be used to pass data between methods of this listener or between multiple listeners.