Package com.tnsai.llm.cost
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());
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final recordAggregated cost statistics. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all recorded data.Gets cost breakdown by model.Gets cost breakdown by provider.intGets the record count.Gets all recorded usage records.getRecords(Instant from, Instant to) Gets usage records within a time range.getRecordsByModel(String modelId) Gets usage records for a specific model.getRecordsByProvider(String provider) Gets usage records for a specific provider.Gets aggregated statistics.getStatistics(Instant from, Instant to) Gets statistics for a time range.Gets the total cost across all records.getTotalCost(Instant from, Instant to) Gets the total cost within a time range.voidrecord(UsageRecord record) Records a usage record.
-
Method Details
-
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
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
Gets usage records for a specific model.- Parameters:
modelId- The model identifier- Returns:
- List of usage records for the model
-
getRecordsByProvider
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
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
CostTracker.CostStatistics getStatistics()Gets aggregated statistics.- Returns:
- Cost statistics
-
getStatistics
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
-