Interface SpendBudgetOptions

A budget's settings.

interface SpendBudgetOptions {
    maxCostUSD: number;
    maxTotalTokens?: number;
    onLimitReached?: "warn" | "throw" | ((info) => void);
    guard?: CostGuard;
    budgetId?: string;
    unpriced?: "refuse" | "allow";
    parent?: SpendBudget;
}

Properties

maxCostUSD: number

The most the run may spend, in US dollars.

maxTotalTokens?: number

The most prompt and completion tokens the run may use, counted by this budget: zero or more.

onLimitReached?: "warn" | "throw" | ((info) => void)

What a call that would pass the budget does: 'throw' (the default) refuses it with CostCapExceededError; 'warn' logs once per call and lets it run; a function is told what was about to be refused and the call is then refused as with 'throw'. A function that throws refuses the call with its own error, and the budgets that draw on this one hear of the refusal as of any other.

Type declaration

guard?: CostGuard

The guard that holds the spending; a budget given none makes its own, with no daily or single-call cap. On a guard given, the budget sets the session cap of its id to maxCostUSD and keeps the daily cap the guard holds for that id.

budgetId?: string

The id the guard keeps the spending under; a budget given none makes one.

unpriced?: "refuse" | "allow"

A call on a model with no price row: 'refuse' (the default) refuses it with UnpricedModelError, since its cost could not be counted; 'allow' lets it run and counts its cost as nothing, while the token budget and a budget already past its cap still refuse it.

parent?: SpendBudget

A budget this one draws on. Every check asks the parent first, with the same estimate, and every record and outside cost is added to the parent after this budget. A refusal by the parent, or by any budget above it, runs this budget's own onLimitReached function (when it is one) with the refusing budget's SpendLimitInfo, after the refusing budget's own, before the error goes on. A parent chain that holds a budget on the same guard and budgetId as this one is refused: the two would keep one total and charge every call to it twice.