Skip to main content
skill CX Codex

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.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add ruvnet/ruflo --skill nested-subagents -a claude-code
Open on GitHub

What 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_type values 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-coordinator for 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-code

2Manually: put the files in place

~/.claude/skills/nested-subagents/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/nested-subagents/

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

Similar skills

All skills for Agent setup: memory, context, orchestration →

From the same set146

trader-cloud-backtest Sends heavy neural-trader work — walk-forward, Monte-Carlo, parameter sweeps, model training — to a Managed Agent container instead of your own machine. CX Codex +1 599installs managed-agent Skill for running Anthropic Claude Managed Agents in the cloud. Best for long asynchronous tasks inside a persistent container with network access. CX Codex +1 596installs trader-portfolio-cg Solves mean-variance optimization with conjugate gradients instead of the Neumann series. Useful when you recompute portfolio weights often and latency gets in the way. CX Codex +1 513installs trader-explain Identifies key features behind LSTM and Transformer trading signals with PageRank and writes signed audit artifacts. CX Codex +1 510installs gaia-submission Runs the full GAIA benchmark pipeline from API key checks to building a HAL-compatible submission package. Useful when evaluating agent accuracy. CX Codex +1 435installs gaia-debugging gaia-debugging examines a failed GAIA task, classifies the failure, and suggests a targeted fix before you submit the task again. CX Codex +1 435installs gaia-architecture-comparison Compares the ruflo GAIA benchmark harness with Princeton's HAL reference implementation. It identifies architectural gaps and prioritizes engineering steps to improve benchmark pass rates. CX Codex +1 432installs claude-flow Connect Claude Code to claude-flow tools for agent coordination and shared task context. The MCP setup uses ANTHROPIC_API_KEY. CX Codex +1 ★ 79,357repo stars

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor