Claude Code Subagents Tutorial: Build a Read-Only Code Reviewer

Published Nov 26, 2026 · 12 min read

This is the Claude Code subagent most tutorials give you for code review: tools: Read, Grep, Glob, Bash. No Edit tool, no Write tool, so it's read-only. It isn't. Anthropic's own docs say it right under their code-reviewer example: it can still change files through Bash. And it's not just rm. I ran git diff, about the most harmless command there is, with one extra flag. It wrote a brand-new file into the repo:

Real run (demo-bash-guard/run.txt, Nov 24, 2026):

$ ./make-repo.sh && cd /tmp/vk-review && git diff --output=review.txt && git status --short
repo ready: /tmp/vk-review (1 file changed)
 M src/cart.js
?? review.txt

This guide builds a Claude Code subagent from scratch: a code reviewer you can actually trust. What a subagent is, the file format, how Claude decides to use it, the settings that quietly give write access back, a hook that really locks Bash down, which model it runs on, and how the same file works in Cursor. Every file is below, copy-paste ready, and you can download them all.

How this was tested: every terminal block on this page is a real run (Linux, bash 5.2, git 2.47, jq 1.7, Python 3, Nov 24, 2026) of shell, git and small scripts on plain files. Claude Code and Cursor were not run for these demos. agents-audit.py is our script that encodes the docs' rules; its output is not Claude Code output. The hook tests pipe our guard script the same JSON shape Claude Code sends a hook. The one block that stands in for agent output is labeled EXAMPLE OUTPUT. Claims were checked against the docs on Nov 23, 2026: Claude Code subagents, permissions and hooks, and Cursor subagents.

Watch the video

The video walks through the same eight chapters with the same real terminal runs (about 10 minutes). Watch it on YouTube.

Intro: the read-only trap

Here is the reviewer. Six lines of settings, and line 4 is the trap:

---
name: code-reviewer
description: Reviews diffs for bugs and security issues. Use proactively after code changes.
tools: Read, Grep, Glob, Bash
model: sonnet
---

You are a senior code reviewer. Run git diff, then review only the changed files.
Report Critical / Warnings / Suggestions with file:line and a concrete fix.

No Edit, no Write. But Bash runs any shell command, so this subagent can sed -i, rm, or write a file with a flag you didn't think of. I checked a folder of reviewer files with a small audit script (more on it in chapter 2):

Our script, not Claude Code: agents-audit.py encodes the rules from Anthropic's subagent docs (checked Nov 23, 2026). Its table is what we compute from those rules, not something Claude Code prints.

$ python3 agents-audit.py .claude/agents
file                 status   edits?  why
code-reviewer.md     loads    YES     via Bash

That's the cold open of this whole guide: "read-only" is a property of the commands the subagent can run, not of the tools line alone. By the end you'll have a reviewer that keeps git diff and npm test and can't change anything else.

1. What a subagent is

You ask Claude to review your changes. The review, the test logs and twenty file reads all land in your main conversation. That's context you'll never use again.

A subagent is a separate Claude with its own context window, its own system prompt and its own tool access. Claude hands it a task, it works on its own, and only its summary comes back to you.

When to skip it: quick, targeted edits, or work with a lot of back-and-forth. A subagent starts from zero, so it adds latency. And for a reusable prompt that runs in your main conversation, use a skill instead.

2. Your first subagent file

A subagent is one Markdown file:

If two have the same name, the project one wins.

The block between the two --- lines is the settings (frontmatter). Only name and description are required. Everything below the second --- becomes the subagent's system prompt: not the Claude Code system prompt, just yours plus a few environment details. Here's the full reviewer, with a prompt that runs the diff, runs the tests once, and reports in a fixed shape:

.claude/agents/code-reviewer.md · download

---
name: code-reviewer
description: Reviews diffs for bugs and security issues. Use proactively after code changes.
tools: Read, Grep, Glob, Bash
model: sonnet
---

You are a senior code reviewer. You never change files.
1. Run git diff to see what changed. Review only the changed files.
2. Run npm test once.
3. Report: Critical (must fix), Warnings (should fix), Suggestions.
   Every item gets file:line and a one-line fix. End with APPROVE or CHANGES NEEDED.

EXAMPLE OUTPUT · the shape of review I want back. Not a real Claude Code session:

Critical     src/cart.js:1  total() subtracts -> use a + b
Warnings     (none)
Suggestions  add a test for total()
CHANGES NEEDED

Claude Code also skips some files, and it's easy to miss. A file with no name is treated as documentation. A file with a name but no description is skipped completely, and the reason only shows up in the debug log (claude --debug). So I wrote a small script that checks a folder against the rules in the docs, and seven sample files to test it on:

