TokenBudget class
Controls token spending with configurable budgets.
Prevents runaway API costs by enforcing limits per request, per session, and per day.
Constructors
- TokenBudget({int? maxTokensPerRequest, int? maxTokensPerSession, int? maxTokensPerDay})
Properties
- dailyRemaining → int?
-
Remaining tokens in the daily budget (null if unlimited).
no setter
- dailyTokensUsed → int
-
Tokens used today.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- maxTokensPerDay → int?
-
Maximum tokens per day.
final
- maxTokensPerRequest → int?
-
Maximum tokens per single request.
final
- maxTokensPerSession → int?
-
Maximum tokens per session (resets when resetSession is called).
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- sessionRemaining → int?
-
Remaining tokens in the session budget (null if unlimited).
no setter
- sessionTokensUsed → int
-
Tokens used in the current session.
no setter
Methods
-
canProceed(
int estimatedTokens) → bool -
Checks whether a request with
estimatedTokenscan proceed. -
enforce(
int estimatedTokens) → void - Enforces the budget — throws AIBudgetExceededError if over budget.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
recordUsage(
AIUsage usage) → void - Records token usage from a completed response.
-
resetAll(
) → void - Resets all budgets.
-
resetSession(
) → void - Resets the session budget.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited