{"record":{"id":"b980168c88686aaa","repo":"JuliusBrussee/caveman","slug":"cave-subagent-invocation-limit","errorCode":"cave_subagent_invocation_limit","errorMessage":"cave_subagent_invocation_limit","messagePattern":"cave_subagent_invocation_limit","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/agent/src/runtime.ts","lineNumber":3830,"sourceCode":"  appliedPlan: AppliedPlan,\n): { description: string; input: TSchema } {\n  const segment = lowered.ir.segments.find((item) => item.id === `tool.${definition.name}`);\n  if (!segment) throw new Error(`cave_context_segment_missing:tool.${definition.name}`);\n  const body = appliedPlan.bodies.get(segment.bodyHandle);\n  if (!body) throw new Error(`cave_context_body_missing:tool.${definition.name}`);\n  const parsed = JSON.parse(new TextDecoder().decode(body)) as unknown;\n  if (!isRecord(parsed) || parsed.name !== definition.name ||\n      typeof parsed.description !== \"string\" || !isRecord(parsed.input)) {\n    throw new Error(`cave_tool_schema_invalid:${definition.name}`);\n  }\n  return { description: parsed.description, input: parsed.input as TSchema };\n}\n\nfunction admitSubagent(state: InvocationState): () => void {\n  const ledger = state.ledger;\n  if (ledger.maxInvocations !== undefined && ledger.admitted >= ledger.maxInvocations) {\n    ledger.invocationRejections++;\n    throw new Error(\"cave_subagent_invocation_limit\");\n  }\n  if (ledger.maxConcurrent !== undefined && ledger.active >= ledger.maxConcurrent) {\n    ledger.concurrencyRejections++;\n    throw new Error(\"cave_subagent_concurrency_limit\");\n  }\n  ledger.admitted++;\n  ledger.active++;\n  ledger.peakActive = Math.max(ledger.peakActive, ledger.active);\n  let released = false;\n  return () => {\n    if (released) return;\n    released = true;\n    ledger.active = Math.max(0, ledger.active - 1);\n  };\n}\n\nfunction childInvocationTrace(parent: InvocationTrace): InvocationTrace {\n  return Object.freeze({","sourceCodeStart":3812,"sourceCodeEnd":3848,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/agent/src/runtime.ts#L3812-L3848","documentation":"admitSubagent enforces the invocation ledger's maxInvocations cap: if the number of already-admitted subagent invocations has reached the limit, it throws this error and increments invocationRejections. The library enforces it to bound total subagent spawn volume per invocation.","triggerScenarios":"Spawning a subagent when ledger.maxInvocations !== undefined and ledger.admitted >= ledger.maxInvocations at runtime.ts:3830.","commonSituations":"A low maxInvocations config combined with a fan-out workflow spawning many subagents; recursive/looping agent designs that keep requesting subagents; cost-control configs set too tightly.","solutions":["Raise maxInvocations in the invocation/ledger config to fit the workflow.","Refactor the agent to batch work into fewer subagent calls.","Catch the error and degrade gracefully (do the work in-line instead of spawning).","Remove the maxInvocations cap (leave undefined) if volume should be unbounded."],"exampleFix":"// before\n{ subagents: { maxInvocations: 3 } }\n// after\n{ subagents: { maxInvocations: 20 } }","handlingStrategy":"try-catch","validationCode":"// before spawning a subagent\nconst ledger = invocationState.ledger;\nif (ledger.maxInvocations !== undefined && ledger.admitted >= ledger.maxInvocations) {\n  // run in-line instead of spawning\n  return runInline(task);\n}","typeGuard":null,"tryCatchPattern":"try {\n  return await spawnSubagent(task);\n} catch (e) {\n  if (e?.message === 'cave_subagent_invocation_limit') {\n    return await runInline(task);\n  }\n  throw e;\n}","preventionTips":["Size maxInvocations to the fan-out your workflow actually needs.","Track admitted counts in your orchestrator before requesting subagents.","Design agents to batch work instead of spawning per-item.","Leave headroom in cost-control configs to avoid mid-run rejections."],"tags":["limit-exceeded","subagents","invocation-limit"],"backgroundTag":"rate-limit-exceeded","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"}