Founders BetaFounder price: US$ 9.90/month locked for as long as you subscribe.→
HomeBlogDicasSubagents in Claude Code: how to create yours in 5 steps
Claude Code documentation card on how to create custom subagents
Image: Anthropic (Claude Code Docs)
Dicas6 min

Subagents in Claude Code: how to create yours in 5 steps

Learn to create subagents in Claude Code with Markdown files: scope, tools, model, and worktree isolation, all verified in the official documentation.

Your Claude Code reads dozens of files to understand a bug, and in the end the conversation is full of logs and searches you will never look at again. The solution is to delegate that task to a subagent, and you can create yours with a single Markdown file.

The biggest AI newsletter in the World

Get AI articles in your inbox

Technical curation on models, agents and coding tools. No spam, unsubscribe anytime.

Daily at 8:00 and 16:00 Brasília time. No spam.

Each subagent runs in its own context window, with independent prompt, tools, and permissions, and returns only the summary. In this guide, everything comes from the official Claude Code subagents documentation.

In this article you will see:

  • what a subagent is and when it is worth creating one
  • how to write the file and choose the scope
  • how to limit tools and model
  • how to isolate the subagent in a worktree

What is a subagent in Claude Code

A subagent is a specialized assistant for a type of task. It works in its own context window and delivers only the result, which keeps the main conversation clean.

!Claude Code documentation card about custom subagents

The documentation lists the gains: preserving context, limiting which tools the subagent can use, reusing configurations across projects, and choosing a cheaper model for simple tasks.

An important detail: subagent requests count toward the same usage limits as your main conversation. It organizes the work, it is not free usage.

Claude Code already includes internal subagents, such as Explore, read-only and focused on searching and analyzing code, and Plan, used in planning mode. Create yours when you notice you are repeating the same type of request with the same instructions.

Step 1: choose where the file goes

The file location defines who can use the subagent. See the two most common options:

  • .claude/agents/ at the project root: valid only for that codebase and can go into version control, for the whole team to use and improve.
  • ~/.claude/agents/: personal and available in all your projects.

If two subagents have the same name, the higher priority one wins, and the project one beats the personal one. Keep names unique, because identity comes only from the name field, not the folder name.

Step 2: write the Markdown file

A subagent is a file with YAML frontmatter, followed by the system prompt in Markdown. This example, identical to the documentation, creates a read-only reviewer:

Markdown · .claude/agents/code-reviewer.md
---
name: code-reviewer
description: Reviews code for quality and best practices
tools: Read, Glob, Grep
model: sonnet
---

Você é um revisor de código. Quando for chamado, analise o código e dê
feedback específico e acionável sobre qualidade, segurança e boas práticas.

The description field is the most important: Claude uses this text to decide when to delegate. Keep it short and clear, because descriptions consume context.

Step 3: limit tools and choose the model

The tools field restricts what the subagent can do. In the example, only Read, Glob, and Grep: it can read and search, but not edit. It is a simple way to create a reviewer that does not touch the code.

The model field accepts, for example, sonnet. The documentation notes that routing simple tasks to faster, cheaper models, such as Haiku, helps control cost.

To protect sensitive files in any agent, combine this with the hook from the post how to block edits to .env with a PreToolUse hook.

Step 4: test the delegation

Ask explicitly in Claude Code:

Texto
Use the code-reviewer agent to review the changes in this project

In the transcript, the delegation appears as a line with the subagent name and a short task description. If Claude does not find the subagent, restart Claude Code and try again.

The documentation states that Claude Code watches the .claude/agents/ and ~/.claude/agents/ folders. When creating or editing a file, the change is detected in a few seconds, without restarting. The exception is folders added with --add-dir.

Step 5: isolate the subagent in a worktree

If the subagent edits files, several running together can step on each other. To avoid this, add isolation: worktree to the frontmatter. This is the example from the worktrees documentation:

Markdown · .claude/agents/refactorer.md
---
name: refactorer
description: Applies mechanical refactors across many files
isolation: worktree
---

Aplique o refactor pedido em todos os arquivos afetados, rode os testes
e reporte os resultados.
Claude Code documentation card on parallel sessions with worktrees
Imagem: Anthropic (Claude Code Docs)

With this, the subagent works in its own worktree. When it finishes, Claude Code checks the worktree: if there were no changes and no new commit, it can be removed right away. If there were changes, the worktree stays until the periodic cleanup runs.

The subagent worktree starts from the repository's default branch, unless the worktree.baseRef setting is "head". It is also worth knowing that, in this mode, the subagent does not load the CLAUDE.md from the worktree root. It uses the instructions from your main conversation.

Subagent, parallel session, or agent team

The documentation separates three things that often get mixed up. Subagents run inside a single session. For multiple independent sessions monitored in one place, there are background agents. For sessions that Claude coordinates, there are agent teams.

If your question is about running two tools in the same project, see also how to use Claude Code and Codex together in the same repo without conflicts. And for other everyday habits, there are the 5 tips for using Claude Code daily.

Frequently asked questions about Claude Code subagents

What is a subagent in Claude Code?

It is a specialized assistant that runs in its own context window, with an independent system prompt, tools, and permissions. It performs the task and returns only the summary to the main conversation.

Where are subagent files located?

In .claude/agents/ for project subagents and in ~/.claude/agents/ for personal subagents, available across all projects. Project subagents take priority over personal ones when the name is the same.

Do I need to restart Claude Code after creating a subagent?

Generally not. The documentation states that Claude Code detects changes in these folders within seconds. The exception is folders added with --add-dir or /add-dir, which require a restart.

How do I make a subagent run in an isolated worktree?

Add isolation: worktree to the file's frontmatter. This way it always works in its own worktree, and parallel edits don't conflict.

Is a subagent the same as running multiple sessions in parallel?

No. Subagents divide work within a single session. For multiple independent sessions, the documentation recommends worktrees and background agents.

How to use subagents without polluting context

Start with a single read-only subagent for review or research. Give it a clear name, a short description, and the minimum set of tools. Only then add subagents that edit files, and use isolation: worktree on those.

The practical gain: the main conversation stays focused on the decision, and the heavy lifting happens outside it. If the behavior is not what you expected, adjust the description first, because that is what triggers the delegation.

Stop switching between ten AI tools. Coordinate your agents in a single flow on bridgecode.

Source: Claude Code Docs

Share this article

Tags:#claude-code#subagentes#agentes de código#dicas
Marcelo Santiago
Marcelo Santiago·Creator of bridgecode

Developer and creator of bridgecode, an agentic development environment for orchestrating squads of AI agents. Helps people going from vibe coding to professional development move from idea to shipped product.

The biggest AI newsletter in the World

Enjoyed this content? Stay ahead in AI engineering.

Code analysis, multi-agent architecture, token benchmarks and tools selected by developers for developers.

Daily at 8:00 and 16:00 Brasília time. No spam.