Our script, not Claude Code: agents-audit.py encodes the rules from Anthropic's subagent docs (checked Nov 23, 2026). Its table is what we compute from those rules, not something Claude Code prints.

Real run (demo-agent-files/run.txt, Nov 24, 2026):

$ python3 agents-audit.py .claude/agents
file                 status   edits?  why
README.md            doc      -       no name: read as docs
code-reviewer.md     loads    YES     via Bash
no-description.md    SKIPPED  -       no description
no-push.md           loads    no      drops ALL of Bash; Glob, Grep, Read only
reviewer-memory.md   loads    YES     memory: adds Write, Edit
reviewer-strict.md   loads    no      Glob, Grep, Read only
typo.md              loads    YES     unknown key disallowed-tools; inherits all tools

agents-audit.py · our script, encodes the docs' rules · download

#!/usr/bin/env python3
"""agents-audit.py <agents-dir>: our script, NOT Claude Code. It encodes the rules in
code.claude.com/docs/en/sub-agents (checked 2026-11-23): which subagent files load, which
frontmatter fields are ignored, and whether the resulting tool set can change files."""
import os, re, sys
KNOWN = {"name", "description", "tools", "disallowedTools", "model", "permissionMode", "maxTurns",
         "autoCompactWindow", "skills", "mcpServers", "hooks", "memory", "background", "omitClaudeMd",
         "effort", "isolation", "color", "initialPrompt", "experimental"}
def front(path):
    lines = open(path).read().splitlines()
    if not lines or lines[0].strip() != "---": return None
    fm = {}
    for ln in lines[1:]:
        if ln.strip() == "---": break
        m = re.match(r"^([A-Za-z_][\w-]*):\s*(.*)$", ln)
        if m: fm[m.group(1)] = m.group(2).strip()
    return fm
def tool_list(v): return [t.strip() for t in v.strip("[]").split(",") if t.strip()]
args = [a for a in sys.argv[1:] if a != "--compact"]; compact = "--compact" in sys.argv  # --compact: portrait-card width
d = args[0] if args else ".claude/agents"
def out(f, status, edits, why, short):
    if compact: print(f"{f:<19} {edits:<4} {short}")
    else: print(f"{f:<20} {status:<8} {edits:<7} {why}")
print(f"{'file':<19} {'edit':<4} why" if compact else f"{'file':<20} {'status':<8} {'edits?':<7} why")
for f in sorted(os.listdir(d)):
    if not f.endswith(".md"): continue
    fm = front(os.path.join(d, f))
    if fm is None or "name" not in fm:
        out(f, "doc", "-", "no name: read as docs", "docs (no name)"); continue
    if "description" not in fm:
        out(f, "SKIPPED", "-", "no description", "SKIPPED"); continue
    unknown = [k for k in fm if k not in KNOWN]
    tools = set(tool_list(fm["tools"])) if "tools" in fm else {"*"}
    dropped = {re.sub(r"\(.*\)$", "", t) for t in tool_list(fm.get("disallowedTools", ""))}
    tools -= dropped
    why = []
    if unknown: why.append('unknown key ' + ', '.join(unknown))
    if dropped: why.append(f"drops ALL of {', '.join(sorted(dropped))}")
    if "memory" in fm: tools |= {"Read", "Write", "Edit"}; why.append("memory: adds Write, Edit")
    if "*" in tools: edits = "YES"; why.append("inherits all tools")
    else:
        via = [t for t in ("Edit", "Write", "Bash") if t in tools]
        edits = "YES" if via else "no"
        if via and "memory" not in fm: why.append("via " + ", ".join(via))
        if not via: why.append(", ".join(sorted(tools)) + " only")
    short = ("unknown key, inherits all" if unknown and "*" in tools else "memory adds Write+Edit" if "memory" in fm
             else "via Bash" if edits == "YES" and "Bash" in tools else "ALL of Bash dropped" if dropped else "Read/Grep/Glob" if edits == "no" else why[-1])
    out(f, "loads", edits, "; ".join(why), short)

The seven sample files from the table (all in the zip under audit/.claude/agents/):

.claude/agents/README.md · in the zip

# Team subagents
Notes for humans. Not a subagent: no frontmatter.

.claude/agents/code-reviewer.md · in the zip

---
name: code-reviewer
description: Reviews diffs for bugs and security issues. Use proactively after code changes.
tools: Read, Grep, Glob, Bash
model: sonnet
---

You are a senior code reviewer. Run git diff, then review only the changed files.
Report Critical / Warnings / Suggestions with file:line and a concrete fix.

