Skip to content
한국어

Create Your Own Claude Code Subagents

In short
  • A subagent is a helper that works in its own context window with its own system prompt, tool access, and permissions, then returns a summary. Use one when a side task would flood your main conversation with logs, search results, or file contents.
  • A subagent is a Markdown file with YAML frontmatter. The body is its system prompt. Only name and description are required. Put it in .claude/agents/ (this project, shareable through git) or ~/.claude/agents/ (all your projects).
  • Limit it with tools (allowlist) or disallowedTools (denylist), and pick a model (sonnet, opus, haiku, fable, a full model ID, or inherit).
  • Call it three ways: name it in your prompt, @-mention it to guarantee it runs, or run the whole session as it with claude --agent <name>.
  • Subagents’ requests count toward the same usage limits as your main conversation (official docs).
  • On 2026-11-01 we validated the example files with claude plugin validate (Claude Code 2.1.316) and sent one real prompt that used the reviewer. We did not exercise every option in the tables below.

What you’ll be able to do after this tutorial

Section titled “What you’ll be able to do after this tutorial”
  • Create a read-only reviewer subagent and a test-runner subagent in a practice project.
  • Decide between asking Claude to write the file and writing it yourself, and where to save it.
  • Call a subagent by name, by @-mention, or as the whole session, and know which one forces it.
  • Spot why a subagent file is silently skipped.

C14 introduced subagents in one step, next to hooks and MCP. This tutorial is the hands-on version. Hooks (Coding 10) run code at fixed moments; a subagent is a separate worker that Claude delegates to.

Term Meaning
Subagent A specialized assistant with its own context window, system prompt, tool access, and permissions. It reports back a summary.
Frontmatter The settings between --- lines at the top of the file (name, description, tools, model, …).
Description Tells Claude when to delegate. It is the most important line.
Delegation Claude hands the task to the subagent, automatically when it matches the description or when you ask.
Scope Where the file lives: project (.claude/agents/), user (~/.claude/agents/), plugin, managed settings, or a CLI flag.

Where subagent files live (official docs, 2026-11-01)

Section titled “Where subagent files live (official docs, 2026-11-01)”
Location Scope Priority
Managed settings Organization-wide 1 (highest)
--agents CLI flag Current session only 2
.claude/agents/ Current project 3
~/.claude/agents/ All your projects 4
A plugin’s agents/ directory Where the plugin is enabled 5 (lowest)

If two subagents share a name, the higher-priority location wins. Claude Code scans .claude/agents/ and ~/.claude/agents/ recursively, so you can use subfolders such as agents/review/; identity comes only from the name field.

