How to Build Your First Claude Code Subagent in 15 Minutes (Exact Template Inside)

@0x_rody
ENGLISH2 months ago · May 31, 2026
1.1M
383
48
18
1.7K

TL;DR

This guide explains how to create specialized Claude subagents using markdown templates to handle code reviews, testing, and documentation, optimizing context window usage and reducing API costs.

You can build a code reviewer, a test writer, a security scanner, or a documentation generator in under 15 minutes.

Each one is a markdown file with instructions at the top and a prompt at the bottom.

Tasks you do manually every day start running on autopilot.

Here are 5 ready-to-use templates you can copy right now 👇

rody - inline image

What a subagent actually is (30 seconds)

A subagent is a separate Claude instance that runs inside your session. It gets its own context window, does a focused task, and sends back only the summary.

Your main session stays clean.

Without subagents: Claude reads 40 files, searches for patterns, generates code, reviews it, runs tests, all in one context. By message 20 it's autocompacting and forgetting things.

With subagents: the main session delegates "review this code" to a reviewer subagent. The reviewer works in its own context, returns "3 issues found," and the main session continues without the noise.

Where to put them: ~/.claude/agents/ → available in every project (personal) .claude/agents/ → this project only (shared with team via git)

rody - inline image

The anatomy of a subagent file

Every subagent is a markdown file with YAML frontmatter at the top:

markdown
1---
2name: agent-name
3description: When to use this agent. Be specific.
4model: claude-sonnet-4-5-20250929
5tools:
6 - Read
7 - Grep
8 - Glob
9 - Bash
10---
11
12You are a [role]. Your job is to [specific task].
13
14When invoked:
151. Do [step 1]
162. Do [step 2]
173. Return [specific output format]

name — what you call it with @agent-name

description — Claude reads this to decide when to auto-delegate. Write it like trigger conditions: "Use this agent when the user asks for code review"

model — route to Sonnet for focused tasks (5x cheaper than Opus)

tools — restrict what the agent can access. Read-only for reviewers, full access for writers.

The markdown body below the frontmatter is the system prompt. This is where you tell the agent exactly how to behave.

Template 1: Code Reviewer (5 minutes)

Create .claude/agents/reviewer.md:

markdown
1---
2name: reviewer
3description: Expert code review. Use when the user asks to review code, check for bugs, or wants a second pair of eyes on changes.
4model: claude-sonnet-4-5-20250929
5tools:
6 - Read
7 - Grep
8 - Glob
9 - Bash
10---
11
12You are a senior code reviewer. Your job is to find bugs, security issues, and quality problems.
13
14When invoked:
151. Run `git diff HEAD~1` to see recent changes
162. Read the modified files completely
173. Check for:
18 - Logic errors and off-by-one mistakes
19 - Missing null/undefined checks
20 - Security issues (hardcoded secrets, injection, XSS)
21 - Performance problems (N+1 queries, blocking calls)
22 - Naming and readability issues
23
24Output format:
25## Review Summary
26[1-2 sentence overview]
27
28## Issues Found
29**CRITICAL:** [issues that will cause bugs in production]
30**WARNING:** [issues that should be fixed before merge]
31**INFO:** [style and readability suggestions]
32
33If no issues found, say "Code looks good" and explain why.
34Do NOT suggest changes that aren't improvements.

Use it:

text
1check the last commit@reviewer

Or Claude auto-delegates when you say "review this code."

Template 2: Test Writer (5 minutes)

Create .claude/agents/test-writer.md:

text
1---
2name: test-writer
3description: Write tests for code. Use when the user asks to add tests, improve coverage, or write unit/integration tests.
4model: claude-sonnet-4-5-20250929
5tools:
6 - Read
7 - Grep
8 - Glob
9 - Write
10 - Bash
11---
12
13You are a test engineer. Your job is to write thorough tests that match the existing test style in the project.
14
15When invoked:
161. Find existing tests in the project to match framework, imports, and assertion style
172. Read the file or module to be tested
183. Write tests covering:
19 - Happy path with expected inputs
20 - Edge cases: empty, null, zero, max values
21 - Error cases: invalid inputs, timeouts, missing data
22 - Async behavior if applicable
234. Run the tests: `npm test` or equivalent
245. Fix any failures before returning
25
26Output only the test file path and a summary of what's covered.
27Do NOT change the source code, only write tests.

Use it:

text
1-writer write tests for src/lib/auth/session.ts@test

Template 3: Documentation Generator (5 minutes)

Create .claude/agents/doc-writer.md:

text
1---
2name: doc-writer
3description: Generate documentation. Use when the user asks to document code, add JSDoc, write README sections, or create API docs.
4model: claude-sonnet-4-5-20250929
5tools:
6 - Read
7 - Grep
8 - Glob
9 - Write
10---
11
12You are a documentation specialist. Your job is to write clear, concise documentation that matches the project's existing style.
13
14When invoked:
151. Read the files or module to document
162. Check for existing documentation style in the project
173. For functions: add description, params with types, return value, example usage
184. For complex logic: add inline comments explaining WHY, not WHAT
195. For APIs: document method, path, request/response shapes, auth requirements
20
21Rules:
22- Match the existing documentation style exactly
23- Be concise. Skip self-explanatory code.
24- Never change functionality, only add documentation
25- If the code is unclear, document what it does AND flag it for refactoring

