A Claude Code fan-out fails with `Concurrent subagent limit reached` — what limits subagent concurrency and nesting, and how do you change them?
answer
- two separate caps, two variables
- running count, not session total
- twenty at once by default
- three layers below main
- adjustable, never removable
basics
~10 sClaude Code refuses a new Agent spawn while 20 subagents are running (CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS) and lets subagents nest three layers below the main conversation (CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH). Both take a positive whole number; neither can be removed.
solid answer
~40 sThere are two independent limits, both from v2.1.217. The **concurrency** limit counts subagents running right now: at 20, an `Agent` spawn fails with `Concurrent subagent limit reached` and the error tells Claude not to retry; spawning works again once the count drops. There is no cap on the total spawned over a session. `CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS` adjusts it. The **depth** limit defaults to three layers below the main conversation since v2.1.219; at the limit Claude Code withholds the `Agent` tool, so that subagent does the work itself. `CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH` changes it, and `1` turns nesting off. Both variables accept only a positive whole number, so they adjust the limits but cannot disable them.
code
json · 6 lines{
"env": {
"CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS": "8",
"CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH": "2"
}
}go deeper
Recall that Claude Code limits how many subagents run at once and how deeply they can nest, and that environment variables adjust both.
State the defaults (20 running, three layers), what happens at each limit, and that the variables adjust but never remove them.
Diagnose a refused fan-out: which runs hold slots, why resumes and /subtask forks are not blocked, and when batching work beats raising the cap.
Set team limits that balance parallel throughput against usage bursts and the context cost of many reports, and document why the defaults changed across releases.
## Two limits, two jobs Claude Code keeps subagent use bounded with two separate limits, each with its own environment variable. Both require v2.1.217 or later. | Limit | Default | Variable | What it stops | | --- | --- | --- | --- | | Concurrency | 20 running subagents per session | `CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS` | Spawning another while too many run | | Depth | 3 layers below the main conversation | `CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH` | Subagents nesting deeper | Neither limit caps the **total** number of subagents over a session. Both variables accept a positive whole number in plain digits; anything else is ignored, so you can raise or lower a limit but not remove it. ## The concurrency limit When 20 subagents are already running and Claude spawns another with the `Agent` tool, the call fails with `Concurrent subagent limit reached`, and the error tells Claude **not to retry**. It is not a queue: the request is refused, and spawning succeeds again once the running count drops below the limit. Sessions with ultracode active are exempt. Some runs occupy slots without being stopped by the limit: - A fork you start with `/subtask` takes a slot while it runs and is **never blocked**. - **Resuming** a finished subagent takes a fresh slot without checking the limit, so resumes can push the running count past it. - Workflow agents and agent-team teammates follow **their own limits** instead. A fan-out that trips the limit usually means one message asked for far more parallel work than the session can absorb. Raising the variable is one answer; batching the work into fewer, broader subagents is often a better one, because every report also lands in the parent's context. ## The depth limit By default a subagent can spawn subagents of its own, **up to three layers** below the main conversation. At the limit, Claude Code withholds the `Agent` tool from every subagent except a fork, so a subagent at the bottom layer does its delegated work itself and returns one summary. A fork at the limit keeps `Agent` listed, but the tool returns an error instead of spawning. Nesting suits a delegated task that itself splits, such as a bug-hunt subagent that dispatches one verifier per suspect module. In an interactive session, a subagent that launches background subagents **waits** for their results, so only the top-level summary reaches you. In `claude -p` and the Agent SDK the launcher does not wait, so a nested background subagent that finishes after its launcher has ended reports to the main conversation instead. To keep one particular subagent from spawning while nesting stays on, leave `Agent` out of its `tools` list or add it to `disallowedTools`. ## How the defaults moved The depth default has changed twice, which explains conflicting advice: 1. v2.1.172 through v2.1.216: nesting allowed by default, up to five layers, not configurable. 2. v2.1.217 through v2.1.218: default of one layer, so subagents could not spawn their own unless you raised the limit. 3. v2.1.219 onward: default of three. Advice written during the one-layer window, or any explanation saying the `Agent` tool is withheld from subagents by default, is out of date: on v2.1.286 the environment-variable reference and the subagents page both give three. ## Diagnosing a refused fan-out When a request such as "check every service for the timeout bug in parallel" stops with the concurrency error, work through these checks: - **Count what is running**, not what was spawned: `/tasks` lists running subagents, and finished ones no longer hold slots. - **Look for slot holders the limit does not police**: `/subtask` forks and resumed subagents count toward the running total even though they are never refused. - **Check for nesting**: subagents that spawn their own children multiply the running count quickly, and each layer below the main conversation counts. - **Decide whether the fan-out is the right shape**: twenty parallel reports land in the parent's context, so fewer subagents with broader scopes are often cheaper and easier to synthesise. - **Only then raise the cap**, knowing that every running subagent sends its own requests against the same usage limits. ## Setting both Put the values in the `env` block of a settings file so every session in the project gets them. Lowering depth to `2` lets your subagents delegate one more level and stops there; lowering concurrency trades throughput for a smaller burst of usage and smaller combined reports.
- With the default limits, can a reviewer subagent at the third layer still spawn a verifier?No. At the depth limit Claude Code withholds the `Agent` tool from every subagent except a fork, so the third-layer subagent does the verification itself and returns one summary. Raise `CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH` if the design genuinely needs another layer.
- Why might a nested background subagent's report land in your main conversation during a `claude -p` run?In non-interactive mode and the Agent SDK, a subagent that launches background subagents does not wait for them. If a nested one finishes after its launcher has ended, its report goes to the main conversation. Interactively, the launcher waits, so only the top-level summary reaches you.
- Subagents can never spawn subagents of their own
- Claude Code caps the total number of subagents per session
- A spawn over the concurrency limit is queued until a slot frees
- Setting the limit variable to 0 removes the cap
- Nesting is fixed at five layers and cannot be changed
Assumes Claude Code 2.1.286: both limits exist since 2.1.217, and the depth default has been 3 since 2.1.219 (it was 1 in 2.1.217-2.1.218 and a fixed 5 before).