Skip to content
Dashboard

Claude Code subagents and when to use them

Claude Code can hand a task like tracing a failing test to a subagent, so the raw logs stay out of the main conversation. Here's how to define a subagent, decide what to delegate, and choose where to run Claude Code if you bypass routine approvals.

Copy link to headingWhat is a Claude Code subagent?

A Claude Code subagent is a separate worker that Claude starts inside your session. A standard subagent has its own context window, system prompt, tool access, and permissions. It works in that separate context and reports back to the main conversation.

Claude Code includes Explore, Plan, and general-purpose. You can define your own subagents in Markdown. Each run belongs to the session that started it, even if other sessions can use the same definition.

Copy link to headingSingle sessions, subagents, and agent teams in Claude Code

The modes differ in who coordinates the work and where the results go. Experimental agent teams are off by default. Set CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS=1 to enable them.

Here's how the three modes compare on coordination and cost:

Where they differ

Single session

Subagents

Agent teams

Coordinator

You and Claude in one conversation

The main session starts subagents and collects their results

A lead session assigns and oversees work

Context

One window, compacted as it fills

A private window per subagent

A private window per teammate

Communication

Direct back-and-forth with you

The subagent reports back to the caller

Teammates message each other and share a task list

Typical token cost

Lowest for routine tasks

Usually lower than teams because subagents send back summaries

Usually higher because each teammate runs a separate Claude instance

Best fit

Sequential work and same-file edits

Focused tasks where you only need the result

Independent tasks that benefit from discussion between teammates

Separate conversations don't mean separate files. Two teammates editing the same file can overwrite each other, so a team only helps when each teammate owns its own set of files before parallel work begins.

Copy link to headingWhy use Claude Code subagents?

These are the jobs where a subagent earns its place over a single session:

  • Clean main context: Keep search results, logs, and file contents out of the main conversation. The subagent sends back its findings.

  • Limit tool access: With tools: Read, Grep, Glob, the reviewer can inspect files but can't edit them.

  • Independent review: A reviewer that starts with fresh context can check a change without seeing the main session's reasoning.

  • Parallel research: Several subagents can work at once when none needs another's findings to get started.

  • Reusable workers: Save a definition so you don't have to give the same worker its instructions each time.

When a subagent keeps the raw test output, the main session has more room to work on the fix.

Copy link to headingCreate and configure a subagent

A custom subagent's definition tells it what to do and can set its tools and model. The subagent reports its result to the main conversation.

Copy link to headingWrite a definition

A subagent definition starts with a short block of settings called YAML frontmatter. Only name and description are required. The Markdown below the frontmatter becomes the subagent's system prompt. Save this four-field reviewer as a file called .claude/agents/code-reviewer.md:

---
name: code-reviewer
description: Reviews code for quality and best practices
tools: Read, Glob, Grep
model: sonnet
---
Act as a code reviewer. When invoked, review the code and give specific,
actionable feedback on its quality, security, and adherence to best practices.

Save a project subagent in .claude/agents/ and check it into version control to share it with your team. Save a personal subagent in ~/.claude/agents/ to use it across projects.

If names conflict, Claude Code checks managed settings first, then the --agents flag, project definitions, user definitions, and plugins.

In v2.1.198 and later, /agents prints a reminder to ask Claude to create the files or edit them yourself.

Copy link to headingSet tools and permissions

Use a comma-separated string or a YAML list for tools. If you leave it out, the subagent inherits the tools it can use. Claude Code applies disallowedTools before checking the allowlist. For example, Bash(git push *) in disallowedTools removes all of Bash.

With persistent memory enabled, Claude Code also enables file tools, allowing the subagent to manage its memory.

If the main session uses bypassPermissions, acceptEdits, or auto, that mode overrides the subagent's permissionMode. Plan mode keeps subagents in plan mode unless they run as agent team teammates. A reviewer's permissionMode: default won't bring back routine prompts if the parent session uses bypass mode.

Copy link to headingChoose a model

For model, use sonnet, opus, haiku, fable, a full model ID such as claude-opus-5-5, or inherit. By default, Claude Code checks for a model alias on the call first, then checks the frontmatter and CLAUDE_CODE_SUBAGENT_MODEL. If none is set, it uses the model of the session or subagent that started it.

The model aliases vary by provider. On the Anthropic API, sonnet defaults to Claude Sonnet 5.5 and opus to Claude Opus 5.5. The same sonnet alias defaults to Sonnet 4.6 on Claude Platform on AWS and Sonnet 4.5 on Amazon Bedrock and Google Cloud's Agent Platform. On Microsoft Foundry, opus defaults to Opus 4.6. To pin a reviewer to a version, use a model ID that your provider supports and your organization permits.

Copy link to headingUnderstand what context subagents inherit

