Claude Code guide in three levels, Beginner to Advanced: hands-on lessons with checked exercises, every volatile fact cited to its source. Independent community guide.
JavaScript agent-skillsagentic-codingai-agentsanthropicanthropic-claudebest-practicesclaudeclaude-aiclaude-codeclaude-code-guideclaude-code-hooksclaude-code-pluginsclaude-code-skillsclaude-code-tutorialclaude-code-workflowsguidelearn-claude-codemcpsubagentstutorial
docs: add the Intermediate lesson on delegating to a custom subagent (#86)
Wesam Abousaid507a1f224264
diff --git a/README.md b/README.mdindex 1c3d934..5a8d0ff 100644--- a/README.md+++ b/README.md@@ -46,7 +46,7 @@ macOS, Linux and WSL. For Windows and other installers, see the [official setup**Beginner Electives:** [IDE extensions](docs/beginner/electives/ide-extensions.md) · [Screenshots and images](docs/beginner/electives/screenshots-and-images.md) · [The built-in teachers](docs/beginner/electives/built-in-teachers.md)-**Intermediate** (being rebuilt): [Level index](docs/intermediate/README.md) · [Turn a repeated workflow into a skill](docs/intermediate/01-first-skill.md) · [Organize project memory and see what loaded](docs/intermediate/02-organize-memory.md) · [Pick the model and effort](docs/intermediate/03-model-and-effort.md) · [Permissions, settings scopes and the sandbox](docs/intermediate/04-permissions-and-sandbox.md) · [Enforce a rule with a hook](docs/intermediate/05-hooks.md)+**Intermediate** (being rebuilt): [Level index](docs/intermediate/README.md) · [Turn a repeated workflow into a skill](docs/intermediate/01-first-skill.md) · [Organize project memory and see what loaded](docs/intermediate/02-organize-memory.md) · [Pick the model and effort](docs/intermediate/03-model-and-effort.md) · [Permissions, settings scopes and the sandbox](docs/intermediate/04-permissions-and-sandbox.md) · [Enforce a rule with a hook](docs/intermediate/05-hooks.md) · [Delegate to a custom subagent](docs/intermediate/06-subagents.md)**Advanced** (being rebuilt): [Level index](docs/advanced/README.md)@@ -84,13 +84,14 @@ The previous edition's README sections moved. If a link brought you here, find i- <a id="available-skills-reference"></a><a id="using-skills-in-workflow"></a>Moved to [Skills topic](docs/topics/skills.md).- <a id="hooks"></a><a id="setting-up-claude-hooks"></a><a id="setting-up-hooks"></a><a id="hook-events"></a><a id="hook-input"></a><a id="hook-output"></a><a id="security-considerations"></a><a id="hook-execution-details-and-debugging"></a><a id="execution--debugging"></a>Moved to [Enforce a rule with a hook](docs/intermediate/05-hooks.md).- <a id="ai-agents"></a>Moved to [Subagents and parallel work topic](docs/topics/subagents-and-parallel-work.md).-- <a id="running-agents-in-parallel"></a><a id="subagents--running-agents-in-parallel"></a><a id="1-git-worktrees--parallel-branches-parallel-sessions"></a><a id="2-general-purpose-subagents--when-one-claude-isnt-enough"></a><a id="orchestrating-specialists-from-the-main-session"></a>Moved to [Subagents and parallel work (previous edition)](docs/legacy/subagents-and-parallel-work.md).+- <a id="2-general-purpose-subagents--when-one-claude-isnt-enough"></a><a id="3-specialized-subagents--drop-in-role-prompts"></a>Moved to [Delegate to a custom subagent](docs/intermediate/06-subagents.md). The drop-in role prompts were removed in edition v2026.10: see the [CHANGELOG](CHANGELOG.md#removed-listings).+- <a id="running-agents-in-parallel"></a><a id="subagents--running-agents-in-parallel"></a><a id="1-git-worktrees--parallel-branches-parallel-sessions"></a><a id="orchestrating-specialists-from-the-main-session"></a>Moved to [Subagents and parallel work (previous edition)](docs/legacy/subagents-and-parallel-work.md).- <a id="agent-teams-experimental---2026"></a><a id="agent-teams-experimental"></a><a id="enable-it"></a><a id="the-example-that-justifies-the-cost"></a><a id="staff-a-team-with-the-role-prompts-you-already-have"></a><a id="three-things-that-catch-people-out"></a><a id="monitoring-and-the-naming-trap"></a><a id="best-practices"></a>Moved to [Agent teams (previous edition)](docs/agent-teams.md).- <a id="dynamic-workflows"></a><a id="try-it-in-2-minutes--no-script-required"></a><a id="starting-your-own"></a>Moved to [Dynamic workflows (previous edition)](docs/workflows.md).- <a id="beyond-one-terminal--the-2026-automation-surface"></a>Moved to [Feature map](docs/reference/feature-map.md).- <a id="model-context-protocol-mcp"></a><a id="the-nm-problem-mcp-solves"></a><a id="three-pillars"></a><a id="the-mcp-registry--self-discovering-agents"></a><a id="the-mcp-ecosystem-today"></a>Moved to [MCP (previous edition)](docs/legacy/mcp.md).- <a id="fast-mode"></a><a id="fast-mode-"></a>Moved to [Fast mode (previous edition)](docs/legacy/fast-mode.md).-- <a id="beyond-your-own-skills--the-ecosystem"></a><a id="3-specialized-subagents--drop-in-role-prompts"></a><a id="featured-mcp-servers"></a><a id="more-mcp-servers-worth-knowing"></a><a id="super-claude-framework"></a><a id="the-bmad-method--ai-agent-framework"></a>Removed in edition v2026.10: listings no lesson uses. See the [CHANGELOG](CHANGELOG.md#removed-listings).+- <a id="beyond-your-own-skills--the-ecosystem"></a><a id="featured-mcp-servers"></a><a id="more-mcp-servers-worth-knowing"></a><a id="super-claude-framework"></a><a id="the-bmad-method--ai-agent-framework"></a>Removed in edition v2026.10: listings no lesson uses. See the [CHANGELOG](CHANGELOG.md#removed-listings).- <a id="updates--deprecations-february-2026"></a><a id="updates--deprecations"></a><a id="updates--deprecations-as-of-july-2026"></a>Removed in edition v2026.10: the guide's summaries of Claude Code releases. See the [CHANGELOG](CHANGELOG.md#removed-changelog-mirror).- <a id="faq"></a>Moved to [FAQ (previous edition)](docs/reference/faq.md).- <a id="references"></a>Moved to [Further learning](docs/reference/further-reading.md).diff --git a/docs/intermediate/05-hooks.md b/docs/intermediate/05-hooks.mdindex 04e4df2..f175cb7 100644--- a/docs/intermediate/05-hooks.md+++ b/docs/intermediate/05-hooks.md@@ -141,4 +141,4 @@ If a row is wrong, test that path alone and print the name the script sees: `bas<sub>Sources: [Automate actions with hooks](https://code.claude.com/docs/en/hooks-guide) · [Hooks reference](https://code.claude.com/docs/en/hooks) · [Configure permissions](https://code.claude.com/docs/en/permissions) · [Debug your configuration](https://code.claude.com/docs/en/debug-your-config)</sub>-<sub>← [Permissions, settings scopes and the sandbox](04-permissions-and-sandbox.md) · [Intermediate index](README.md) · Topic: [Hooks](../topics/hooks.md) · [Stuck on this lesson?](https://github.com/wesammustafa/Claude-Code-Everything-You-Need-to-Know/issues/new?template=lesson-feedback.yml&lesson=i-5)</sub>+<sub>← [Permissions, settings scopes and the sandbox](04-permissions-and-sandbox.md) · [Intermediate index](README.md) · [Delegate to a custom subagent](06-subagents.md) → · Topic: [Hooks](../topics/hooks.md) · [Stuck on this lesson?](https://github.com/wesammustafa/Claude-Code-Everything-You-Need-to-Know/issues/new?template=lesson-feedback.yml&lesson=i-5)</sub>diff --git a/docs/intermediate/06-subagents.md b/docs/intermediate/06-subagents.mdnew file mode 100644index 0000000..90caa7a--- /dev/null+++ b/docs/intermediate/06-subagents.md@@ -0,0 +1,121 @@+# Delegate to a custom subagent++<sub>**Intermediate** · Lesson 6 of 8 · about 20 minutes · Needs: [Permissions, settings scopes and the sandbox](04-permissions-and-sandbox.md) · Verified against Claude Code v2.1.285 (stable) on 2026-10-06</sub>++## Goal++By the end of this lesson you can write a subagent that has only the tools its task needs, and delegate work to it.++## You need++- One of your own repositories, and your copy of the practice template with Node.js LTS: the check runs from the copy. To practice in the template copy instead, use it for both.+- `jq`, for the delegation log in Your turn.+- Usage: light to moderate. Each delegation starts a second context window.++## The idea++A [subagent](https://code.claude.com/docs/en/sub-agents) is a separate Claude that your session hands a task to. It works in its own context window, with its own instructions and only the tools you give it, and returns a summary. Its file reads and search results stay out of your conversation:++```mermaid+flowchart LR+ accTitle: How a subagent keeps work out of your context+ accDescr: Your conversation delegates a task to a subagent. The subagent works in its own context window with its own instructions and tools, reading files and searching. Only its summary comes back to your conversation.+ A["Your conversation"] -->|task| B["Subagent: own context window, own instructions, limited tools"]+ B -->|reads and searches files| B+ B -->|summary only| A+```++A project subagent is a Markdown file in `.claude/agents/`: frontmatter with a `name` and a `description` (both required), an optional `tools` list, then the instructions it works from. Claude reads the description to decide when to delegate; you can also name the subagent in your request.++Delegate a task whose output you don't need in full, or that should run with restricted tools. Keep it in the main conversation when it needs back-and-forth, or when it's a quick, targeted change ([Choose between subagents and main conversation](https://code.claude.com/docs/en/sub-agents#choose-between-subagents-and-main-conversation)).++## Worked example++1. In your repository, download this guide's example [`code-reviewer`](../../examples/intermediate/06-subagents/dot-claude/agents/code-reviewer.md) and read it:++ ```bash+ mkdir -p .claude/agents+ curl -fsSL https://raw.githubusercontent.com/wesammustafa/Claude-Code-Everything-You-Need-to-Know/main/examples/intermediate/06-subagents/dot-claude/agents/code-reviewer.md -o .claude/agents/code-reviewer.md+ ```++ Its frontmatter limits it to three tools, so it can read and search files but can't edit them or run commands:++ ```yaml+ name: code-reviewer+ tools: Read, Grep, Glob+ ```++2. Start `claude`. If a session was already running when you created `.claude/agents/`, restart it: Claude Code watches only the agent folders that existed when the session started ([Write subagent files](https://code.claude.com/docs/en/sub-agents#write-subagent-files)).+3. Make a small change in a source file, then ask:++ ```text+ Use the code-reviewer agent to review my uncommitted changes.+ ```++ The transcript shows a row such as `code-reviewer(Review uncommitted changes)` while it works. The reviewer can't run `git diff`, so it starts from the task message your session writes for it and a git status snapshot ([What loads at startup](https://code.claude.com/docs/en/sub-agents#what-loads-at-startup)); then you get its findings as a summary, grouped by severity. Your wording will differ.++## Your turn++Finish a read-only subagent that finds code no test calls, then delegate to it.++1. Save this as `.claude/agents/test-gaps.md`, and replace both `TODO`s. Give it only read-only tools:++ ```markdown+ ---+ name: test-gaps+ description: TODO+ tools: TODO+ ---++ You find code that no test exercises. You never change files.++ 1. List the functions each source file exports.+ 2. For each one, search the test files for a call to it.+ 3. Report the functions that no test calls, grouped by file, with one line on what a test for each should check. If every function has a test, say so.+ ```++2. Commit it.+3. Save this delegation log to `.practice/i-6-hook.json`. It is a [`SubagentStart` hook](https://code.claude.com/docs/en/hooks#subagentstart) that appends the name of each subagent Claude starts to `.practice/i-6-agents.txt`, and logs nothing else:++ ```json+ {+ "hooks": {+ "SubagentStart": [+ { "hooks": [ { "type": "command", "command": "jq -r '.agent_type' >> \"$CLAUDE_PROJECT_DIR/.practice/i-6-agents.txt\"" } ] }+ ]+ }+ }+ ```++4. Start `claude --settings .practice/i-6-hook.json` and ask `Use the test-gaps agent to list the functions no test calls.`++## Check++You are done when all of these are true:++- [ ] The committed subagent has its name and a description of when to use it: `git show HEAD:.claude/agents/test-gaps.md | head -5`.+- [ ] Its tools are read-only: the `tools` line names tools such as `Read`, `Grep` and `Glob`, and no tool that edits files or runs commands, such as `Edit`, `Write` or `Bash`.+- [ ] Claude delegated to it: `grep -x test-gaps .practice/i-6-agents.txt` prints `test-gaps`.+- [ ] Self-check (not tested): the subagent's file reads stayed out of your conversation, and only its summary came back.++Run `npm run check -- i-6 --dir <your repo>` from your practice copy, or `npm run check -- i-6` in the copy itself.++If the first item fails, check the frontmatter: Claude Code skips an agent file without a `name`, and one with a `name` but no `description`. If the tools item fails, remove each tool that can change files or run commands; without a `tools` line, a subagent gets every tool. If the log has no `test-gaps` line, start the session with the hook file and name the subagent in your request.++## Watch out++- A subagent that lists `Bash` can still change files through shell commands, such as a `sed` edit or a [`> file` redirect](https://code.claude.com/docs/en/permissions#redirections), even without `Edit` or `Write`. Leave `Bash` out, and let your session pass it what a command would have shown.+- On macOS, Linux and WSL, where Glob and Grep are absent by default, a subagent whose `tools` list names `Glob` or `Grep` and leaves out `Bash` gets the ones it names ([Glob tool behavior](https://code.claude.com/docs/en/tools-reference#glob-tool-behavior)).+- A custom subagent reads your `CLAUDE.md` as the main conversation does, but the built-in Explore and Plan subagents skip it: restate a rule in your request when Claude hands them work ([Check common causes](https://code.claude.com/docs/en/debug-your-config#check-common-causes)).++## Go further++- [Introduction to subagents](https://academy.claude.com/courses/introduction-to-subagents): Claude Academy's course on designing and using subagents.+- [How and when to use subagents in Claude Code](https://claude.com/blog/subagents-in-claude-code): Anthropic on when to delegate and when to stay in the main session.+- [Create custom subagents](https://code.claude.com/docs/en/sub-agents): every frontmatter field, background subagents and resuming one.++---++<sub>Sources: [Create custom subagents](https://code.claude.com/docs/en/sub-agents) · [Hooks reference](https://code.claude.com/docs/en/hooks) · [Tools reference](https://code.claude.com/docs/en/tools-reference) · [Configure permissions](https://code.claude.com/docs/en/permissions) · [Debug your configuration](https://code.claude.com/docs/en/debug-your-config)</sub>++<sub>← [Enforce a rule with a hook](05-hooks.md) · [Intermediate index](README.md) · Topic: [Subagents and parallel work](../topics/subagents-and-parallel-work.md) · [Stuck on this lesson?](https://github.com/wesammustafa/Claude-Code-Everything-You-Need-to-Know/issues/new?template=lesson-feedback.yml&lesson=i-6)</sub>diff --git a/docs/intermediate/README.md b/docs/intermediate/README.mdindex 0223434..0fe9253 100644--- a/docs/intermediate/README.md+++ b/docs/intermediate/README.md@@ -30,7 +30,7 @@ Five of the ways to extend Claude Code; [Extend Claude Code](https://code.claude- **Skills:** you give Claude the same instructions or workflow again and again. A project skill lives in `.claude/skills/<name>/SKILL.md` ([Extend Claude with skills](https://code.claude.com/docs/en/skills); lesson: [Turn a repeated workflow into a skill](01-first-skill.md)).- **Hooks:** something must happen every time at a set point in a session, such as before a tool runs, whatever Claude decides. Hooks are defined in a settings file such as `.claude/settings.json` ([Hooks](https://code.claude.com/docs/en/hooks); lesson: [Enforce a rule with a hook](05-hooks.md)).-- **Subagents:** a side task should run in its own context window and report back. A project subagent lives in `.claude/agents/` ([Create custom subagents](https://code.claude.com/docs/en/sub-agents); previous edition: [Subagents and parallel work](../legacy/subagents-and-parallel-work.md)).+- **Subagents:** a side task should run in its own context window and report back. A project subagent lives in `.claude/agents/` ([Create custom subagents](https://code.claude.com/docs/en/sub-agents); lesson: [Delegate to a custom subagent](06-subagents.md)).- **Dynamic workflows:** the job needs more agents than one conversation can coordinate. A saved project workflow lives in `.claude/workflows/` ([Workflows](https://code.claude.com/docs/en/workflows); previous edition: [Dynamic workflows](../workflows.md)).- **MCP servers:** Claude needs tools or data outside your files, such as a browser, a database or an API. A project-scope server is listed in `.mcp.json` at the project root ([Connect Claude Code to tools via MCP](https://code.claude.com/docs/en/mcp); previous edition: [MCP](../legacy/mcp.md)).@@ -43,7 +43,7 @@ They combine: a [plugin](https://code.claude.com/docs/en/plugins) packages skill3. [Pick the model and effort](03-model-and-effort.md)4. [Permissions, settings scopes and the sandbox](04-permissions-and-sandbox.md)5. [Enforce a rule with a hook](05-hooks.md)-6. Delegate to a custom subagent+6. [Delegate to a custom subagent](06-subagents.md)7. Connect a tool with MCP8. Install and manage pluginsdiff --git a/docs/legacy/subagents-and-parallel-work.md b/docs/legacy/subagents-and-parallel-work.mdindex 84ad219..f0a48b1 100644--- a/docs/legacy/subagents-and-parallel-work.md+++ b/docs/legacy/subagents-and-parallel-work.md@@ -10,7 +10,7 @@ Claude Code has **four** ways to run agents at once. They're easy to confuse, so| Surface | Who coordinates | Reach for it when… ||---|---|---|-| **Subagents** *(below)* | Claude, turn by turn, inside one session | A side task would flood your main conversation with search results, logs, or file contents you'll never reference again |+| **[Subagents](../intermediate/06-subagents.md)** | Claude, turn by turn, inside one session | A side task would flood your main conversation with search results, logs, or file contents you'll never reference again || **Agent view**: `claude agents` *(research preview)* | **You**: hand off, check back later | You have several independent tasks and want to dispatch them, glance at status, and step in only when one needs you. Each dispatched session gets **its own worktree automatically** || **[Agent Teams](../../README.md#agent-teams-experimental)** *(experimental)* | A lead agent supervising peer sessions | Workers need to **talk to each other**: share findings, challenge each other, self-claim from a shared task list || **[Dynamic Workflows](../../README.md#dynamic-workflows)** | **A script**, not Claude's judgement | The job outgrows a handful of subagents, or you want findings cross-checked against each other: codebase-wide audits, 500-file migrations |@@ -41,16 +41,7 @@ git worktree remove ../feature-a # clean up when done> [!TIP]> Prefer not to manage them by hand? `claude agents` (agent view) puts **each dispatched session in its own worktree automatically**, and `/batch` does the same per unit of work.-<a id="2-general-purpose-subagents--when-one-claude-isnt-enough"></a>-## 2. General-purpose subagents: when one Claude isn't enough--From your main session, ask Claude to spawn subagents for a parallel sub-task. Each subagent runs in its own context window and reports a summary back, so the main session stays focused.--```markdown-Analyze the implementation of the payment feature.-Spawn 5 subagents to accelerate the work.-Ultrathink.-```+Subagents now have their own lesson: [Delegate to a custom subagent](../intermediate/06-subagents.md).---diff --git a/docs/reference/commands.md b/docs/reference/commands.mdindex 7f9ba96..81cd6c3 100644--- a/docs/reference/commands.md+++ b/docs/reference/commands.md@@ -31,7 +31,7 @@ Electives: `/powerup` and `/output-style` ([the built-in teachers](../beginner/e- `/permissions`: manage allow, ask and deny rules. [Permissions, settings scopes and the sandbox](../intermediate/04-permissions-and-sandbox.md)- `/sandbox`: turn the sandbox on or off. [Permissions, settings scopes and the sandbox](../intermediate/04-permissions-and-sandbox.md)- `/hooks`: see which hooks are configured. [Enforce a rule with a hook](../intermediate/05-hooks.md)-- `/agents`: a reminder of how to create and manage subagents. *Delegate to a custom subagent*+- `/agents`: a reminder of how to create and manage subagents. [Delegate to a custom subagent](../intermediate/06-subagents.md)- `/mcp`: manage MCP server connections and sign-ins. *Connect a tool with MCP*- `/plugin`: browse, install and manage plugins. *Install and manage plugins*diff --git a/docs/reference/feature-map.md b/docs/reference/feature-map.mdindex ddacde0..91c4da6 100644--- a/docs/reference/feature-map.md+++ b/docs/reference/feature-map.md@@ -52,7 +52,7 @@ How to read it:| [Skills](https://code.claude.com/docs/en/skills); taught in [Turn a repeated workflow into a skill](../intermediate/01-first-skill.md) | – | Every provider || [Hooks](https://code.claude.com/docs/en/hooks-guide); taught in [Enforce a rule with a hook](../intermediate/05-hooks.md) | – | Every provider || [Agent hooks](https://code.claude.com/docs/en/hooks-guide#agent-based-hooks) | "Agent hooks are experimental." | As for hooks |-| [Subagents](https://code.claude.com/docs/en/sub-agents) | – | Every provider |+| [Subagents](https://code.claude.com/docs/en/sub-agents); taught in [Delegate to a custom subagent](../intermediate/06-subagents.md) | – | Every provider || [MCP](https://code.claude.com/docs/en/mcp) | – | Every provider; claude.ai connectors only when you sign in with a Claude subscription || [Plugins and marketplaces](https://code.claude.com/docs/en/plugins/overview) | – | Every provider || [Channels](https://code.claude.com/docs/en/channels) | "Channels are in research preview." | A Claude subscription (Team and Enterprise once an admin turns them on) and the Console; not Claude Platform on AWS, Bedrock, Agent Platform or Foundry |diff --git a/docs/topics/subagents-and-parallel-work.md b/docs/topics/subagents-and-parallel-work.mdindex d099e9c..754f28a 100644--- a/docs/topics/subagents-and-parallel-work.md+++ b/docs/topics/subagents-and-parallel-work.md@@ -6,7 +6,9 @@ Ways to split work across contexts and sessions: subagents, worktrees, dynamic w## Lessons-This topic's lessons are linked here in level order, with its Electives, as they are published.+- Intermediate: [Delegate to a custom subagent](../intermediate/06-subagents.md)++More of this topic's lessons and Electives are linked here as they are published.## Official docsdiff --git a/examples/README.md b/examples/README.mdindex 560a63f..653c474 100644--- a/examples/README.md+++ b/examples/README.md@@ -16,3 +16,4 @@ Each example is listed here, with its lesson, platforms and test line, when the- [`intermediate/01-first-skill/`](intermediate/01-first-skill/README.md): the `five-whys` skill and the unfinished `tdd` skill, for [Turn a repeated workflow into a skill](../docs/intermediate/01-first-skill.md). Any platform. Tested in Claude Code v2.1.285 (stable) on macOS, 2026-10-06.- [`intermediate/05-hooks/`](intermediate/05-hooks/README.md): three teaching hooks (protect-files, format-after-edit and desktop-notify) and the settings that register them, for [Enforce a rule with a hook](../docs/intermediate/05-hooks.md). macOS, Linux and WSL 2, with bash, jq and python3. Tested in Claude Code v2.1.285 (stable) on macOS, 2026-10-06.+- [`intermediate/06-subagents/`](intermediate/06-subagents/README.md): the read-only `code-reviewer` subagent, for [Delegate to a custom subagent](../docs/intermediate/06-subagents.md). Any platform. Tested in Claude Code v2.1.285 (stable) on macOS, 2026-10-06.diff --git a/specialized-agents/README.md b/specialized-agents/README.mdindex 7d1d637..9cb0522 100644--- a/specialized-agents/README.md+++ b/specialized-agents/README.md@@ -1,3 +1,3 @@# Specialized agents-Removed in edition v2026.10: the ten role prompts and nine role descriptions this folder held, which no lesson uses. To write a subagent of your own, see [Create custom subagents](https://code.claude.com/docs/en/sub-agents). See the [CHANGELOG](../CHANGELOG.md#removed-listings).+Removed in edition v2026.10: the ten role prompts and nine role descriptions this folder held, which no lesson uses. To write a subagent of your own, see the Intermediate lesson [Delegate to a custom subagent](../docs/intermediate/06-subagents.md). See the [CHANGELOG](../CHANGELOG.md#removed-listings).