Claude Code Subagents: Setup and Workflow Guide

A Claude Code subagent is a specialized assistant defined as a Markdown file in the .claude/agents/ directory. It runs in its own context window with its own system prompt and a scoped set of tools, so the main session can delegate a focused task to it and receive only a summary back. This isolation keeps bulky intermediate work out of the primary conversation.
Run the /agents command, which guides you through the fields and writes the file for you, or create the file by hand at .claude/agents/<name>.md. The YAML frontmatter sets the name, description, allowed tools, and optional model, while the body of the file becomes the subagent's system prompt. Project-level files live in the repo; user-level files live under your home directory.
Use a subagent when the work produces far more tokens than the conclusion is worth keeping, such as reading many files to answer one question, running an adversarial review on a diff, or exploring several research threads in parallel. Delegating keeps the raw work out of your main window and returns only the distilled result.
Both, depending on how you use them. They reduce pressure on the main context by keeping large file reads and tool output isolated, but a multi-agent run re-reads shared context and can use several times more tokens overall than a single chat. Route high-volume, low-stakes subagents to a cheaper model and reserve parallel agents for genuinely breadth-first work.
Only the tools you allow. Each subagent declares an allowlist in its frontmatter, so a reviewer can be limited to read and grep while being denied write and shell access. This scoping is a security control: a narrowly permissioned subagent cannot modify files or run commands even if its prompt is manipulated.

Key Takeaway
Claude Code subagents are specialized assistants defined as Markdown files in .claude/agents/, each with its own context window, system prompt, and scoped tools. They let a main session delegate focused work such as code review or research to an isolated context that returns only a summary, keeping the primary conversation clean and cost under control.
A single Claude Code session does everything in one context window, and that window is its scarcest resource. Read twenty files to answer one question and all twenty stay in context, crowding out the task you actually care about. Subagents are the built-in fix: a way to hand a slice of work to a separate assistant that thinks in its own window and hands back only the answer.
This guide covers what a subagent really is, how to create one, when delegation is worth it, and how subagents change what a session costs. The examples use Claude Code, but the same delegation pattern applies to any agentic coding tool that supports isolated sub-tasks.
A subagent is a second Claude with its own context window, its own system prompt, and its own permissions. It cannot see your main conversation and your main conversation cannot see its working notes. The parent describes a job, the subagent does it in isolation, and only the final summary crosses back. That isolation is the whole point: bulky intermediate work never touches the main window.
Each subagent is a plain Markdown file with YAML frontmatter. Three fields do most of the work:
The fastest path is the /agents command, which walks you through creating an agent and writes the file for you. You can also create the file by hand at .claude/agents/security-reviewer.md in your project, or under your home directory to make it available everywhere. The frontmatter configures the agent; the body is its system prompt.
A minimal, tightly scoped security reviewer looks like this:
---
name: security-reviewer
description: Reviews diffs for auth, injection and secret-handling issues
tools: Read, Grep, Glob
model: haiku
---
You are a security reviewer. Given a diff, report only concrete,
exploitable issues with a file:line and a one-line fix. Do not
comment on style. If you find nothing, say so plainly.Keep each subagent narrow: one job, one sharp description, and the smallest set of tools it needs. A vague catch-all agent gets invoked for the wrong tasks and quietly drags down the quality of everything it touches.
The rule of thumb is simple: delegate when the work generates far more tokens than the conclusion is worth keeping. Good candidates share that shape.
In practice the main session becomes an orchestrator. It plans the work, decides which pieces to delegate, and stitches the summaries back together. Your job is to describe each delegated task well: state the objective, the boundaries, and the exact shape of the output you want back.
Treat the delegation prompt like an API contract. A subagent that is told to return a ranked list of findings with file and line references gives you something you can act on; one told to please review the code gives you an essay you then have to parse.
Subagents do not share the main conversation's memory. Anything the parent already learned has to be restated in the delegation prompt, and a subagent cannot stop to ask you a follow-up mid-task, so hand it everything it needs up front or it will guess.
Subagents are a spend lever that cuts both ways, and treating them as free is the fastest way to a surprising bill.
Subagents turn Claude Code from one assistant into a small team you direct. Start with two you will actually use, a codebase explorer and a diff reviewer, keep each one narrow, and let the main session stay focused on the task instead of drowning in its own research.