A standard subagent starts with a fresh context window. It doesn't inherit the parent's conversation, tool results, or skills used there. Claude should include the file paths, error text, and decisions the subagent needs. As in other multi-agent systems, the caller supplies the context for the task. Claude Code can also load project instructions and configured skills.

A fork inherits the full conversation, system prompt, and tools. Its first request reuses the parent's prompt cache. Fork mode lets Claude start forks on its own. It's on by default as of Claude Code v2.1.302, or v2.1.232 in interactive terminal sessions.

Copy link to headingTrace a failing test with a subagent

Start with a failing test. The tracer can read a long stack trace and tell you where the failure started. Save this definition to .claude/agents/test-tracer.md. It leaves out Edit and Write but keeps Bash to run the test. The prompt asks the tracer to leave files alone, but Bash can still write to disk:

---
name: test-tracer
description: Traces a failing test to its root cause and reports file paths and line numbers
tools: Read, Grep, Glob, Bash
model: sonnet
---
Run the failing test, read the stack trace, and follow it into the source.
Report the root cause in under 200 words, with the file path and line number
for each finding. Do not propose or make edits.

To request the tracer, type @agent-test-tracer find why checkout.test.ts fails. Claude writes the delegation prompt. The tracer's instructions ask for a short report with the file paths and line numbers you'll need for the fix.

Copy link to headingManage subagent cost and usage

Coding agents initiated more than half of Vercel deployments as of September 2026. Agent permissions can affect code that ships. Delegate where it saves work to keep costs down. If you bypass routine approvals, run the session inside an operating-system boundary.

A subagent that starts fresh can slow you down if it has to relearn what the main session already knows. In a multi-hop reasoning study, single agents matched or beat the tested multi-agent systems when given the same number of thinking tokens.

Keep the work in the main conversation if:

  • You need to go back and forth.

  • The steps rely on the same background.

  • The change is small and specific.

  • You need a fast answer.

Subagents make their own requests and count toward the same usage limits as the main session. Try model: haiku for lookups or summaries. On Pro, Max, Ultra, Team, and Enterprise, /usage shows how much of your recent usage came from subagents.

The built-in Explore usually runs on the model of the session that started it. If that session uses Fable, Explore switches to Opus on Claude subscriptions, Anthropic Console, and LLM gateways configured with ANTHROPIC_BASE_URL. It stays on Fable on other supported connections, including Claude apps gateways. To use the lower-cost Haiku model, create a user or project subagent named Explore with model: haiku.

Copy link to headingIsolate Claude Code sessions

If you run Claude Code with --dangerously-skip-permissions, use a container, a virtual machine (VM), or Anthropic's sandbox runtime. This also puts file tools, Model Context Protocol (MCP) servers, and hooks inside the boundary. On Linux and macOS, Claude Code normally refuses the flag if you run as root or use sudo. It skips the check in a recognized sandbox.

Code in a dev container can access the files and credentials you put there, including Claude Code credentials if you mount ~/.claude. With --dangerously-skip-permissions, what the project can send out depends on what it can access and what the container's network policy allows.

Claude Code's shell sandbox is off by default. Turn it on with /sandbox or set sandbox.enabled to true. It uses Seatbelt on macOS and bubblewrap on Linux and Windows Subsystem for Linux 2 (WSL2). Because subagents share the parent's process and configuration, the same setting applies to their shell commands.

Shell sandboxing cut permission prompts by 84% in Anthropic's internal use. Claude Code's file tools and helper processes run outside the shell sandbox. A container or VM can cover them too.

Copy link to headingRun Claude Code in Vercel Sandbox

Vercel Sandbox can run Claude Code in a Firecracker microVM with its own kernel, filesystem, and network. The VM also covers the subagents because they share the session's process. Conductor built Cloud Workspaces on Vercel Sandbox so agents such as Claude Code can keep working after you close your laptop.

If you bypass routine approvals on your machine, a subagent with shell access can run commands and edit files within the limits of its account and sandbox settings. With the Sandbox CLI 4.8.0 or later, run this command to start Claude Code in a new microVM and use it from your local terminal:

Terminal
npx sandbox@latest create claude

The CLI doesn't upload local project files. Copy in only what the task needs.

Sessions can run for up to 45 minutes on Hobby, or for up to 24 hours on Pro and Enterprise. By default, Sandbox saves the filesystem when a session stops. Use Vercel Drives when workspace files need to outlive the sandbox and be mounted in a new one. Quitting Claude Code leaves the sandbox running, so use the stop command the CLI prints when you're done.

Vercel tested the boundary by pointing an open-weight model with no safeguards at Sandbox, and it didn't escape.

Copy link to headingIsolate parallel sessions

Subagents share the parent's process and sit inside the same boundary. A standard subagent already starts with fresh context, and a Git worktree gives it a separate copy to edit. To isolate sessions from each other at the operating-system level, run each session in its own sandbox.