0/6 done
  1. Choose the scope and open the project

    Open a terminal in your practice folder and start claude. We’ll save the subagents in the project’s .claude/agents/ folder so they can be committed and shared with a team. For a personal helper available everywhere, use ~/.claude/agents/ instead.

    If the .claude/agents/ folder doesn’t exist yet, create it before you start the session, or restart Claude Code after adding the first file. The official docs explain that a running session doesn’t detect a newly created agents directory. Once the folder exists, edits to files in it are picked up within a few seconds.

  2. Write a read-only reviewer by hand

    Save this as .claude/agents/doc-reviewer.md:

    ---
    name: doc-reviewer
    description: Reviews Markdown documentation for unclear steps, typos, and broken internal links. Use proactively after docs are edited. Read-only.
    tools: Read, Grep, Glob
    model: sonnet
    color: blue
    ---
    You are a careful documentation reviewer. You never edit files.
    When invoked:
    1. Find the Markdown files the user named, or the ones changed recently if none were named.
    2. Read each file and check for unclear steps, typos, and internal links that point to files that do not exist.
    3. Report in three groups: Must fix, Should fix, Nice to have.
    For every item give the file, the line or heading, the problem, and a suggested rewrite.
    End with one sentence saying whether the docs are ready to publish.

    How the parts work, per the docs: name and description are required. tools is an allowlist (here: read and search only, so the subagent can’t edit or write files). If you leave tools out, the subagent inherits every tool available to subagents. model accepts sonnet, opus, haiku, fable, a full model ID, or inherit. color only changes how it looks in the task list. The body is the system prompt: the subagent gets only that plus basic environment details like the working directory, not the full Claude Code system prompt.

    Field names are camelCase and must match exactly. Claude Code ignores a field it doesn’t recognize without an error, so a typo such as disallowedtools silently does nothing.

    Copy and paste this prompt

    Create a project subagent in .claude/agents/ named doc-reviewer. It should review Markdown docs for unclear steps, typos, and broken internal links, be read-only (Read, Grep, Glob only), and report findings grouped as Must fix, Should fix, Nice to have. Show me the file after you write it.

    The prompt above is the other route: the docs say you can simply ask Claude to write the file for you. Review the result, especially the tools line, before relying on it.

  3. Call it by name

    Add a small README.md with a typo and a link to a file that doesn’t exist, then ask:

    Use the doc-reviewer subagent to review README.md

    There’s no special syntax for this: you name the subagent and Claude decides to delegate. In the transcript, the delegation appears as a tool call row with the subagent’s name and a short task description, such as doc-reviewer(...).

    What we ran: we did this in a throwaway folder with a seven-line README (a typo, a link to a missing setup.md, and vague steps) through a one-off claude -p prompt on Claude Code 2.1.316. The answer came back in the reviewer’s three groups (Must fix, Should fix, Nice to have), flagged the broken link and the typo, and made no edits. The wording of your result will differ.

  4. Write a test-runner and limit what it can do

    A second pattern from the docs: a worker whose job is to produce less output for your main conversation. Save as .claude/agents/test-runner.md:

    ---
    name: test-runner
    description: Runs the project's tests and reports only the failures with the likely cause. Use when a test run would flood the conversation with output.
    tools: Bash, Read, Grep, Glob
    model: haiku
    maxTurns: 15
    ---
    You run tests and summarize the result. You do not change any files.
    1. Find the test command (check package.json, Makefile, or README).
    2. Run it once.
    3. Reply with: pass/fail counts, then for each failure the test name, the error message, and your best guess at the cause.
    Do not paste full logs.

    maxTurns caps how many agentic turns it takes before stopping; when it reaches the limit the output is marked as partial. We validated this file but did not run it against a real test suite.

    tools and disallowedTools can be combined: disallowedTools is applied first, then tools is resolved against what remains. For whole MCP servers, mcp__github__* in tools grants a server’s tools, and mcp__github in disallowedTools withholds them.

  5. Force a specific subagent with an @-mention

    Natural language lets Claude decide. To guarantee a particular subagent runs, type @ and pick it from the list, like mentioning a file:

    @"doc-reviewer (agent)" look at the README changes

    Your full message still goes to Claude, which writes the subagent’s task prompt. The @-mention controls which subagent runs, not the prompt it receives. You can also type @agent-doc-reviewer by hand.

    To run the whole session as a subagent, with its tool limits and model on the main thread, start Claude Code with claude --agent doc-reviewer. To make a subagent the default for a project, set "agent": "doc-reviewer" in .claude/settings.json. The CLI flag wins if both are set. We did not test the --agent flag or the setting.

    Three ways to call a subagent
    1. Your promptName it: "Use the doc-reviewer subagent to ..."→ Claude decides whether to delegate
    2. Your promptType @ and pick the subagent→ That subagent is guaranteed to run for this task
    3. Your terminalclaude --agent doc-reviewer→ The whole session runs with that subagent's tools and model

    This is a simplified flow. See the official help page for the actual screens.

  6. Check that it loaded, and fix it if not

    Claude Code skips a subagent file without telling you in the session when the frontmatter has a problem: no name; an opening --- that isn’t on the first line; a name that starts with -, contains :, or is over 256 characters; a name but no description; or YAML that doesn’t parse.

    Two checks from the docs: run claude --debug and read the debug log (~/.claude/debug/<session-id>.txt; for a misspelled field, search for does not recognize or apply, which needs Claude Code v2.1.296 or later). Or, before a session, run claude plugin validate .claude/agents (v2.1.233 or later), which checks the directory you name for files whose frontmatter doesn’t parse. It doesn’t flag a file that parses but has no name. On our two files it printed “Validation passed”.

    About the /agents command: per the docs, it now prints a reminder to ask Claude or edit the agent folders directly. On Claude Code v2.1.197 and earlier it opened an interactive wizard instead. Check your version if your screen differs.

