Build a subagent from scratch
When slash commands aren't enough — build a focused subagent for extended, delegated work. This tutorial walks through building a <code>docs-agent</code> that reviews documentation drift.
Prerequisites
- Familiar with slash commands (see first tutorial)
- Claude Code with Opus access (for the subagent model recommendation)
- A project with some existing documentation to review
What you'll learn
- When to build a subagent vs a slash command
- How to write a focused system prompt
- How to scope tools appropriately
- How to pick the right model
- How subagent invocation actually works
- How to iterate on subagent behavior
Slash commands are for one-shot invocations — you type /test-unit and get tests generated. Subagents are for extended, focused work — you delegate a job (like "review our docs for drift") and the subagent works on it for several minutes, potentially making dozens of tool calls, producing a substantial output.
The distinction matters because subagents have their own system prompt, tool allowlist, and model. They're specialized workers you build to be really good at one type of job. This tutorial builds docs-agent — a subagent that reviews documentation for drift (code claims that no longer match reality). By the end, you'll have a working subagent and understand how to build more.
Not every task needs a subagent. Ask yourself:
If all four are yes, build a subagent. If most are no, a slash command is simpler and probably better.
- Does the task involve extended work? (More than 3-4 tool calls, potentially many minutes)
- Does it need focused tooling? (A specific subset of tools, not the general set)
- Would a specialized system prompt help? (Beyond a one-shot instruction — the task benefits from an agent that "knows its job")
- Will you invoke this pattern repeatedly? (Not just once)
Subagents live in .claude/agents/. Like commands, they're Markdown files with frontmatter. Create docs-agent.md:
---
description: Review documentation for drift (claims that no longer match code)
model: claude-opus-4-6
allowed-tools: Read, Grep, Glob, Bash(git log:*), Bash(git blame:*)
---
# Documentation Agent
You are a documentation reviewer. Your job is to find places where the docs claim things that no longer match the code.
## Your process
1. Read the docs (README.md, docs/*.md, inline JSDoc comments)
2. For claims that reference specific code behavior, verify against the actual code
3. Report drift — places where docs and code disagree
4. Do NOT fix — just report
## What to look for
- API signatures in docs that don't match current function signatures
- Configuration options mentioned in docs that don't exist in the code
- Behavior descriptions that contradict what the code does
- Examples that no longer work
- Version-specific info that's outdated (mentions of old dependencies, etc.)
## What to skip
- Style/formatting concerns
- Missing docs (not drift — that's a coverage issue)
- Docs for third-party integrations you can't verify
## Output format
- Group findings by severity: BROKEN (docs describe non-existent behavior), STALE (docs reference old API/version), MISLEADING (docs are technically correct but confusing)
- For each finding: quote the docs, quote the code, one-sentence explanation
- End with a summary count
Note the differences from a slash command: (1) model defaults to opus because agents doing extended work benefit from deeper reasoning, (2) allowed-tools is scoped to reads (agent should never modify anything), (3) system prompt is much longer — it defines the agent's job, process, and output.
The system prompt is the most important part. It defines who the agent is, what it does, and how it does it. A weak system prompt = a wandering agent that doesn't stay focused.
The template that works for most agents:
# <Agent name>
<One-sentence job description — "You are a X who does Y">
## Your process
<Numbered steps the agent follows — usually 3-6 steps>
## What to look for / do
<Specific things in scope>
## What to skip / avoid
<Explicit non-goals — critical for keeping the agent focused>
## Output format
<How to structure the response — makes results consistent across runs>
## When to stop
<Explicit stop condition — otherwise agents can go forever>The "What to skip" section is underrated. Without it, agents drift into adjacent concerns ("while I'm here, let me also check formatting..."). With it, agents stay focused on the actual job.
A good subagent does one thing well. A bad subagent tries to do everything and does none of them well. If you catch yourself writing "and also X, and if Y then Z..." — you're building too many agents in one. Split.
Subagents especially need scoped tools. A docs-review agent should only read; a refactor agent needs to write; an incident-investigator needs specific MCP tools. Getting this right prevents accidents.
For docs-agent, the tool list should be:
allowed-tools: Read, Grep, Glob, Bash(git log:*), Bash(git blame:*)Read/Grep/Glob for reading files. git log and git blame for understanding when docs were written (older docs are more likely to have drifted). No Write, no Edit, no arbitrary bash — the agent physically cannot modify anything.
For subagents doing extended work with hypothesis testing (like docs-agent reasoning about code vs docs), Opus is worth the cost. For simpler agents that follow a mechanical checklist, Sonnet is fine and cheaper.
Rule of thumb: if the agent needs to reason about "is this code doing what the docs claim?" (which requires understanding both), use Opus. If the agent is doing pattern matching or straightforward transformations, Sonnet is enough.
For docs-agent, we're using Opus because judging "is this doc claim still accurate?" is a real reasoning task.
Subagents are invoked from a Claude Code session. Type @docs-agent (or use the agent switcher UI) followed by any additional context you want to provide.
# From Claude Code:
@docs-agent Review our docs for drift✓ Docs Agent starting...
✓ Reading README.md, docs/*.md, and inline JSDoc in src/
✓ Cross-referencing 42 documented functions against actual signatures
✓ Checking 18 configuration options against config schema
✓ Checking 6 example code blocks in README
Findings:
BROKEN (docs describe non-existent behavior):
- README:L47 claims the CLI supports --json flag; not in current CLI code
- docs/api.md:L102 describes .updateAll() method; not in service class
STALE (docs reference old API/version):
- README:L15 mentions Node.js 16+ requirement; package.json requires 20+
- docs/deployment.md:L23 references AWS SDK v2; codebase uses v3
MISLEADING (technically correct but confusing):
- docs/getting-started.md:L88 example uses `port: 3000`; server actually
defaults to $PORT env var (3000 fallback), which the example doesn't show
Summary: 5 drift findings (2 broken, 2 stale, 1 misleading)
The output is actionable — each finding tells you exactly what's wrong and where. You can go fix them one by one, or triage which matter now.
Try invoking @docs-agent in a real project of yours. Compare the output to what you expected. Note where it caught real drift, and where it produced noise or missed something obvious.
This is data for the next step — iteration.
Your first version probably has some rough edges. Common iterations for docs-agent:
Each iteration takes 5-10 minutes: notice the issue, update the system prompt, re-run against the same input, verify the fix. Two or three rounds usually get you to a version that works well.
- Too many false positives (things reported as drift that aren't): add "before reporting, verify the doc claim maps to the code — sometimes docs use aliases or higher-level abstractions"
- Missing obvious drift: add specific examples to look for ("if a function has been renamed but docs still use the old name — that's drift")
- Output too long: add "prioritize BROKEN findings; STALE and MISLEADING can be summarized"
- Agent goes forever: add explicit stop condition ("stop after reviewing all files in docs/ once; do not deep-dive")
- Agent modifies files despite tool allowlist: shouldn't happen with proper allowlist; if it does, tighten the list further
Commit the agent file to git. When teammates pull, they get @docs-agent available. No install step. Just like slash commands, subagents travel with the repo via the .claude/ folder.
Recap
You built a subagent. The steps generalize: pick a focused job, write a system prompt that defines process + output + non-goals, scope tools tightly, choose model based on reasoning depth needed, iterate 2-3 times.
Subagents are the pattern for delegated work. The 12 subagents in cctk's catalog all follow this template. If you find a pattern where you're repeatedly firing off Claude for a specific extended job, that's a subagent candidate.
The next tutorial covers lifecycle hooks — the automation layer that runs on every tool use, session start, and other events.
Common gotchas
- Agent completes too quickly with a shallow answer: The system prompt probably lacks specificity about the process. Add numbered steps for what the agent should actually DO (read X, then Y, then Z), not just what to produce.
- Agent modifies files unexpectedly: Check
allowed-tools— you probably haveWriteorEditin there. Remove them if the agent shouldn't modify anything. - Agent uses too many tokens: Expected for Opus + long sessions. If cost is an issue, try Sonnet — for many agent tasks it's "good enough" at 5x lower cost. Reserve Opus for genuinely hard reasoning.
- Agent doesn't focus — drifts into other concerns: "What to skip" section in the system prompt is your friend. Be explicit about non-goals. "Do NOT check formatting" prevents formatting drift.
- Agent gives generic advice instead of specific findings: The output format section needs to force specificity. Include "quote the exact text" or "reference the specific file:line" so the agent has to be concrete, not vague.
Get cctk to skip the boilerplate
Everything in this tutorial is already scaffolded in cctk — commands, subagents, hooks, MCP configs. Install and customize instead of starting from scratch.