JuliusBrussee/caveman · error

cave_budget_release_exceeds_max

cave_budget_release_exceeds_max

Error message

cave_budget_release_exceeds_max

What it means

`BudgetMeter.release` throws `cave_budget_release_exceeds_max` when the requested amount exceeds `releasable()` — `max - releasedAmount`, the budget that could still be released without breaching `max`. This is pre-flight validation at a developer-controlled checkpoint: the caller asked for a tranche the contract cannot grant, so the library refuses instead of allowing the release (releases can top a staged budget up to `max`, never past it — a release never causes a breach). Nothing is mutated when this throws.

Solutions

  1. Clamp the release to the remaining headroom: `meter.release(Math.min(amount, meter.releasable()), reason);`
  2. Check before releasing: `const head = meter.releasable(); if (amount <= head) meter.release(amount, reason);`
  3. Raise `maxUsd`/`maxTokens` in `RunOptions.budget` if the staged plan genuinely needs more total budget.
  4. Fix the controller's arithmetic so cumulative releases (initial tranche included) never exceed `max`.

Example fix

// before
meter.release(2.0, "checkpoint top-up"); // max=5, 4.5 already released

// after
meter.release(Math.min(2.0, meter.releasable()), "checkpoint top-up");
Defensive patterns

Strategy: validation

Validate before calling

const headroom = meter.releasable();
if (amount <= headroom) {
  meter.release(amount, reason);
} else {
  meter.release(headroom, reason); // or skip entirely
}

Try / catch

try {
  meter.release(amount, reason);
} catch (e) {
  if (e instanceof Error && e.message === "cave_budget_release_exceeds_max") {
    const headroom = meter.releasable();
    if (headroom > 0) meter.release(headroom, reason);
  } else throw e;
}

Prevention

When it happens

Trigger: Calling `meter.release(amount, reason)` where `amount > meter.releasable()`; summing staged tranches to more than the configured `maxUsd`/`maxTokens` (e.g. initial 2 + three 1.5 releases against max 5); an `onBudgetExhausted` handler returning `{ release }` larger than what remains under `max`; releasing the full original amount again after earlier tranches were already granted.

Common situations: Configured `budget.initialUsd` plus checkpoint top-ups exceeding `maxUsd`; off-by-one or unit mistakes (tokens vs USD, whole vs fractional amounts) in a controller; hard-coded release amounts that ignore how much was already released; concurrent controller code paths each releasing without checking the remaining headroom.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/e61b63e941b67b5f. Report an issue: GitHub.

Appendix: source

Thrown at packages/agent/src/budget.ts:338

   * pre-flight validation at a developer-controlled checkpoint, so the caller
   * asked for something the contract cannot grant.
   */
  release(amount: number, reason: string): BudgetTranche {
    if (!Number.isFinite(amount) || amount <= 0) {
      throw new Error("cave_budget_release_invalid");
    }
    if (this.denomination === "tokens" && !Number.isSafeInteger(amount)) {
      throw new Error("cave_budget_release_invalid");
    }
    if (typeof reason !== "string" || reason.trim() === "") {
      throw new Error("cave_budget_release_reason_required");
    }
    if (this.revokedFlag) throw new Error("cave_budget_revoked");
    // A breached ledger is dead. Releasing into it would record a tranche and
    // raise an escalation for money that can never be spent, and would read on
    // the receipt as a run that was still being funded after it went past cap.
    if (this.breachedFlag) throw new Error("cave_budget_cap_breached");
    if (amount > this.releasable()) throw new Error("cave_budget_release_exceeds_max");
    this.releasedAmount += amount;
    const tranche: BudgetTranche = Object.freeze({
      amount,
      reason,
      atCall: this.callIndex,
    });
    this.trancheLog.push(tranche);
    return tranche;
  }

  /**
   * Hold `amount` against the ledger. Returns `undefined` when it does not fit,
   * which is the caller's signal to clamp, compact, or stop — never to proceed.
   */
  reserve(amount: number, outputTokenCap: number): BudgetReservation | undefined {
    const held = this.hold(amount, outputTokenCap);
    if (held !== undefined) this.callIndex++;
    return held;

View on GitHub (pinned to 3ee70a1026)