Each sandbox has its own guest kernel, which isolates its processes from other sandboxes. Vercel's concurrency quotas are 10 sandboxes on Hobby and 10,000 on Pro and Enterprise. The Herdr integration provides each coding agent with a persistent sandbox and returns a Git patch that you can review and apply locally.

Copy link to headingKeep credentials out of the sandbox

A subagent with shell access can read credentials in its environment. Whether it can send them elsewhere depends on the network policy.

With sandbox create claude, sign in to Claude Code separately from Vercel or pass Claude Code credentials with --env. Either way, the credentials are available inside the sandbox.

With credentials brokering, the Sandbox firewall adds secrets to outgoing requests. Code can call services such as the Vercel AI Gateway without seeing the token.

Set rules in the SDK's networkPolicy to match paths, methods, query parameters, or headers. Requests that don't match can still reach an allowed domain without the credential. Hobby includes every firewall feature.

For apps, durable-harness offers an experimental Claude Code integration. The Vercel Function uses the deployment's OpenID Connect (OIDC) token to make model calls, so the sandbox needs no model API key. The integration keeps the workspace on a Drive and records the session in a durable log, so it can recover after an interruption.

Copy link to headingControl outbound traffic

A malicious file or web page may steer a subagent into sending code to an outside server. If the subagent has network access, that may happen without a routine approval prompt.

The Sandbox firewall defaults to allow-all. Use deny-all to block outbound traffic, including Domain Name System (DNS) lookups, or set a custom policy that allows specific domains or address ranges.

Use the SDK or sandbox config network-policy to change the policy without restarting the session. For example, you can restrict traffic after installing dependencies.

An interactive session calls the model from inside the microVM, so its network policy must allow access to the configured model endpoint. With durable-harness, model calls still work under deny-all because the Vercel Function makes them. The sandbox's own commands still can't reach the internet.

Copy link to headingWhat Vercel Sandbox provides

Delegate self-contained tasks that produce a lot of output. Keep related edits that need your input in the main conversation. If you bypass routine approvals, what a subagent can reach depends on its tools and how you isolate the session.

Vercel covers the session boundary for Claude Code:

  • A microVM per session in Vercel Sandbox: Claude Code runs commands in the remote VM. Its filesystem persists by default, and commands can still affect external services they can reach.

  • Separate sandboxes for parallel sessions: Each sandbox has its own guest kernel, which keeps its processes separate.

  • Brokered secrets at the firewall: The firewall adds credentials to outgoing requests without giving the agent the keys.

  • Runtime egress policies: Choose which destinations the sandbox can reach. You can tighten the rules while it runs.

  • Git patch review through Herdr: Review the patch before applying the agent's changes to your checkout.

On Vercel, you can start a project or browse templates for a coding-agent setup.

Copy link to headingFrequently asked questions

Copy link to headingHow many subagents can run at once?

As of November 2026, the default is 20 concurrent subagents. At the limit, another Agent call fails with Concurrent subagent limit reached.

In Claude Code v2.1.217 or later, set CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS to change the concurrency limit for the session.

The limit doesn't apply in ultracode sessions. Manually started forks and resumed agents can take slots even at the limit. There's no cap on how many subagents a session starts over its lifetime.

Copy link to headingCan subagents start other subagents?

Yes. By default, subagents can nest up to three layers below the main conversation. Set CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH to change the depth. A value of 1 turns nesting off. In agent teams, only the lead manages the team. Teammates can't start teammates of their own.

Copy link to headingDo subagents load CLAUDE.md and auto memory?

Not all of them. Custom and general-purpose subagents load CLAUDE.md files at startup. Explore and Plan skip them at startup, but can still load directory instructions as they work with files.

In v2.1.271 or later, set omitClaudeMd: true to skip user, project, and local files at startup. Managed policy files can still load.

Standard subagents don't inherit the main session's auto memory. If auto memory is on, use the memory field to give a subagent its own store.

Copy link to headingCan subagents run in the background?

Yes. Most top-level subagents start in the background so you can keep working. If one needs permission to use a tool, Claude Code shows the prompt in the main session and names the subagent. Background subagents usually have fewer built-in tools. Forks and resumed foreground subagents are exceptions.

Reference Architecture

Choose a stack and work through the decisions behind it, with curated documentation, guides, templates, and courses to help you build.

  • Agent stack: Build and operate agents with models, tools, and durable workflows.
  • Web applications: Choose rendering, data, authentication, and caching approaches.
  • Backends: Choose a runtime, connect services, and run background work.

Browse all stacks

Build with prompts

Build an app, extend an agent, or improve your project with ready-to-use prompts for your coding agent.

More Build with AI articles

Ready to deploy?