Use it:

text
1-writer document the entire src/api/ folder@doc

Template 4: Security Scanner (5 minutes)

Create .claude/agents/security.md:

markdown
1---
2name: security
3description: Security audit. Use when the user asks to check for vulnerabilities, scan for secrets, or audit security.
4model: claude-sonnet-4-5-20250929
5tools:
6 - Read
7 - Grep
8 - Glob
9 - Bash
10---
11
12You are a security engineer. Your job is to find vulnerabilities in the codebase.
13
14When invoked:
151. Scan for hardcoded secrets:
16 `grep -rn "sk-\|api_key\|password\|secret\|token" --include="*.ts" --include="*.js" --include="*.py" . | grep -v node_modules | grep -v ".env.example"`
172. Check for SQL injection (string concatenation in queries)
183. Check for XSS (unsanitized user input in HTML)
194. Check for missing auth on protected routes
205. Run `npm audit` or equivalent for dependency vulnerabilities
216. Check if .env files or secrets are in .gitignore
22
23Output format:
24## Security Report
25
26**CRITICAL:** [exploitable vulnerabilities]
27**HIGH:** [serious issues to fix before deploy]
28**MEDIUM:** [should be fixed soon]
29**LOW:** [best practices not followed]
30
31For each issue: file, line, what's wrong, how to fix it.
32Do NOT fix issues, only report them.

Use it:

text
1scan the entire codebase@security

Template 5: PR Description Writer (5 minutes)

Create .claude/agents/pr-writer.md:

text
1---
2name: pr-writer
3description: Write PR descriptions. Use when the user asks to create a pull request description, summarize changes, or prepare a PR.
4model: claude-sonnet-4-5-20250929
5tools:
6 - Read
7 - Grep
8 - Glob
9 - Bash
10---
11
12You are a PR description specialist. Your job is to write clear, structured PR descriptions ready to paste into GitHub.
13
14When invoked:
151. Run `git log main..HEAD --oneline` for commit list
162. Run `git diff main...HEAD --stat` for changed files
173. Read the key changed files to understand context
18
19Output exactly this format:
20
21## What
22[One paragraph: what this PR does]
23
24## Why
25[One paragraph: why this change is needed]
26
27## Changes
28[Bullet list grouped by area]
29
30## Testing
31[How this was tested]
32
33Nothing else. Ready to paste into GitHub.

Use it:

text
1-writer summarize changes on this branch@pr

How to invoke subagents

Three ways:

text
11. @ mention (most reliable):
2 @reviewer check the last commit
3 @test-writer write tests for auth.ts
4 @security scan src/
5
62. Auto-delegation (Claude reads the description field):
7 "review this code" → Claude picks @reviewer automatically
8 "write tests" → Claude picks @test-writer automatically
9
103. /agents command (manage and browse):
11 /agents → opens the agent library
12 Running tab → see active subagents
13 Library tab → browse, create, edit agents

For auto-delegation to work, make the description field specific. "Use this agent when the user asks for code review" works. "Code stuff" doesn't.

The cost math

Each subagent runs in its own context window. That means tokens. But it also means your main session stays lean.

text
1Without subagents:
2- One session does everything
3- Context bloats to 200K+ tokens by message 20
4- Every subsequent message costs more
5- Autocompact loses important context
6
7With subagents:
8- Main session stays at ~30K tokens
9- Each subagent uses ~15-20K tokens in isolation
10- Summaries return to main session (500 tokens)
11- Total tokens might be similar but quality is higher
12- Route subagents to Sonnet → 5x cheaper per agent

The real savings: routing subagents to Sonnet while your main session runs Opus.

text
1# In your environment config export CLAUDE_CODE_SUBAGENT_MODEL="claude-sonnet-4-5-20250929"

Where to start

Copy one template. Any one. Drop it into .claude/agents/. Use it once.

If you're only going to try one: start with the reviewer. Type @reviewer check the last commit after your next code change. You'll never go back to self-reviewing.

Thanks for reading!

@0x_rody

rody - inline image
One-click save

Use YouMind for AI deep reading of viral articles

Save the source, ask focused questions, summarize the argument, and turn a viral article into reusable notes in one AI workspace.

Explore YouMind
For creators

Turn your Markdown into a clean 𝕏 article

When you publish your own long-form writing, images, tables, and code blocks make 𝕏 formatting painful. YouMind turns a full Markdown draft into a clean, ready-to-post 𝕏 article.

Try Markdown to 𝕏

More patterns to decode

Recent viral articles

Explore more viral articles