Skip to content

Budget guard (#66)

Opt-in, off when runtime.budget is absent. runtime.budget = { maxCalls?, maxCallsPerTool?: {tool: int}, maxCost?, costFn?(tool, input) -> number }. Limits must be positive integers (maxCost: positive finite number), otherwise create() throws. costFn is host code and never comes from JSON.

Pipeline order: resolve -> enabled -> approval -> budget.reserve -> dispatch -> budget.commit -> envelope.

  • Reserve is synchronous before dispatch, so concurrent calls cannot overspend.
  • Exceeding any limit returns BUDGET_EXCEEDED (recoverable: false) with meta.callsUsed / meta.callsRemaining (the latter only when maxCalls is set).
  • A dispatched call counts even if the tool fails. TOOL_DISABLED, unknown tools and APPROVAL_* outcomes never reach reserve.
  • A costFn that throws or returns a negative/non-finite number fails closed with BUDGET_EXCEEDED.
  • State lives in the PolicyStateStore keyed by policyScope (never the spoofable input sessionId), under budget:calls, budget:cost, budget:tool:<name>. kv.* and state.checkpoint cannot reset it.
  • Retries and cache hits do not consume extra budget (see retry and idempotency).

Usage: WarTools.budgetUsage() (Node), budget_usage() (Python), BudgetUsage() (.NET) return {callsUsed, callsRemaining?, costUsed}. Successful and denied envelopes carry meta.callsUsed / meta.callsRemaining when a budget is configured.