Packages@robojs/ai
tokenLedger
Variable: tokenLedger
const tokenLedger: {
assertWithinLimit: (model, usageKind?) => Promise<void>;
configure: (configuration) => void;
getEntriesForDay: (dayKey) => Promise<TokenUsageEntry[]>;
getLifetimeTotals: (model?) => Promise<Record<string, TokenWindowTotals>>;
getLimitRule: (model) => TokenLimitRule | undefined;
getLimits: () => TokenLimitConfig;
getLimitState: (model) => Promise<TokenLimitState>;
getSummary: (query) => Promise<TokenSummaryResult>;
off: <T>(event, listener) => void;
on: <T>(event, listener) => void;
once: <T>(event, listener) => void;
recordUsage: (options) => Promise<TokenRecordResult | null>;
setLimits: (limits?) => void;
willExceedLimit: (model, tokens, window?) => Promise<boolean>;
};High-level facade exposing token accounting utilities, aggregation helpers, and limit enforcement hooks for the AI plugin.
Type declaration
| Name | Type | Default value |
|---|---|---|
assertWithinLimit | (model, usageKind?) => Promise<void> | - |
configure | (configuration) => void | configureTokenLedger |
getEntriesForDay | (dayKey) => Promise<TokenUsageEntry[]> | - |
getLifetimeTotals | (model?) => Promise<Record<string, TokenWindowTotals>> | - |
getLimitRule | (model) => TokenLimitRule | undefined | - |
getLimits | () => TokenLimitConfig | - |
getLimitState | (model) => Promise<TokenLimitState> | - |
getSummary | (query) => Promise<TokenSummaryResult> | - |
off | <T>(event, listener) => void | - |
on | <T>(event, listener) => void | - |
once | <T>(event, listener) => void | - |
recordUsage | (options) => Promise<TokenRecordResult | null> | - |
setLimits | (limits?) => void | - |
willExceedLimit | (model, tokens, window?) => Promise<boolean> | - |
Examples
const result = await tokenLedger.recordUsage({ model: 'gpt-4o', tokensIn: 500, tokensOut: 300 })
console.log(result.totals.windows.day?.totals.total)const summary = await tokenLedger.getSummary({ model: 'gpt-4o', window: 'day' })tokenLedger.configure({
limits: { perModel: { 'gpt-4o': { window: 'day', maxTokens: 50_000, mode: 'warn' } } },
hooks: {
onLimitReached: ({ breaches }) => console.warn(breaches)
}
})Remarks
Aggregates are persisted via Flashcore and cached in-memory for fast read access.
See
- TokenLimitError
- TokenLimitConfig
- TokenSummaryQuery
