JuliusBrussee/caveman · error · Error

caveman agent: run maxCostUsd must be positive

Error message

caveman agent: run maxCostUsd must be positive

What it means

rootExecutionContext validates the legacy maxCostUsd run parameter: if provided it must be a finite number greater than zero. Zero, negative, NaN, or Infinity values are rejected before the run starts.

Solutions

  1. Omit maxCostUsd entirely for 'no limit' instead of passing 0.
  2. Validate the value is finite and > 0 before calling run.
  3. Fix the config/computation that produced 0/NaN/Infinity.
  4. Use the newer structured options.budget API if migrating away from legacy maxCostUsd.

Example fix

// before
await run(agent, { maxCostUsd: 0 });
// after
await run(agent, { maxCostUsd: 1.5 }); // or omit for no limit
Defensive patterns

Strategy: validation

Validate before calling

if (maxCostUsd !== undefined && (!Number.isFinite(maxCostUsd) || maxCostUsd <= 0)) {
  throw new Error("maxCostUsd must be a positive finite number or undefined");
}

Type guard

const isValidMaxCost = (v: unknown): v is number =>
  v === undefined || (typeof v === "number" && Number.isFinite(v) && v > 0);

Try / catch

try {
  await runAgent(definition, input, maxCostUsd);
} catch (e) {
  if (e.message.includes("maxCostUsd must be positive")) {
    console.error("Check budget config: value must be > 0 and finite, or omitted");
  } else throw e;
}

Prevention

When it happens

Trigger: Calling runAgent/streamAgent with maxCostUsd = 0, a negative value, NaN, or Infinity (e.g. from a computed config where a budget was zeroed out or parsed from a bad string).

Common situations: Config defaulting to 0 meaning 'no limit' instead of undefined; parseFloat on an empty/invalid string producing NaN; template math producing Infinity (division by zero).

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/83ed14a957255580. Report an issue: GitHub.

Appendix: source

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

  costUsd: number;
  unpriced: boolean;
  incomplete: boolean;
  /**
   * True once any descendant run reported `observe-only`. A graph whose traffic
   * partly bypassed the gateway cannot be labelled plainly optimized, so the
   * root under-claims instead of averaging.
   */
  observeOnly: boolean;
};

function rootExecutionContext(
  definition: AgentDefinition,
  maxCostUsd?: number,
  options: Pick<RunOptions, "budget" | "breakers" | "maxSubagentInvocations" | "maxConcurrentSubagents"> = {},
): InternalExecutionContext {
  validateAgentGraph(definition);
  if (maxCostUsd !== undefined && (!Number.isFinite(maxCostUsd) || maxCostUsd <= 0)) {
    throw new Error("caveman agent: run maxCostUsd must be positive");
  }
  const rootBudget: InvocationLedger["rootBudget"] = maxCostUsd !== undefined
    ? "legacy_usd"
    : options.budget !== undefined && "maxUsd" in options.budget
      ? "usd"
      : options.budget !== undefined && "maxTokens" in options.budget ? "tokens" : "absent";
  const invocationTrace = Object.freeze({
    traceId: randomBytes(16).toString("hex"),
    spanId: randomBytes(8).toString("hex"),
    parentSpanId: "",
  });
  return Object.freeze({
    rootDefinitionSha256: agentDefinitionSHA256(definition),
    agentPath: Object.freeze([]),
    // Root cap is one more ancestor ledger: root turns reserve against it, and
    // every descendant stacks its own ledger on top of it.
    spendLedgers: Object.freeze(maxCostUsd === undefined ? [] : [{
      limitUsd: maxCostUsd,

View on GitHub (pinned to 3ee70a1026)