Interface CostTracker

All Known Implementing Classes:
InMemoryCostTracker

public interface CostTracker
Interface for tracking LLM API usage costs.

CostTracker provides a unified interface for recording and analyzing LLM usage across different providers and models.

Features

  • Record individual API call usage
  • Query usage by time range, model, or provider
  • Get aggregated statistics
  • Export usage data

Usage Example


 CostTracker tracker = new InMemoryCostTracker();

 // Record usage
 UsageRecord record = UsageRecord.builder()
     .modelId("gpt-4o")
     .inputTokens(1000)
     .outputTokens(500)
     .build();
 tracker.record(record);

 // Get statistics
 CostStatistics stats = tracker.getStatistics();
 System.out.println("Total cost: $" + stats.totalCost());
 
  • Method Details

    • record

      void record(UsageRecord record)
      Records a usage record.
      Parameters:
      record - The usage record to track
    • getRecords

      List<UsageRecord> getRecords()
      Gets all recorded usage records.
      Returns:
      List of all usage records
    • getRecords

      List<UsageRecord> getRecords(Instant from, Instant to)
      Gets usage records within a time range.
      Parameters:
      from - Start time (inclusive)
      to - End time (exclusive)
      Returns:
      List of usage records in the range
    • getRecordsByModel

      List<UsageRecord> getRecordsByModel(String modelId)
      Gets usage records for a specific model.
      Parameters:
      modelId - The model identifier
      Returns:
      List of usage records for the model
    • getRecordsByProvider

      List<UsageRecord> getRecordsByProvider(String provider)
      Gets usage records for a specific provider.
      Parameters:
      provider - The provider name
      Returns:
      List of usage records for the provider
    • getTotalCost

      BigDecimal getTotalCost()
      Gets the total cost across all records.
      Returns:
      Total cost in USD
    • getTotalCost

      BigDecimal getTotalCost(Instant from, Instant to)
      Gets the total cost within a time range.
      Parameters:
      from - Start time (inclusive)
      to - End time (exclusive)
      Returns:
      Total cost in USD for the range
    • getStatistics

      Gets aggregated statistics.
      Returns:
      Cost statistics
    • getStatistics

      CostTracker.CostStatistics getStatistics(Instant from, Instant to)
      Gets statistics for a time range.
      Parameters:
      from - Start time (inclusive)
      to - End time (exclusive)
      Returns:
      Cost statistics for the range
    • getCostByModel

      Map<String,BigDecimal> getCostByModel()
      Gets cost breakdown by model.
      Returns:
      Map of model ID to total cost
    • getCostByProvider

      Map<String,BigDecimal> getCostByProvider()
      Gets cost breakdown by provider.
      Returns:
      Map of provider name to total cost
    • clear

      void clear()
      Clears all recorded data.
    • getRecordCount

      int getRecordCount()
      Gets the record count.
      Returns:
      Number of recorded usage records