The Error
Cannot spawn subagent: maximum agent depth reached (3/3)
The Fix
# Restructure your CLAUDE.md to use sequential tasks instead of nested agents
# Replace nested agent calls with direct tool usage in the parent agent
# In your CLAUDE.md, add:
echo "Prefer sequential tool calls over nested agent spawning" >> CLAUDE.md
Why This Works
Claude Code enforces a maximum nesting depth of 3 levels to prevent infinite recursion and resource exhaustion. When an agent spawns a subagent that spawns another subagent, the third level hits the ceiling. Flattening the task structure keeps execution within bounds.
If That Doesn’t Work
# Break the work into separate top-level invocations
claude "Do step 1: analyze the codebase structure"
claude "Do step 2: implement changes based on analysis"
claude "Do step 3: verify and test the implementation"
Run each step as an independent session so each starts at depth 0. You can pass context between sessions using files — write analysis results to a JSON file in step 1, then reference that file in step 2 to maintain continuity without requiring nested agents.
Prevention
Add to your CLAUDE.md:
Never design workflows requiring more than 2 levels of agent nesting. Use sequential tool calls or break into separate top-level sessions instead of spawning deeply nested subagents.