Package com.tnsai.llm.cost
Class InMemoryCostTracker
java.lang.Object
com.tnsai.llm.cost.InMemoryCostTracker
- All Implemented Interfaces:
CostTracker
In-memory implementation of CostTracker.
Stores usage records in memory with thread-safe operations. Suitable for development, testing, and single-instance deployments.
Features
- Thread-safe record storage
- Efficient time-range queries
- Automatic statistics calculation
- Optional max records limit
Usage Example
CostTracker tracker = InMemoryCostTracker.builder()
.maxRecords(10000)
.build();
tracker.record(UsageRecord.builder()
.modelId("gpt-4o")
.inputTokens(1000)
.outputTokens(500)
.build());
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface com.tnsai.llm.cost.CostTracker
CostTracker.CostStatistics -
Method Summary
Modifier and TypeMethodDescriptionstatic InMemoryCostTracker.Builderbuilder()voidclear()Clears all recorded data.static InMemoryCostTrackercreate()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
-
builder
-
create
-
record
Description copied from interface:CostTrackerRecords a usage record.- Specified by:
recordin interfaceCostTracker- Parameters:
record- The usage record to track
-
getRecords
Description copied from interface:CostTrackerGets all recorded usage records.- Specified by:
getRecordsin interfaceCostTracker- Returns:
- List of all usage records
-
getRecords
Description copied from interface:CostTrackerGets usage records within a time range.- Specified by:
getRecordsin interfaceCostTracker- Parameters:
from- Start time (inclusive)to- End time (exclusive)- Returns:
- List of usage records in the range
-
getRecordsByModel
Description copied from interface:CostTrackerGets usage records for a specific model.- Specified by:
getRecordsByModelin interfaceCostTracker- Parameters:
modelId- The model identifier- Returns:
- List of usage records for the model
-
getRecordsByProvider
Description copied from interface:CostTrackerGets usage records for a specific provider.- Specified by:
getRecordsByProviderin interfaceCostTracker- Parameters:
provider- The provider name- Returns:
- List of usage records for the provider
-
getTotalCost
Description copied from interface:CostTrackerGets the total cost across all records.- Specified by:
getTotalCostin interfaceCostTracker- Returns:
- Total cost in USD
-
getTotalCost
Description copied from interface:CostTrackerGets the total cost within a time range.- Specified by:
getTotalCostin interfaceCostTracker- Parameters:
from- Start time (inclusive)to- End time (exclusive)- Returns:
- Total cost in USD for the range
-
getStatistics
Description copied from interface:CostTrackerGets aggregated statistics.- Specified by:
getStatisticsin interfaceCostTracker- Returns:
- Cost statistics
-
getStatistics
Description copied from interface:CostTrackerGets statistics for a time range.- Specified by:
getStatisticsin interfaceCostTracker- Parameters:
from- Start time (inclusive)to- End time (exclusive)- Returns:
- Cost statistics for the range
-
getCostByModel
Description copied from interface:CostTrackerGets cost breakdown by model.- Specified by:
getCostByModelin interfaceCostTracker- Returns:
- Map of model ID to total cost
-
getCostByProvider
Description copied from interface:CostTrackerGets cost breakdown by provider.- Specified by:
getCostByProviderin interfaceCostTracker- Returns:
- Map of provider name to total cost
-
clear
public void clear()Description copied from interface:CostTrackerClears all recorded data.- Specified by:
clearin interfaceCostTracker
-
getRecordCount
public int getRecordCount()Description copied from interface:CostTrackerGets the record count.- Specified by:
getRecordCountin interfaceCostTracker- Returns:
- Number of recorded usage records
-