JuliusBrussee/caveman · error · Error

cave_budget_escalation_result_invalid

cave_budget_escalation_result_invalid

Error message

cave_budget_escalation_result_invalid

What it means

When the budget ladder decides to stop for budget_exhausted, the library calls options.onBudgetExhausted and expects either "stop" or an object { release: number, reason: string }. Any other return shape is rejected with this error, since an invalid escalation result cannot be applied to the budget meter.

Solutions

  1. Return exactly "stop" or { release: <number>, reason: <string> } from onBudgetExhausted
  2. Await the callback and return its value instead of letting it fall through to undefined
  3. Add a return-type annotation/union so TypeScript enforces the allowed outcomes

Example fix

// before
onBudgetExhausted: async (ctx) => { log(ctx); }
// after
onBudgetExhausted: async (ctx) => ({ release: 2.0, reason: "approved top-up" })
Defensive patterns

Strategy: type-guard

Type guard

function isEscalationOutcome(v: unknown): v is "stop" | { release: number; reason: string } {
  return v === "stop" || (typeof v === "object" && v !== null && typeof (v as any).release === "number" && typeof (v as any).reason === "string");
}

Try / catch

try { await streamAgent(opts); } catch (e) { if (e.message === "cave_budget_escalation_result_invalid") { /* fix onBudgetExhausted return value */ } else throw e; }

Prevention

When it happens

Trigger: Providing an onBudgetExhausted callback that returns something other than "stop" or a well-formed release object — e.g. returning undefined, true, or { release: "5" } with a non-number release.

Common situations: Async callback whose return value is wrapped in a Promise resolved to undefined, a callback refactored to log without returning, or returning a release amount as a string from config.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at packages/agent/src/runtime.ts:1632

        restorableBytes: restorableRequestBytes(
          conversationOriginals,
          instructions,
          originalInstructions,
        ),
      });
      let decided: NextCallDecision;
      try {
        decided = plan();
        // Escalation gets exactly one attempt per exhaustion: the handler either
        // funds the next call or it does not, and a handler that keeps releasing
        // slivers must not turn one stop into an unbounded loop.
        if (decided.action === "stop" && decided.reason === "budget_exhausted" &&
            budgetMeter !== undefined && typeof options.onBudgetExhausted === "function") {
          const outcome = await options.onBudgetExhausted(budgetExhaustionContext(budgetMeter));
          if (outcome !== "stop") {
            if (!isRecord(outcome) || typeof outcome.release !== "number" ||
                typeof outcome.reason !== "string") {
              throw new Error("cave_budget_escalation_result_invalid");
            }
            budgetMeter.release(outcome.release, outcome.reason);
            decided = plan();
          }
        }
      } catch (error) {
        // Pi answers a thrown streamFn with a synthesized terminal error turn,
        // which would otherwise replace this cause with a generic provider
        // failure. Record it first so the run reports what actually went wrong.
        ladderFailure ??= error instanceof Error ? error : new Error(String(error));
        throw ladderFailure;
      }
      if (decided.action === "stop") {
        // Pi answers a thrown streamFn with a synthesized zero-usage error turn,
        // so record the refusal before throwing or the stop becomes the usage
        // failure that turn produces.
        stopReason = decided.reason;
        refusalPending = true;

View on GitHub (pinned to 3ee70a1026)