Claude Code subagents

Subagents let Claude Code delegate focused work to agents with their own instructions and context. They are useful when a task has separable research, review, testing, or implementation slices.

Last verified
Applies to
Claude Code 2.1.x. CLI spellings were checked on local version 2.1.198; official docs may describe newer 2.1.x behavior.
Verification method
Subagent definitions, tool allowlists, nesting, and permission behavior were reviewed in official docs and local agent flags. No subagent or model task was launched.
Official sources
SubagentsPermissionsChangelog

Reproducible practice

Design a read-only reviewer subagent and prove its tool boundary

Delegate an independent review without granting edits, secret access, or unrestricted child-agent spawning.

Prerequisites

  • A unique temporary repository.
  • A bounded review question with expected evidence.
  • No confidential fixture beyond what the reviewer must read.

Steps

  1. Create a scoped agent definition

    Allow only inspection tools and require evidence in the prompt.

    Step 1
    AGENT_LAB=$(mktemp -d "${TMPDIR:-/tmp}/claude-agent.XXXXXX")
    printf '%s\n' "$AGENT_LAB"
    cd "$AGENT_LAB" && git init -q
    mkdir -p .claude/agents
    printf '%s\n' '---' 'name: read-only-reviewer' 'description: Review a bounded diff and cite file evidence.' 'tools: Read, Grep, Glob' '---' '' 'Report findings by severity. Do not propose edits without cited evidence.' > .claude/agents/read-only-reviewer.md
  2. Inspect the allowlist

    Confirm Edit, Write, Bash, MCP, and Agent are absent before launch.

    Step 2
    sed -n '1,12p' .claude/agents/read-only-reviewer.md
  3. Run against a harmless fixture

    Ask the main session to use the named agent on one text file; model execution was not performed in this audit.

  4. Challenge the result independently

    Verify cited lines yourself and reject conclusions unsupported by the file.

Expected result

The reviewer returns file-grounded findings without editing, running commands, calling MCP, or spawning another agent.

Validation

  • Inspect tool usage rather than trusting the final prose.
  • Confirm `git status --short` is unchanged.
  • Run a negative prompt that requests an edit and verify the boundary holds.

Failure handling

Agent cannot launch

Check tool names and definition scope; unresolved tool entries now fail explicitly.

Review lacks evidence

Narrow the task and require file and line citations before acting.

Cleanup or rollback

  • Return to the original directory and remove only the printed AGENT_LAB path.
  • Disable a real project agent through reviewed version control rather than editing user-level agents.

Boundaries and when not to use it

  • Subagents inherit available tools unless restricted; least privilege must be explicit.
  • Delegation separates context, not responsibility: the main reviewer must verify findings and data exposure.

Why isolate work

Isolation reduces context clutter. A review agent can focus on bugs, a research agent can inspect documentation, and an implementation agent can work inside a narrower file set.

Parallel work

Parallel agents help when subtasks are independent. They are less useful when every next action depends on the same file or the same unresolved design decision.

Instruction design

A subagent needs a concrete scope, expected output, and boundaries. For code changes, assign ownership of files or modules so multiple agents do not overwrite each other.

Reviewing results

Treat subagent output as input, not final truth. Merge results only after checking evidence, conflicts, tests, and whether the result still matches the user goal.

CommandDescription
/tasksInspect running and completed background subagents.
/backgroundDetach the main session while work continues.
/branchTry an alternate approach without losing the current thread.
claude agents --jsonScript against active background sessions.

Subagent design patterns

Well-scoped subagents reduce coordination cost and make parallel work safer.

Subagent checklist

Related topics