Claude Code Subagents: When to Delegate and How to Scope Tools
When a Claude Code subagent beats the main thread, how to write its definition file, and how to limit its tools to what the task needs.
Updated · Markdown version
Claude Code subagents are separate workers that the main session hands a self-contained task, each with its own context window and an optional tool allowlist. Use one when the task produces output you do not need to keep in the main conversation, or when it should run with fewer tools than the main thread has.
Decide: subagent or main thread
A regular subagent works in its own context rather than inheriting the main conversation. Put the relevant paths, constraints and expected output in its delegation message. Keep back-and-forth work, tightly connected phases and quick targeted changes in the main thread. Claude Code also supports forks that reuse the parent context; those are a distinct mode, not the default behavior assumed in this example.
Delegate when you can describe the result you want in a short summary. Test runs with long logs, broad searches across a large codebase, and documentation sweeps fit that pattern, because only the summary returns to your context.
Create a project subagent
- Create the folder
.claude/agents/at the repository root if it does not exist. - Create
.claude/agents/test-triage.mdwith this frontmatter:
---
name: test-triage
description: Finds the cause of failing tests and reports file, line and likely fix. Use after code changes when tests fail.
tools: Read, Grep, Glob
model: haiku
---
Report each failing test as: name, file and line, one-sentence cause, suggested fix.
Do not edit files.- Write the body as the system prompt. Keep the report format fixed so the summary is easy to scan.
- Start or refresh the Claude Code session, then invoke the agent explicitly: "Use the test-triage subagent to explain why the login tests fail." You can also type
@agent-test-triage; local and plugin-scoped agents have different mention forms. If the agent is not recognized, inspect the definition and useclaude --debugto look for unrecognized top-level fields.
Scope the tools
toolslists the tools the subagent may use. If it is omitted, the subagent inherits the tools available to subagents, so an intended read-only reviewer should name only the read and search tools it needs.disallowedToolsremoves tools from either the inherited set or the set named intools. A scoped entry can still remove the whole tool, so test the resulting definition rather than assuming a command pattern creates a narrow exception.permissionModedoes not always override the parent session. InbypassPermissions,acceptEditsorauto, the subagent runs in the parent's mode; otherwise an unset value inherits the main conversation's mode.
Pitfalls
- Silent frontmatter errors.
nameanddescriptionare required, while an unrecognized optional field can be ignored without a normal error. If the agent loads but a setting appears ineffective, runclaude --debugand inspect the session debug log for fields Claude Code did not recognize. - Unscoped tools. Omitting
toolsinherits the tools available to subagents. If that inherited set includes mutation tools, a supposed reviewer can change files. List only the required tools and inspect the working tree after a trial run. - Thin delegation message. A regular subagent has a separate context, so it may need to read files that the main thread already examined. Put the relevant paths, goal, constraints and report format in the task message. A fork behaves differently because it reuses the parent context.
- Vague description. Claude delegates based on the description, so a generic one leads to no delegation at all. Notice that the subagent is never used. Fix it by naming the situations that should trigger it.
- Parallel edits. Workers using the same checkout can conflict. Claude Code can start a fork with
isolation: "worktree"on the Agent call so its edits land in a separate Git worktree. Do not assume every subagent invocation is isolated merely because an agent definition mentions isolation; verify the launch mode and resulting worktree before parallel edits.
When to use a skill instead
A subagent is the right unit when a task needs its own context window and its own tool limits. A skill loads instructions into the main conversation instead, which suits a repeated prompt that depends on what you have already discussed. The subagent-design skill is related to this guide: it drafts the delegation brief and the definition file, including the tool allowlist and the fixed report format, which are the parts most often left vague. Use it when you are writing several subagents and want the files to follow one pattern. For a single file like the one above, writing it by hand from the steps is usually enough.
FAQ
How do I create a subagent in Claude Code? Add a Markdown file to .claude/agents/ for one project or ~/.claude/agents/ for all projects, with name and description in the YAML frontmatter. The body becomes the subagent's system prompt.
What is the difference between a Claude Code skill and a subagent? A skill runs in the main context and suits reusable prompts. A subagent runs in its own context window and returns only its summary to the main conversation.
Can I reopen a completed subagent's details? On supported recent versions, run /tasks, open "Earlier in this session," and select an available subagent transcript. The official documentation says only some departed subagents can be reopened, so do not rely on that view as permanent audit storage.
Related skills in this catalog
Platform Score is our assessment under a published method. It is not a safety guarantee.
Related guides
Terms this guide uses
This item is a worked example in the AI glossary for:
Sources
- https://code.claude.com/docs/en/sub-agents (accessed 2026-11-25)