DEV Community

Jack Napier
Jack Napier

Posted on Edited on

Claude Code Subagents: 4 Practical Examples You Can Copy Today

If you use Claude Code for more than quick edits, you eventually hit the same wall: the main conversation fills up with file dumps, test logs and half-finished investigations, and the quality of answers drops.

Subagents fix a big part of that. A subagent is a separate Claude instance with its own context window, its own system prompt and its own tool permissions. The main session hands it a task, the subagent does the noisy work, and only a short result comes back.

This post shows how subagents are defined and four examples you can copy into a project today.

Where subagents live

A subagent is a Markdown file with YAML frontmatter:

  • .claude/agents/<name>.md — project subagents (commit them so your team gets them)
  • ~/.claude/agents/<name>.md — personal subagents, available in every project

If both exist with the same name, the project one wins. You can also create and edit them interactively with the /agents command inside Claude Code.

The basic shape:

---
name: my-agent
description: "When Claude should use this agent. Be specific."
tools: Read, Grep, Glob
model: sonnet
---

The system prompt for the agent goes here.
Enter fullscreen mode Exit fullscreen mode

Three fields do most of the work:

  • description is how Claude decides when to delegate. Vague descriptions mean the agent never gets picked. Phrases like "Use proactively after code changes" help.
  • tools restricts what the agent can do. Leave it out and the agent inherits all tools from the main session, including Bash and edits.
  • model lets you run cheap tasks on a smaller model (haiku) and keep sonnet/opus for harder ones. inherit uses whatever the main session uses.

You can always call an agent explicitly: "Use the code-reviewer subagent to review my last commit."

Example 1: a read-only code reviewer

The most useful subagent for most people. Note that it has no Edit or Write tool, so it can only report, not "fix" things behind your back.

---
name: code-reviewer
description: Reviews recent code changes for bugs, security problems and readability. Use proactively after writing or modifying code.
tools: Read, Grep, Glob, Bash
model: sonnet
---

You are a senior code reviewer.

1. Run `git diff HEAD` to see what changed. If nothing is staged or modified, review the last commit with `git show`.
2. Read the surrounding code for every changed function, not just the diff lines.
3. Report findings grouped as: Critical, Warnings, Suggestions.
4. For each finding give file:line, what is wrong, and a concrete fix.

Do not rewrite files. Do not comment on formatting a linter would catch.
If you find nothing important, say so in one line.
Enter fullscreen mode Exit fullscreen mode

Bash is included only so it can run git diff. If you want it stricter, a PreToolUse hook can block anything that isn't a read-only git command.

Example 2: a test runner that keeps logs out of your context

Test output is the classic context killer: hundreds of lines of passing tests to find one failure. Let a subagent read them.

---
name: test-runner
description: Runs the project's test suite and reports only failures with their root cause. Use after code changes or when the user asks to run tests.
tools: Read, Grep, Glob, Bash
model: haiku
---

You run tests and summarize results.

1. Detect the test command (package.json scripts, pytest.ini/pyproject.toml, Makefile, go.mod).
2. Run the full suite once.
3. If everything passes, reply with one line: the command and the pass count.
4. For each failure, report: test name, the assertion or error, and the most likely cause after reading the code under test.

Never modify source or test files. Keep the final answer under 25 lines.
Enter fullscreen mode Exit fullscreen mode

haiku is usually enough here: the job is running a command and reading output, not deep reasoning.

Example 3: a debugger with a strict process

When a bug is subtle, the main session tends to guess and patch. A dedicated debugger with a fixed process guesses less.

---
name: debugger
description: Investigates errors, failing tests and unexpected behavior to find the root cause. Use when a bug is not obvious from the error message.
tools: Read, Grep, Glob, Bash, Edit
model: sonnet
---

You are a debugging specialist.

1. Reproduce the problem first. Write down the exact command and the observed output.
2. Form at most three hypotheses and rank them.
3. Test each hypothesis with the smallest possible experiment (a log line, a one-off script, a focused test).
4. Only after the cause is confirmed, apply the minimal fix and rerun the reproduction.

Report: root cause, evidence, the fix, and how you verified it.
If you could not confirm the cause, say so instead of guessing.
Enter fullscreen mode Exit fullscreen mode

This one gets Edit because applying the fix is part of the job. Decide per agent; don't hand edit rights to every agent by default.

Example 4: a cheap codebase explorer

"Where is X handled?" questions often need reading 20 files to produce a 5-line answer. That's exactly the shape subagents are good at.

---
name: explorer
description: Finds where things are implemented in this codebase and explains how they connect. Use for "where is", "how does", and "what calls" questions.
tools: Read, Grep, Glob
model: haiku
---

You answer questions about this codebase by searching it.

- Start with Grep/Glob, then read only the relevant parts of files.
- Answer with file paths and line numbers.
- Finish with a 3-5 line explanation of how the pieces connect.

Never guess a path. If you did not find it, say what you searched for.
Enter fullscreen mode Exit fullscreen mode

Recent Claude Code versions also ship built-in agents for exploration and planning, so check what you already have before duplicating it. A project-specific explorer is still worth it when you want to point it at your own conventions ("API handlers live in src/routes, never in src/lib").

Things that bite people

  • Subagents can't spawn other subagents. Orchestration happens in the main session. Design agents as leaves, not managers.
  • They start with a fresh context. The agent doesn't see your conversation, so the delegation message has to include what matters (file names, the error, what you already tried). Write system prompts that tell the agent to find context itself.
  • Long reports defeat the purpose. If an agent returns 300 lines, you've moved the noise, not removed it. Put an output limit in every prompt.
  • Descriptions are routing rules. If an agent never triggers, rewrite its description before touching anything else.
  • Tool lists are your safety boundary. Review agents should not have Edit/Write. Combine that with hooks for anything destructive.

Try it

  1. mkdir -p .claude/agents
  2. Save one of the files above as .claude/agents/code-reviewer.md
  3. Make a small change and ask: "Use the code-reviewer subagent on this change."
  4. Run /agents to see it listed and tweak it.

Start with one reviewer and one test runner. Add more only when you notice the same kind of task polluting your main session.


Free resources: I keep a free collection of Claude Code skills and hooks here: github.com/MrNapier89/claude-code-skills-hooks.

Disclosure: if you want a ready-made set, I sell a pack of 12 subagents (planner, code reviewer, debugger, QA and more) with install notes: Claude Code Agents Pack ($24). It's my product; everything in this post works without it.

Top comments (0)