.claude/agents/no-description.md · in the zip

---
name: no-description
tools: Read
---

Review the diff.

.claude/agents/no-push.md · in the zip

---
name: no-push
description: Reviewer that should never push.
tools: Read, Grep, Glob, Bash
disallowedTools: Bash(git push *)
---

Review the diff. Never push.

.claude/agents/reviewer-memory.md · in the zip

---
name: reviewer-memory
description: Reviewer that remembers team review preferences across sessions.
tools: Read, Grep, Glob
memory: project
---

Check your memory for past review notes before you start.

.claude/agents/reviewer-strict.md · in the zip

---
name: reviewer-strict
description: Read-only reviewer with no shell. Use when no commands need to run.
tools: Read, Grep, Glob
model: sonnet
---

Review the files you are given. Never propose running commands.

.claude/agents/typo.md · in the zip

---
name: typo
description: Reviewer meant to have no write tools.
disallowed-tools: Write, Edit
---

Review the diff.

Good news: no restart needed. Claude Code watches both agents folders and picks up a change within a few seconds. In current versions (v2.1.198+), /agents just reminds you to edit these folders; the old wizard is gone.

3. Description + how to call it

"I built a reviewer and Claude never uses it." The description is usually why. Claude reads every subagent's description to decide when to hand work off, so write when to use it, not just what it is:

- description: Code reviewer
+ description: Reviews diffs for bugs and security issues. Use proactively after code changes.

The docs suggest phrases like "use proactively" to encourage delegation. Keep descriptions short, because they sit in context all the time: if your custom descriptions add up to more than 15,000 tokens, Claude Code warns you at startup. Put the detail in the prompt instead; that only loads when the subagent runs.

Three ways to call it yourself:

> Have the code-reviewer subagent look at my changes
> @"code-reviewer (agent)" check the auth diff
$ claude --agent code-reviewer

Plain English usually works; the @ mention guarantees it runs; claude --agent starts the whole session as the reviewer. By default, a custom subagent that Claude starts runs in the background so you can keep working. If it needs permission for something, the prompt shows up in your main session with the subagent's name on it.

4. Tools: allowlist vs denylist

You want a reviewer that can read code and run tests, but never change it. Two fields control that:

Back to the audit table. The docs-style code-reviewer.md can edit, through Bash. reviewer-strict.md, with only Read, Grep and Glob, can't. That's the only truly read-only setup in the folder, and it can't run your tests either. Then the surprises:

To block one command but keep Bash, put a deny rule in your settings under permissions. Those rules apply to subagents too:

.claude/settings.json (permissions.deny) · merge into your settings · download

{
  "permissions": {
    "deny": ["Bash(git push *)"]
  }
}

But it doesn't stop git -C . push. A Bash deny rule matches the command text Claude writes. The permissions docs give exactly this example: a rule for git push * stops git push origin main but doesn't match git -C . push origin main. So for a reviewer, flip it around. Don't list what's banned; list what's allowed.

- Bash(git push *)   stops:        git push origin main
+                     doesn't stop: git -C . push origin main

5. Lock Bash with a hook

Goal: make "read-only" true while keeping git diff and tests. The lock is a PreToolUse hook on the Bash tool in .claude/settings.json. Hooks in your settings files fire inside subagents too, and the hook input tells you which agent is calling, in a field named agent_type.

.claude/settings.json · download

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          { "type": "command", "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/reviewer-bash-guard.sh" }
        ]
      }
    ]
  }
}

.claude/hooks/reviewer-bash-guard.sh · needs jq; chmod +x · download

#!/bin/bash
# PreToolUse guard: the code-reviewer subagent may only run read-only commands.
INPUT=$(cat)
AGENT=$(echo "$INPUT" | jq -r '.agent_type // empty')
CMD=$(echo "$INPUT" | jq -r '.tool_input.command // empty')
[ "$AGENT" = "code-reviewer" ] || exit 0       # not the reviewer: other rules decide
if echo "$CMD" | grep -qE '[;&|<>`]|\$\(|--output'; then
  echo "Blocked: one read-only command, no chaining or output files" >&2; exit 2
fi
if echo "$CMD" | grep -qE '^(git (diff|log|show|status)|npm test)( |$)'; then
  exit 0                                      # allowlist
fi
echo "Blocked: code-reviewer is read-only" >&2
exit 2

The script reads the hook input with jq. If the caller isn't code-reviewer, it exits 0 and your other rules decide. If the command chains, pipes, redirects, substitutes or uses --output, it's blocked. Then the allowlist: git diff, log, show, status and npm test. Everything else exits with code 2, which blocks the call and sends the stderr message back to Claude.

