JuliusBrussee/caveman · error

cave_subagent_invocation_limit

cave_subagent_invocation_limit

Error message

cave_subagent_invocation_limit

What it means

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.

Solutions

  1. Raise maxInvocations in the invocation/ledger config to fit the workflow.
  2. Refactor the agent to batch work into fewer subagent calls.
  3. Catch the error and degrade gracefully (do the work in-line instead of spawning).
  4. Remove the maxInvocations cap (leave undefined) if volume should be unbounded.

Example fix

// before
{ subagents: { maxInvocations: 3 } }
// after
{ subagents: { maxInvocations: 20 } }
Defensive patterns

Strategy: try-catch

Validate before calling

// before spawning a subagent
const ledger = invocationState.ledger;
if (ledger.maxInvocations !== undefined && ledger.admitted >= ledger.maxInvocations) {
  // run in-line instead of spawning
  return runInline(task);
}

Try / catch

try {
  return await spawnSubagent(task);
} catch (e) {
  if (e?.message === 'cave_subagent_invocation_limit') {
    return await runInline(task);
  }
  throw e;
}

Prevention

When it happens

Trigger: Spawning a subagent when ledger.maxInvocations !== undefined and ledger.admitted >= ledger.maxInvocations at runtime.ts:3830.

Common situations: 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.

Related errors


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

Appendix: source

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

  appliedPlan: AppliedPlan,
): { description: string; input: TSchema } {
  const segment = lowered.ir.segments.find((item) => item.id === `tool.${definition.name}`);
  if (!segment) throw new Error(`cave_context_segment_missing:tool.${definition.name}`);
  const body = appliedPlan.bodies.get(segment.bodyHandle);
  if (!body) throw new Error(`cave_context_body_missing:tool.${definition.name}`);
  const parsed = JSON.parse(new TextDecoder().decode(body)) as unknown;
  if (!isRecord(parsed) || parsed.name !== definition.name ||
      typeof parsed.description !== "string" || !isRecord(parsed.input)) {
    throw new Error(`cave_tool_schema_invalid:${definition.name}`);
  }
  return { description: parsed.description, input: parsed.input as TSchema };
}

function admitSubagent(state: InvocationState): () => void {
  const ledger = state.ledger;
  if (ledger.maxInvocations !== undefined && ledger.admitted >= ledger.maxInvocations) {
    ledger.invocationRejections++;
    throw new Error("cave_subagent_invocation_limit");
  }
  if (ledger.maxConcurrent !== undefined && ledger.active >= ledger.maxConcurrent) {
    ledger.concurrencyRejections++;
    throw new Error("cave_subagent_concurrency_limit");
  }
  ledger.admitted++;
  ledger.active++;
  ledger.peakActive = Math.max(ledger.peakActive, ledger.active);
  let released = false;
  return () => {
    if (released) return;
    released = true;
    ledger.active = Math.max(0, ledger.active - 1);
  };
}

function childInvocationTrace(parent: InvocationTrace): InvocationTrace {
  return Object.freeze({

View on GitHub (pinned to 3ee70a1026)