Subagents

Claude Code · Advanced · 14 min

Subagents keep noisy research and verification out of your main context and let you enforce narrower permissions.

A subagent is a helper Claude starts for a sub-task. It runs in its own context window with its own system prompt, tool list and permissions. When it finishes, only its summary returns to your conversation. That makes subagents the main tool for keeping the main context clean: a search across 200 files happens inside the subagent, and you get back five lines.

Claude Code has built-in subagents, including Explore (fast, read-only codebase search), Plan (used in plan mode to research) and a general-purpose one. Claude delegates to them on its own, and you can ask directly: "use subagents to investigate how token refresh works and whether we already have OAuth helpers".

You can define your own as markdown files with frontmatter:

  • .claude/agents/<name>.md for the project, ~/.claude/agents/<name>.md for you everywhere,
  • required fields: name and description (when Claude should delegate to it; "use proactively" encourages automatic use),
  • optional: tools (restrict to, say, read-only tools), model (haiku for cheap tasks, opus for deep review, or inherit), permissionMode, maxTurns and more.

The body is the subagent's system prompt. Invoke one by description match, by name ("use the security-reviewer subagent"), or by @-mentioning it. claude --agent <name> runs a whole session as that agent.

Good uses: codebase research, running and summarizing test suites, reviewing a diff with fresh eyes (a reviewer that did not write the code is less biased), and domain checks like security review. Remember each subagent's own work still uses tokens, and subagents cannot see your conversation unless you pass the details in the request.

Example

---
name: security-reviewer
description: Reviews diffs for security problems. Use proactively after changes to auth, payments or input handling.
tools: Read, Grep, Glob, Bash
model: opus
---
You are a senior security engineer. Review the changes for:
- injection (SQL, command, XSS) and unsafe deserialization
- authentication and authorization flaws
- secrets or credentials in code or logs
Report each finding with file:line, severity and a concrete fix.
Report only real risks, not style.

# saved as .claude/agents/security-reviewer.md
# use: "use the security-reviewer subagent on the current diff"

Remember

  • Subagents run in their own context and return only a summary
  • Define them in .claude/agents/*.md with name, description, tools and model
  • Use them for research, test runs and unbiased review

Watch out for

  • Assuming a subagent knows what you discussed earlier; it starts fresh, so include the needed details in the delegation.

Check yourself

  1. Why delegate a broad codebase search to a subagent instead of doing it in the main session?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: code.claude.com/docs/en/sub-agents