Model and tool budgets
Set a token or estimated cost limit, understand reservations, and reproduce a tool call blocked by the runtime.
Reserve before dispatch
Vaern reserves budget before a model call or a priced tool effect starts. A reservation that exceeds the amount remaining raises BudgetError with the code RESERVE_EXCEEDS_REMAINING before dispatch.
A model call reserves an estimate for the input and its maximum output. Successful responses settle against reported usage. A priced tool reserves and charges its declared USD cost.
vaern run --budget-usd 0.05 --budget-tokens 4000 run.ts- The CLI caps apply from the beginning of the run. A script can lower a cap with budget() and inspect what remains with budget.remaining().
- Dollar amounts depend on the prices declared by the script. Set a token cap for model calls when those prices are unknown.
- Raw fetch() calls are recorded as network effects. Budget metering applies to model(), priced tools, and the documented MCP connection and call costs.
Block a second tool call
Save this example as run.ts. It gives a local counting tool a three-cent declared cost and starts with a five-cent budget. The first call returns two ready entries and leaves two cents. The second call is rejected before the tool runs again.
import { budget, BudgetError, tool } from "vaern:ai";
budget({ usd: 0.05 });
const count = tool({
name: "count_ready",
cost: { usd: 0.03 },
run: () => ["new", "ready", "ready", "queued"]
.filter((entry) => entry === "ready").length,
});
console.log("Ready entries:", await count.call({}));
try {
await count.call({});
} catch (error) {
if (!(error instanceof BudgetError)) throw error;
console.log("Second call blocked:", error.code);
}vaern run run.ts
vaern journal verify latest
vaern replay latestReady entries: 2
Second call blocked: RESERVE_EXCEEDS_REMAININGTrack a budget across runs
Set a name on the first call to budget() to reuse a ledger across runs. A named budget is stored under the runtime's journal home. Vaern verifies the ledger on open and holds one active writer per named budget.
import { budget } from "vaern:ai";
budget({ name: "nightly", usd: 1 });
console.log(budget.remaining());