Available in
Marketplaces, most starred first
Tags
Add to README
Shows its components, always-on context cost and install status, kept current. Add ?tokens=0 to hide the cost.
Marketplaces, most starred first
Shows its components, always-on context cost and install status, kept current. Add ?tokens=0 to hide the cost.
Marketplaces, most starred first
Shows its components, always-on context cost and install status, kept current. Add ?tokens=0 to hide the cost.
By karanb192v1.0.0MITIn 1 marketplace
Caps the total number of subagents spawned in a session, including nested ones. Prompts for approval from spawn 20 and every 10 after, and denies at spawn 60. Unattended runs stop at the ask threshold unless SPAWN_CAP_ASK is set equal to SPAWN_CAP_DENY.
Trending plugins
7-day copy clicks
Run automatically on Claude Code events. Claude doesn't decide.
No risk signals detected
Nothing flagged in this plugin's hooks, MCP servers or commands. Other components are not analyzed.
Static checks of hooks, mods, MCP servers and commands. Not a security audit.
1 marketplace, most starred first
subagent-budget, spawn-limit, cost-control, guardrail, pretooluse-hook, runaway-agents, claude-code, hooks ·
Shows its components, always-on context cost and install status, kept current. Add ?tokens=0 to hide the cost.
A total-spawns-per-session budget for subagents: asks from spawn 20, again every 10, denies at 60, counted across nested fan-outs. The cap Claude Code shipped in 2.1.212 and removed in 2.1.224, back as a hook you control.
A PreToolUse hook on the Agent tool (older name Task). Every spawn in a session appends one line to a per-session ledger; the line count is the budget spent. Spawn SPAWN_CAP_ASK (default 20) prompts you, then every SPAWN_CAP_ASK_STEP spawns (default 10) prompts again, and spawn SPAWN_CAP_DENY (default 60) is denied. Hooks also fire inside subagents (agent_id / agent_type) with the parent's session_id (observed in local subagent transcripts; the docs do not state it), so nested fan-outs share the budget.
Why: Claude Code had this cap natively from 2.1.212 ("Added a per-session cap on subagent spawns (default 200, override with CLAUDE_CODE_MAX_SUBAGENTS_PER_SESSION)") to 2.1.224 ("Removed the 200-subagent-per-session spawn cap; long-running sessions no longer refuse new agents (concurrency and depth limits still apply)"), per the CHANGELOG. The remaining native caps bound the rate (20 concurrent) and depth, not the total, which is the shape of #92349 (325 agents for a settings audit), #91942 (160 subagents, session limit exhausted), #69206 (218 spawned, about 10 intended) and #69578 (recursive loop, about 800k tokens).
/plugin marketplace add karanb192/claude-code-hooks # once per machine
/plugin install subagent-spawn-cap@claude-code-hooks
Restart Claude Code, done. Already running guard-pack? It includes this guard; do not install both.
| Event | Matcher | What happens |
|---|---|---|
| PreToolUse | Agent|Task | Counts the session's ledger lines plus this call. At SPAWN_CAP_ASK and every SPAWN_CAP_ASK_STEP after it: appends, returns "ask". At or above SPAWN_CAP_DENY: returns "deny", no append. Otherwise: appends, returns {}. Other tools: {} with no filesystem work. |
Per the hooks reference an ask reason is shown to you and a deny reason to Claude, so the two texts have different readers. What you see in the permission prompt at spawn 20:
⚠️ [spawn-cap] Subagent spawn #20 in this session (ask threshold SPAWN_CAP_ASK=20, next check at #30, hard cap SPAWN_CAP_DENY=60). Approve to continue; the next 9 spawns then pass without asking. Deny to stop the fan-out. Reset this session's count by deleting ~/.claude/subagent-spawn-cap/<session_id>.jsonl; change the cadence via SPAWN_CAP_ASK / SPAWN_CAP_ASK_STEP in the settings.json env block (restart needed).
What Claude sees at spawn 60:
🚨 [spawn-cap] Subagent spawn #60 in this session hit the hard cap (SPAWN_CAP_DENY=60). Do not retry: finish with the results you already have and tell the user the session's spawn budget is spent. The user can reset it by deleting ~/.claude/subagent-spawn-cap/<session_id>.jsonl, or raise SPAWN_CAP_DENY in the settings.json env block and restart.
Hook processes inherit the environment Claude Code was launched with: set these in the env block of settings.json and restart. Invalid values fall back to the default.
| Variable | Default | Purpose |
|---|---|---|
SPAWN_CAP_ASK | 20 | First spawn number that prompts. Positive integer. |
SPAWN_CAP_ASK_STEP | 10 | Prompt again every this many spawns after SPAWN_CAP_ASK; 1 prompts on every spawn. Positive integer. |
SPAWN_CAP_DENY | 60 | Spawn number denied outright. Clamped up to SPAWN_CAP_ASK if set lower; equal to it skips the ask stage. |
SPAWN_CAP_ALLOW | unset | The literal string true lets spawns through past both thresholds while set; each is still recorded and logged ALLOW_OVERRIDE. |
HOOK_SAFETY_LEVEL is not read: the repo's levels pick pattern sets, a budget is a number, and a hidden level-to-number table would move your budget whenever you tune the level for another guard.
Unattended runs. In claude -p with no permission host and in dontAsk mode, anything that would prompt is denied (headless reference), so the ask threshold is the effective hard stop: with the defaults an unattended session ends at spawn 20, not 60. Set SPAWN_CAP_ASK equal to SPAWN_CAP_DENY for CI so Claude gets the deny text it can act on.
Built from public signals: copy clicks on this site, GitHub stars and activity, the README and the manifest. Not a security audit or an endorsement.
npx claudepluginhub karanb192/claude-code-hooks --plugin subagent-spawn-capPlugins from other owners that cover similar ground