{"record":{"id":"83ed14a957255580","repo":"JuliusBrussee/caveman","slug":"caveman-agent-run-maxcostusd-must-be-positive","errorCode":null,"errorMessage":"caveman agent: run maxCostUsd must be positive","messagePattern":"caveman agent: run maxCostUsd must be positive","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/agent/src/runtime.ts","lineNumber":774,"sourceCode":"  costUsd: number;\n  unpriced: boolean;\n  incomplete: boolean;\n  /**\n   * True once any descendant run reported `observe-only`. A graph whose traffic\n   * partly bypassed the gateway cannot be labelled plainly optimized, so the\n   * root under-claims instead of averaging.\n   */\n  observeOnly: boolean;\n};\n\nfunction rootExecutionContext(\n  definition: AgentDefinition,\n  maxCostUsd?: number,\n  options: Pick<RunOptions, \"budget\" | \"breakers\" | \"maxSubagentInvocations\" | \"maxConcurrentSubagents\"> = {},\n): InternalExecutionContext {\n  validateAgentGraph(definition);\n  if (maxCostUsd !== undefined && (!Number.isFinite(maxCostUsd) || maxCostUsd <= 0)) {\n    throw new Error(\"caveman agent: run maxCostUsd must be positive\");\n  }\n  const rootBudget: InvocationLedger[\"rootBudget\"] = maxCostUsd !== undefined\n    ? \"legacy_usd\"\n    : options.budget !== undefined && \"maxUsd\" in options.budget\n      ? \"usd\"\n      : options.budget !== undefined && \"maxTokens\" in options.budget ? \"tokens\" : \"absent\";\n  const invocationTrace = Object.freeze({\n    traceId: randomBytes(16).toString(\"hex\"),\n    spanId: randomBytes(8).toString(\"hex\"),\n    parentSpanId: \"\",\n  });\n  return Object.freeze({\n    rootDefinitionSha256: agentDefinitionSHA256(definition),\n    agentPath: Object.freeze([]),\n    // Root cap is one more ancestor ledger: root turns reserve against it, and\n    // every descendant stacks its own ledger on top of it.\n    spendLedgers: Object.freeze(maxCostUsd === undefined ? [] : [{\n      limitUsd: maxCostUsd,","sourceCodeStart":756,"sourceCodeEnd":792,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/agent/src/runtime.ts#L756-L792","documentation":"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.","triggerScenarios":"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).","commonSituations":"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).","solutions":["Omit maxCostUsd entirely for 'no limit' instead of passing 0.","Validate the value is finite and > 0 before calling run.","Fix the config/computation that produced 0/NaN/Infinity.","Use the newer structured options.budget API if migrating away from legacy maxCostUsd."],"exampleFix":"// before\nawait run(agent, { maxCostUsd: 0 });\n// after\nawait run(agent, { maxCostUsd: 1.5 }); // or omit for no limit","handlingStrategy":"validation","validationCode":"if (maxCostUsd !== undefined && (!Number.isFinite(maxCostUsd) || maxCostUsd <= 0)) {\n  throw new Error(\"maxCostUsd must be a positive finite number or undefined\");\n}","typeGuard":"const isValidMaxCost = (v: unknown): v is number =>\n  v === undefined || (typeof v === \"number\" && Number.isFinite(v) && v > 0);","tryCatchPattern":"try {\n  await runAgent(definition, input, maxCostUsd);\n} catch (e) {\n  if (e.message.includes(\"maxCostUsd must be positive\")) {\n    console.error(\"Check budget config: value must be > 0 and finite, or omitted\");\n  } else throw e;\n}","preventionTips":["Use undefined (not 0) to mean 'no cost limit'.","Sanitize values parsed from strings (parseFloat can yield NaN).","Clamp/config-validate budget values at config load time."],"tags":["budget","validation","invalid-value"],"backgroundTag":"invalid-argument-value","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}