nested-subagents — Claude Code skill: nested sub-agents to depth 5
Spawns a tree of sub-agents where each child can spawn its own children. Useful when intermediate state would overflow a single agent's context.
npx skills add ruvnet/ruflo --skill nested-subagents -a claude-codeWhat nested-subagents does
The skill starts nested-coordinator as the top-level agent, and that agent lays out the spawn tree with TodoWrite before it makes any Task call. Children hand back structured summaries of roughly 200 tokens rather than full transcripts, so the top level never sees the inner chatter. The point is context management, not parallelism: every level gets a fresh window.
Any agent whose YAML frontmatter declares Task can spawn the next level. Leaf agents without Task in their tools list cannot, so you attach their subagent_type directly under the coordinator instead. On every spawn the post-task hook writes parent_agent_id and depth to AgentDB, which gives you per-tree spend after the run rather than a flat per-agent number.
Depth is capped. The Anthropic API allows 5 levels, while ruflo defaults to 4 as a one-level guard band, configurable in claude-flow.config.json. Setting CLAUDE_FLOW_STRICT_NESTING=true enforces the ruflo cap. At the limit the hook returns a typed NESTING_DEPTH_EXCEEDED error with the full chain in the payload, leaving the parent to summarize, hand off or abort. Nesting costs latency, so if a flat fan-out of several Task calls in one message gets the job done, the tree only slows you down.
What it helps with
- split a research task into layers without flooding the parent context
- run recursive entity expansion for a dossier
- give leaves different
subagent_typevalues with their own prompts - attribute cost across a whole agent tree instead of one agent
- hold intermediate state that will not fit in one window
Example prompts for the agent
- Break this task into a sub-agent tree with nested-subagents: research, expand, verify, synthesize
- Spawn
nested-coordinatorfor this request and show me the task tree before anything runs - Check nesting depth first: keep it at 4 levels or the hook returns
NESTING_DEPTH_EXCEEDED
How to install nested-subagents
1Skills CLI (Vercel)
npx skills add ruvnet/ruflo --skill nested-subagents -a claude-code2Manually: put the files in place
~/.claude/skills/nested-subagents/SKILL.mdCopy the whole skill folder; for one project use .claude/skills/nested-subagents/
1Skills CLI (Vercel)
npx skills add ruvnet/ruflo --skill nested-subagents -a codex2Manually: put the files in place
.agents/skills/nested-subagents/SKILL.mdFor all projects use ~/.agents/skills/
1Skills CLI (Vercel)
npx skills add ruvnet/ruflo --skill nested-subagents -a cursor2Manually: put the files in place
.cursor/skills/nested-subagents/SKILL.mdCursor also reads .agents/skills/ and .claude/skills/
1GitHub CLI
gh skill install ruvnet/ruflo nested-subagents2Manually: put the files in place
.github/skills/nested-subagents/SKILL.md1Skills CLI (Vercel)
npx skills add ruvnet/ruflo --skill nested-subagents -a gemini-cli2Platform CLI
gemini skills install https://github.com/ruvnet/ruflo --path plugins/ruflo-agent/skills/nested-subagentsManually: put the files in place
.devin/skills/nested-subagents/SKILL.mdThe legacy .windsurf/skills/ path still works
Skills CLI (Vercel)
npx skills add ruvnet/ruflo --skill nested-subagents -a opencodeClaude.ai
Zip the folder and upload it in Settings → Capabilities → Skills
Step by step, with commands for each platform: How to install skills in Claude Code: five ways
Questions about nested-subagents
How do I install nested-subagents in Claude?
Run npx skills add ruvnet/ruflo --skill nested-subagents -a claude-code. The skill lands at ~/.claude/skills/nested-subagents/SKILL.md.
What does nested-subagents do?
It spawns a tree of sub-agents where each child can spawn children of its own. Each level works in a fresh context and returns a compact summary upward.
How deep can the agents nest?
The Anthropic API allows 5 levels; ruflo defaults to 4 as a one-level guard band. Change the cap in claude-flow.config.json, or enforce it with CLAUDE_FLOW_STRICT_NESTING=true.
Source text
SKILL.md · MIT
---
name: nested-subagents
description: Spawn nested sub-agents (agents that spawn sub-agents, up to depth=5) via Claude Code's native Task tool — for context-managed deep delegation
argument-hint: "<problem-statement>"
allowed-tools: Task TodoWrite Read Grep Glob Bash
---
# Nested Sub-Agents
Spawn a tree of sub-agents where each child can itself spawn children, up to 5 levels deep. The motivation is **context management**, not parallelism: each level gets a fresh context window so deep work doesn't blow the top-level agent's context budget.
## When to use
- The problem decomposes into nested layers (research → expand → verify → synthesize), each of which would otherwise pollute the parent's context.
- A single agent's context window would not be enough to hold all the intermediate state.
- The leaves of the tree are different `subagent_type`s and need their own specialized prompts (e.g., `pii-detector` at one leaf, `tester` at another).
Skip this skill when flat fan-out (`Task` × N in one message) suffices — nesting adds latency.
## Steps
1. **Invoke the coordinator** — spawn `nested-coordinator` as your top-level agent:
```
Task({
subagent_type: "nested-coordinator",
name: "root-coordinator",
description: "Decompose and delegate <problem>",
prompt: "<problem statement, with constraints and expected output shape>"
})
```
2. **The coordinator decomposes first** — it lays out the spawn tree via `TodoWrite` before any `Task` call. Inspect the tree before approving deep work.
3. **Children spawn children** — any `nested-coordinator` (or any other agent whose YAML frontmatter declares `tools: [..., Task]`) can itself call `Task` to spawn the next level. Leaf agents (without `Task` in their tools list) cannot.
4. **Each level reports a summary** — children return ~200-token structured summaries, not full transcripts. The whole point is to keep the parent's context clean.
5. **Tree shape is persisted** — the `post-task` hook writes `parent_agent_id` and `depth` to AgentDB on every spawn (ADR-147 P2). Query after the run for cost attribution and pattern learning.
## Depth budget
| Source | Limit |
|---|---|
| Anthropic API | 5 levels (announced 2026-06-09) |
| Ruflo default (`pre-task` hook) | 4 levels — one-level guard band, configurable in `claude-flow.config.json` |
| Strict-mode env var | `CLAUDE_FLOW_STRICT_NESTING=true` to enforce the ruflo cap |
The hook returns a typed `NESTING_DEPTH_EXCEEDED` error at the cap, with the full chain in the payload so the parent can decide to summarize, hand off, or abort.
## Benefits
- **Context isolation per level** — top-level coordinator never sees inner chatter; leaf summaries climb back up.
- **Deeper delegation without re-summarization** — eliminates the "summarize at level 1 to fit it all" anti-pattern that flat fan-out forces.
- **Tree-shaped cost attribution** — `parent_agent_id` lineage gives accurate per-tree spend, not just flat per-agent.
- **Maps cleanly onto ruflo's existing orchestrators** — `ruflo-sparc:sparc-orchestrator` (5 phases ≈ 5 levels), `ruflo-goals:dossier-investigator` (recursive entity expansion), `v3-queen-coordinator` (hierarchical-mesh top).
## Anti-patterns (do NOT)
- **Pass `Task` to leaf agents.** Leaves must not spawn. Add the leaf's `subagent_type` directly under the coordinator instead.
- **Wrap a Tier-1 codemod in a coordinator.** Codemods (`hooks_codemod`) are depth-0 deterministic transforms — never put them inside a spawn tree.
- **Nest "for cleanliness".** A premature nesting layer wastes latency and cost without saving context. If one agent can do the work, use one agent.
- **Return full transcripts from a child.** That defeats the entire purpose of nesting. Children return structured summaries.
## Related
- **Agent**: `ruflo-agent:nested-coordinator` — the orchestrator
- **ADR-147** — design rationale and four-phase rollout
- **ADR-144** — authorization propagation shares the depth counter as `AuthScope.delegationDepth`
- **ADR-099** — dossier investigator (recursive parallel research) is the textbook deep use case
© ruvnet · MIT · ruvnet/ruflo