Package com.tnsai.llm.cost
Class BudgetManager
java.lang.Object
com.tnsai.llm.cost.BudgetManager
Manages budget limits and alerts for LLM usage.
BudgetManager provides configurable spending limits with automatic enforcement and alerting capabilities.
Features
- Set overall budget limits
- Specure alert thresholds (e.g., 80%, 90%)
- Time-based budget periods (daily, monthly)
- Callback notifications for alerts and limit breaches
- Hard/soft limit modes
Usage Example
BudgetManager budget = BudgetManager.builder()
.limit(new BigDecimal("100.00")) // $100 budget
.period(Duration.ofDays(30)) // Monthly
.alertThreshold(0.80) // Alert at 80%
.onAlert(alert -> log.warn(alert))
.onLimitExceeded(cost -> stopRequests())
.build();
// Check before making request
if (budget.canSpend(estimatedCost)) {
// Make API call
budget.recordSpend(actualCost);
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordstatic enumstatic enumstatic final recordstatic class -
Method Summary
Modifier and TypeMethodDescriptionstatic BudgetManager.Builderbuilder()booleancanSpend(BigDecimal amount) Checks if a spend amount is within budget.Gets the current spend in this period.getLimit()Gets the budget limit.Gets the budget period.Gets the period start time.Gets the remaining budget.Gets time remaining in current period.Gets current budget status.doubleGets the usage percentage (0.0 to 1.0+).booleanChecks if in hard limit mode.booleanrecordSpend(BigDecimal amount) Records a spend and triggers alerts if thresholds crossed.voidreset()Manually resets the budget period.booleantrySpend(BigDecimal amount) Atomically checks budget and records spend in one operation.
-
Method Details
-
builder
-
canSpend
Checks if a spend amount is within budget.- Parameters:
amount- Amount to spend- Returns:
- true if spend is allowed
-
trySpend
Atomically checks budget and records spend in one operation. Prevents TOCTOU race between separate canSpend/recordSpend calls.- Parameters:
amount- Amount to spend- Returns:
- true if spend was within budget and recorded, false if it would exceed the limit
-
recordSpend
Records a spend and triggers alerts if thresholds crossed.- Parameters:
amount- Amount spent- Returns:
- true if within budget, false if limit exceeded
-
getCurrentSpend
Gets the current spend in this period. -
getRemainingBudget
Gets the remaining budget.- Returns:
- Remaining budget, or null if no limit set
-
getUsagePercent
public double getUsagePercent()Gets the usage percentage (0.0 to 1.0+).- Returns:
- Usage as a fraction of the limit
-
getLimit
Gets the budget limit. -
getPeriod
Gets the budget period. -
getPeriodStart
Gets the period start time. -
getRemainingTime
Gets time remaining in current period. -
isHardLimit
public boolean isHardLimit()Checks if in hard limit mode. -
reset
public void reset()Manually resets the budget period. -
getStatus
Gets current budget status.
-