Frontmatter fields (official docs, 2026-11-01)

Section titled “Frontmatter fields (official docs, 2026-11-01)”
Field Required What it does
name Yes Unique ID (at most 256 characters). The filename doesn’t have to match. No :.
description Yes When Claude should delegate. Add “use proactively” to encourage automatic use.
tools No Allowlist, comma-separated or a YAML list. Omit to inherit every tool available to subagents.
disallowedTools No Denylist, removed from the inherited or listed tools.
model No sonnet, opus, haiku, fable, a full model ID, or inherit.
permissionMode No default, acceptEdits, auto, dontAsk, bypassPermissions, plan. May be overridden by the main conversation’s mode.
maxTurns No Stop after this many agentic turns.
skills No Skills to preload into its context at startup.
mcpServers No MCP servers available to this subagent.
hooks No Lifecycle hooks scoped to this subagent.
memory No Persistent memory scope: user, project, or local.
isolation No worktree runs it in a temporary git worktree.
background, effort, color, omitClaudeMd, autoCompactWindow, initialPrompt No See the docs; several need a recent Claude Code version.

Plugin subagents ignore hooks, mcpServers, and permissionMode for security. Copy the file into .claude/agents/ if you need them.

Claude Code picks a subagent’s model in this order: (1) the model Claude passes for that one call, (2) the model in the file, (3) the CLAUDE_CODE_SUBAGENT_MODEL environment variable, (4) the main conversation’s model. For how to split work across models, see C15.

Item Value (official docs)
Combined subagent descriptions Warning at startup above 15,000 tokens (built-ins excluded); everything still loads
Concurrent subagents 20 by default; a 21st fails with Concurrent subagent limit reached (CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS changes it)
Nesting A subagent can spawn subagents up to three layers below the main conversation by default (CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH; 1 turns nesting off)
Default run mode Subagents Claude spawns start in the background with a smaller built-in tool set
  • Cause: you created .claude/agents/ (or ~/.claude/agents/) after the session started, or the file is skipped.
  • Fix: restart Claude Code, then check the frontmatter (step 6).
  • Cause: the description is vague or too similar to another subagent’s.
  • Fix: make it specific about when to use it, add “use proactively”, or @-mention it.
  • Cause: background subagents keep only a limited built-in tool set, and some tools are never available to subagents. A tools list with no valid entries can fail to launch with an error naming the entries.
  • Fix: check names and capitalization against the official tools list in the docs.

A permission prompt appeared even though I set permissionMode

Section titled “A permission prompt appeared even though I set permissionMode”
  • Cause: the main conversation’s mode can override the subagent’s. For example, if the main session is in bypassPermissions, acceptEdits, or auto mode, the subagent runs in that mode.
  • Fix: set the mode of the main session deliberately; see the permission modes section of the docs.
  • Cause: only a summary comes back by design.
  • Fix: ask in the system prompt for exactly the details you want back (file names, line numbers, commands run).
  • Make each subagent good at one job, and write the description so only one subagent matches.
  • Give the least tools needed. Reviewers rarely need Edit or Write.
  • Put cheap, mechanical work (test runs, log reading) on haiku and keep judgment work on a stronger model, then compare results on a few real tasks.
  • Commit project subagents so teammates get the same helpers, and treat them like code: review changes to the tools line.
  • Use a subagent for verbose side work; stay in the main conversation for back-and-forth, quick edits, or work where several phases share context (the docs’ own guidance).
  • Subagent usage counts toward your plan limits, so many parallel subagents use usage faster.

Sources

Last verified: Nov 16, 2026

Prices, limits, and menu names change often. Before making an important decision, check the official pages above for the latest details.