I fed it seven commands, in the same shape Claude Code sends a hook (that's a real run of the hook script, not of Claude Code):

Real run (demo-bash-guard/run.txt, Nov 24, 2026):

$ ./cases.sh
ALLOW   git diff --stat main
ALLOW   npm test
BLOCK   sed -i s/-/+/ src/cart.js   (exit 2)
BLOCK   rm -rf src   (exit 2)
BLOCK   git diff --output=review.txt   (exit 2)
BLOCK   git -C . push origin main   (exit 2)
BLOCK   git diff && git push   (exit 2)
$ ./try.sh - "npm run build"
ALLOW   npm run build
$ ./raw.sh "rm -rf src"
Blocked: code-reviewer is read-only
exit 2

git diff and npm test pass. sed, rm -rf, git diff --output, git -C . push and a chained push are all blocked. And the main session's npm run build (no agent_type) isn't touched.

Why block --output? That's the intro run: git diff --output=review.txt wrote a new file. An allowlist still needs you to know your commands' flags. Two more honest caveats: npm test runs whatever your test script does, so if your tests write files, so can the reviewer; and this is an allowlist, not a sandbox. Keep the list short.

The test harness, so you can run the same checks on your copy (run from the folder that contains .claude/):

cases.sh · download

#!/bin/bash
# cases.sh: the 7 Bash calls a code-reviewer subagent might try, through try.sh
for c in "git diff --stat main" "npm test" "sed -i s/-/+/ src/cart.js" "rm -rf src" \
         "git diff --output=review.txt" "git -C . push origin main" "git diff && git push"; do
  ./try.sh code-reviewer "$c"
done

try.sh · download

#!/bin/bash
# try.sh <agent_type|-> <command>: pipes the guard the JSON shape Claude Code sends a PreToolUse
# hook for a Bash call (tool_name, tool_input.command, agent_type) and prints ALLOW or BLOCK.
agent="$1"; cmd="$2"
json=$(jq -nc --arg a "$agent" --arg c "$cmd" \
  '{hook_event_name:"PreToolUse", tool_name:"Bash", tool_input:{command:$c}} + (if $a == "-" then {} else {agent_type:$a} end)')
echo "$json" | .claude/hooks/reviewer-bash-guard.sh 2>/dev/null; code=$?
if [ $code -eq 0 ]; then echo "ALLOW   $cmd"; else echo "BLOCK   $cmd   (exit $code)"; fi

raw.sh · download

#!/bin/bash
# raw.sh <command>: one reviewer Bash call straight into the guard; shows the stderr Claude would see + exit code
jq -nc --arg c "$1" '{tool_name:"Bash", tool_input:{command:$c}, agent_type:"code-reviewer"}' \
  | .claude/hooks/reviewer-bash-guard.sh; echo "exit $?"

make-repo.sh · download

#!/bin/bash
# make-repo.sh: tiny git repo at /tmp/vk-review with one uncommitted change, to show that
# an "allowed" read-only command (git diff) can still write a file via --output.
rm -rf /tmp/vk-review && mkdir -p /tmp/vk-review/src && cd /tmp/vk-review || exit 1
git init -q && printf 'export const total = (a, b) => a + b;\n' > src/cart.js
git add . && git -c user.email=demo@vibekitlab.com -c user.name=demo commit -qm init
printf 'export const total = (a, b) => a - b;\n' > src/cart.js
echo "repo ready: /tmp/vk-review (1 file changed)"

Settings file or subagent file? You could put this hook in the subagent's own frontmatter instead. It works, with one catch: hooks in a project subagent's file only run after you accept the trust dialog for that folder, and a claude -p run never counts as trusted. Hooks in your settings files run in both. So if the reviewer runs in CI, use settings. (subagents docs, hooks docs.)

6. Models and cost

A reviewer that runs after every change adds up, so pick its model on purpose. By default a subagent runs on the same model as your conversation. The model field changes that:

model: sonnet | opus | haiku | fable
model: <a full model ID>
model: inherit
(omitted)  -> same as your conversation

If more than one thing sets a model, Claude Code takes the first of these:

  1. a model Claude passes when it starts the subagent,
  2. the model: field,
  3. the CLAUDE_CODE_SUBAGENT_MODEL environment variable,
  4. your main conversation's model.

Real run (demo-agent-files/run.txt, Nov 24, 2026):

$ grep -rn "^model:" .claude/agents/
.claude/agents/reviewer-strict.md:5:model: sonnet
.claude/agents/code-reviewer.md:5:model: sonnet

In my folder, two reviewer files set a model, both Sonnet. A cheaper model is fine for a narrow checklist; for security reviews I'd rather pay for a stronger one (that's my opinion, not the docs'). To check which model a running subagent is actually on, run /tasks.

And remember, each subagent spends its own tokens. Five reviewers running in parallel means five context windows. A nested subagent runs on the model of the subagent that started it, unless you set one.

7. Cursor + recap

Cursor has subagents too, and for compatibility it reads .claude/agents/ (alongside .cursor/agents/ and .codex/agents/). If .cursor/agents/ has a file with the same name, Cursor uses that one. Two differences, per Cursor's docs:

.claude/agents/code-reviewer.md (Cursor-compatible) · save as code-reviewer.md · download

---
name: code-reviewer
description: Reviews diffs for bugs and security issues. Use proactively after code changes.
tools: Read, Grep, Glob, Bash
model: inherit
readonly: true
---

You are a senior code reviewer. You never change files.
Run git diff, review only the changed files, and report Critical / Warnings /
Suggestions with file:line and a one-line fix. End with APPROVE or CHANGES NEEDED.

Our script, not Claude Code: agents-audit.py encodes the rules from Anthropic's subagent docs (checked Nov 23, 2026). Its table is what we compute from those rules, not something Claude Code prints.

Real run (demo-cursor-compat/run.txt, Nov 24, 2026):

$ python3 agents-audit.py .claude/agents
file                 status   edits?  why
code-reviewer.md     loads    YES     unknown key readonly; via Bash

For Claude Code, readonly is an unknown key and the file can still edit through Bash. That's why the hook stays. (Not tested in Cursor itself; the Cursor claims come from its docs.)

Recap

  1. A subagent is a Markdown file with a name and a description.
  2. Write the description as when to use it.
  3. The tools line is an allowlist, and Bash means it can write.
  4. Memory adds Write and Edit, and typos are ignored silently.
  5. Lock Bash with an allowlist hook in your settings.
  6. Pick the model on purpose.

All files (download)

Everything above in one zip, laid out in the right folders: claude-code-subagent-reviewer.zip. Or grab single files:

The zip also has the 7 sample agent files for agents-audit.py and a README.

Want more ready-made setups?

Lock the rest of your repo down with the 7 Claude Code guardrails, or trim your instructions with the lean CLAUDE.md guide. If you'd rather have subagents, skills, hooks and 5 CLAUDE.md templates pre-wired, that's what our Claude Code Starter Kit ($39) is for. Using Cursor too? The Cursor Rules Pack ($19) has 18 tested .mdc rules plus an AGENTS.md template.

Get the Starter Kit ($39) Cursor Rules Pack ($19)

Sources

Docs checked Nov 23, 2026. Claude Code changes fast; check the docs if your version behaves differently.

Correction note: our older post How to set up Claude Code subagents called a Read/Grep/Glob/Bash reviewer "read-only plus Bash for git diff". That was wrong, and it now carries a correction pointing here.

FAQ

Is a Claude Code subagent with tools Read, Grep, Glob, Bash read-only?

No. Bash runs any shell command, so the subagent can still write or delete files. Anthropic's docs say their code-reviewer example can still change files through Bash, and even git diff --output=review.txt writes a file. For a truly read-only reviewer use tools: Read, Grep, Glob, or keep Bash behind a PreToolUse hook that only allows read-only commands.

Where do Claude Code subagent files go?

Project subagents go in .claude/agents/ (commit them for your team); personal ones go in ~/.claude/agents/ and work in every project. Only name and description are required. If two files share a name, the project one wins.

Why isn't my Claude Code subagent loading?

A file with no name is treated as documentation, and a file with a name but no description is skipped; the reason shows up in claude --debug. Unknown frontmatter fields, such as disallowed-tools with a dash, are ignored without an error.

How do I stop a subagent from running git push but keep Bash?

Don't put Bash(git push *) in disallowedTools: that removes the whole Bash tool. Add a permissions.deny rule Bash(git push *) in settings instead, which also applies to subagents. It matches command text, so it doesn't stop git -C . push origin main; for a reviewer, allowlist commands with a PreToolUse hook.

Does a .claude/agents subagent work in Cursor?

Cursor reads .claude/agents/ for compatibility, and a file in .cursor/agents/ with the same name wins. Use model: inherit and add readonly: true for Cursor. Claude Code ignores readonly, so keep the Bash hook for Claude Code.

The VibeKit Dispatch

One tested Claude Code / Cursor workflow every week. No hype, unsubscribe anytime.