Step-by-step: build a subagent (Claude Code & Cursor)
Each step has a Goal, what to Do, and how to Verify it. Track your progress with the agents checklist.
Running example: a code-reviewer subagent that reviews the current diff against team standards and returns prioritised findings, without editing anything.
To skip copying a template, use the Subagent builder. It asks for the fields this guide requires and writes a <name>.md you can copy or download. Save it in .claude/agents/ (both hosts) or .cursor/agents/ (Cursor only), then come back to these steps for location, testing, and shipping.
Step 1: Define the job and the hand-off
Section titled “Step 1: Define the job and the hand-off”- Goal: A clear contract covering what the subagent receives, does, and returns.
- Do:
- Write the job in one sentence: “Review the current uncommitted diff for bugs, security issues, and standards violations; return findings grouped Critical / Warning / Suggestion with file:line references.”
- Define the input: what the parent must pass (the diff scope, the standards file, the focus).
- Define the output: its exact shape and maximum length. The parent pays for every token that comes back.
- Define done: when the subagent stops.
- Collect 3–5 real tasks you’d delegate to it.
- Verify: Someone else could do the job from your contract alone.
Step 2: Confirm a subagent is right
Section titled “Step 2: Confirm a subagent is right”- Goal: Delegate only where isolation pays off.
- Do: Use a subagent if at least one of these is true:
- It reads a lot but returns a little (search, review, research).
- It can run in parallel with other work.
- It needs different permissions (read-only) or a different model.
- It benefits from an independent perspective (verification).
- Verify: You can name the specific isolation benefit. If you can’t, and it’s just a procedure, use a skill instead.
Step 3: Choose permissions and model
Section titled “Step 3: Choose permissions and model”-
Goal: Least privilege and the right cost.
-
Do:
Decision Claude Code Cursor Read-only tools: Read, Grep, Glob(an allowlist: the subagent has only these), ordisallowedTools: Edit, Write. If you addBash, limit what it does in the prompt, and enforce that with hooks if it mattersreadonly: trueCan edit Include Edit, Writeintoolsreadonly: false(the default)MCP tools Include them by name in tools, omittoolsto inherit all, or declare the subagent’s own servers withmcpServersInherited automatically Model model: sonnet/opus/haiku/fable/ a full model ID /inheritmodel: inheritor a model IDBackground (non-blocking) background: true(Claude Code runs subagents in the background by default)is_background: true -
Rules of thumb: reviewers, researchers, and explorers are read-only. Use a fast, cheap model for search, and a strong model for review or architecture.
-
Verify: It can’t do anything its job doesn’t need.
Step 4: Write the description (the routing rule)
Section titled “Step 4: Write the description (the routing rule)”-
Goal: The parent delegates at the right moments.
-
Do:
description: >-Expert code reviewer. Use proactively immediately after writing ormodifying code, or when the user asks for a review of a diff, PR, or branch.Reviews for correctness, security, and team standards; read-only.- Say when, and include “use proactively” if you want automatic delegation.
- Say what it won’t do if there’s a sibling agent it could be confused with.
-
Verify: Read the descriptions of all your subagents side by side. None of them overlap.
Step 5: Write the system prompt
Section titled “Step 5: Write the system prompt”- Goal: A focused, self-sufficient brief. Remember that the subagent can’t see the parent’s chat.
- Do: Use this structure (see the template):
- Role: one line.
- When invoked: the first actions to take (for example “run
git diff, read changed files”). - Process / checklist: what to check, in priority order.
- Constraints: what never to do.
- Output format: an exact template with a length limit.
- Stop condition: when to return.
- Verify: The prompt makes sense to someone who has no idea what the parent was doing.
Step 6: Create the file
Section titled “Step 6: Create the file”- Goal: The host detects the subagent.
- Do:
- Choose a location from the platform matrix:
- Both hosts, this repo:
.claude/agents/code-reviewer.md - Cursor only:
.cursor/agents/code-reviewer.md - Just you:
~/.claude/agents/or~/.cursor/agents/
- Both hosts, this repo:
- Start from
code-reviewer.md(cross-platform) orcursor-verifier.md(Cursor-specific fields). Download the template from the templates page, or copy it from the template page (each file has a copy button). Or fill in the Subagent builder and download a.mdfile that already follows this guide. - Alternatively, ask Claude Code to create the subagent for you, or use
/create-subagentin Cursor (at the time of writing; check Cursor’s docs). Then edit the file. In Claude Code,/agentsno longer creates or lists subagents.
- Choose a location from the platform matrix:
- Verify: In Claude Code, type
@and check that@agent-code-reviewerappears in the typeahead. In Cursor, check that it’s listed in the Subagents section.
Step 7: Test delegation
Section titled “Step 7: Test delegation”- Goal: It’s used when it should be, and not otherwise.
- Do:
- Explicit:
@agent-code-reviewer review my changesin Claude Code, or/code-reviewerin Cursor. Confirm it runs. A plain-language request (“use the code-reviewer subagent”) doesn’t guarantee delegation in Claude Code, so use the@agent-form for this test. - Automatic: make a code change, then ask “is this ready to commit?” Does the parent delegate?
- Negative: ask an unrelated question. It should not delegate.
- Explicit:
- Verify: The results are recorded in your eval sheet (template).
Step 8: Test behaviour and output
Section titled “Step 8: Test behaviour and output”- Goal: Correct, well-shaped results.
- Do:
- Seed a diff with known issues (a hard-coded secret, an off-by-one, and a missing null check).
- Delegate a review. Did it find each one? Were there false positives?
- Check the output against your template: length, grouping, and file:line references.
- Check that the constraints held. For a read-only agent, confirm no files changed (
git status).
- Verify: It catches the seeded issues, the format is exact, and there were no forbidden actions.
Step 9: Tune
Section titled “Step 9: Tune”-
Goal: Fix observed failures, not imagined ones.
-
Do:
Symptom Fix Never delegated automatically Add “use proactively” plus concrete trigger situations to the description Delegated too often Narrow the description; say when not to use it Returns an essay Tighten the output template; add a hard length limit Misses context Tell it which files to read first, or make the parent pass more in the task Does forbidden things Restrict tools/disallowedTools/readonly; add a hook for hard enforcementSlow or expensive Use a cheaper model; narrow the scope -
Verify: Re-run Steps 7–8. All the checks pass.
Step 10: Ship
Section titled “Step 10: Ship”- Goal: The team uses it.
- Do: Commit it to
.claude/agents/(so both hosts pick it up) or package it in a plugin. Document it in the README with example invocations, and log it inDECISIONS.md. - Verify: A colleague’s session delegates to it without being told how.
Next: Build an SDK agent · Design patterns · Checklist