Class ModelTelemetryHelper

java.lang.Object
com.google.genkit.ai.telemetry.ModelTelemetryHelper

public class ModelTelemetryHelper extends Object
ModelTelemetryHelper provides utilities for recording model telemetry.

This helper should be used when invoking models to automatically record metrics like latency, token counts, and error rates.

  • Constructor Details

    • ModelTelemetryHelper

      public ModelTelemetryHelper()
  • Method Details

    • runWithTelemetry

      public static ModelResponse runWithTelemetry(String modelName, String featureName, String path, ModelRequest request, ModelTelemetryHelper.ModelExecutor modelFn) throws GenkitException
      Executes a model call with automatic telemetry recording.
      Parameters:
      modelName - the model name
      featureName - the feature/flow name
      path - the span path
      request - the model request
      modelFn - the function that executes the model
      Returns:
      the model response
      Throws:
      GenkitException - if model execution fails
    • calculateBasicUsage

      public static Usage calculateBasicUsage(ModelRequest request, ModelResponse response)
      Calculates basic usage statistics from request and response.
      Parameters:
      request - the model request
      response - the model response
      Returns:
      calculated usage statistics
    • runWithTelemetryStreaming

      public static ModelResponse runWithTelemetryStreaming(String modelName, String featureName, String path, ModelRequest request, ModelTelemetryHelper.StreamingModelExecutor modelFn) throws GenkitException
      Executes a streaming model call with automatic telemetry recording.
      Parameters:
      modelName - the model name
      featureName - the feature/flow name
      path - the span path
      request - the model request
      modelFn - the function that executes the model with streaming
      Returns:
      the model response
      Throws:
      GenkitException - if model execution fails