Class UsageStatistics

java.lang.Object
org.machanism.machai.ai.manager.UsageStatistics

public class UsageStatistics extends Object
Central registry for aggregated GenAI token-usage statistics.

Usage entries are grouped by model identifier and can later be logged or queried programmatically. All access to the internal storage is synchronized on the shared map instance.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final org.slf4j.Logger
    Logger used for reporting aggregated token usage.
    private static final Map<String,List<Usage>>
    In-memory registry of usage entries grouped by model identifier.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Private constructor to prevent instantiation of this usage statistics utility class.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    addUsage(String modelId, Usage usage)
    Adds a single Usage record for a specific model identifier.
    static Map<String,List<Usage>>
    Returns the aggregated usage map for all models.
    static List<Usage>
    Returns the aggregated usage entries for a specific model.
    static void
    Initializes the usage statistics module.
    static void
    Logs usage summaries for every model currently present in the registry.
    static void
    Logs a summary of the aggregated Usage records for a specific model.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • logger

      private static final org.slf4j.Logger logger
      Logger used for reporting aggregated token usage.
    • modelUsages

      private static final Map<String,List<Usage>> modelUsages
      In-memory registry of usage entries grouped by model identifier.
  • Constructor Details

    • UsageStatistics

      private UsageStatistics()
      Private constructor to prevent instantiation of this usage statistics utility class.
  • Method Details

    • init

      public static void init()
      Initializes the usage statistics module.

      This method can be called during system startup to force class loading, ensuring the internal static structures are pre-loaded and ready for tracking.

    • addUsage

      public static void addUsage(String modelId, Usage usage)
      Adds a single Usage record for a specific model identifier.
      Parameters:
      modelId - the model identifier, for example OpenAI:gpt-4o-mini
      usage - the usage entry to record
    • logUsage

      public static void logUsage()
      Logs usage summaries for every model currently present in the registry.
    • logUsageForModel

      public static void logUsageForModel(String modelId)
      Logs a summary of the aggregated Usage records for a specific model.
      Parameters:
      modelId - the model identifier, for example OpenAI:gpt-4o-mini
    • getUsageForModel

      public static List<Usage> getUsageForModel(String modelId)
      Returns the aggregated usage entries for a specific model.

      A defensive copy is returned so callers can inspect the recorded values without modifying the internal registry state.

      Parameters:
      modelId - the model identifier to query
      Returns:
      a defensive copy of the recorded usage entries for the specified model; never null
    • getAllModelUsages

      public static Map<String,List<Usage>> getAllModelUsages()
      Returns the aggregated usage map for all models.

      The returned map is a shallow copy of the registry. The map instance itself can be modified by the caller without affecting the registry, but the nested usage lists remain shared references.

      Returns:
      a shallow copy of the model-to-usage-list registry