Module 04: Agents & Specialization
Last updated:
Time: 1.5 hours | Complexity: ⭐⭐ Intermediate
Previous: Module 03: Memory & Config
Create specialized agents for specific tasks. Learn how to focus AI capabilities on targeted problems.
What you’ll learn
Section titled “What you’ll learn”- What agents are and why they’re useful
- Creating custom agents (subagents) as
.claude/agents/<name>.mdfiles - Restricting an agent’s tools and permissions
- Using agents for specific tasks
- When to use agents vs Claude directly
What are agents?
Section titled “What are agents?”An agent (Claude Code calls it a subagent) is a specialized assistant with its own instructions and its own context window, configured for one specific task. Claude delegates a task to it and gets a result back.
Example: Code reviewer agent
Section titled “Example: Code reviewer agent”Instead of asking regular Claude for code reviews (which takes mental context-switching), you name the agent in your prompt:
Use the code-reviewer subagent to review this function for bugs and performance issuesThe code-reviewer agent:
- Only handles code review
- Has only the tools you gave it
- Follows the review checklist in its system prompt
- Doesn’t get distracted by other tasks
There is no /agent slash command. You invoke a subagent three ways: by naming it in a prompt, by @-mentioning it, or by running the whole session as that agent with claude --agent code-reviewer. See Invoke subagents explicitly.
Normal Claude vs agents
Section titled “Normal Claude vs agents”| Aspect | Normal Claude | Agent |
|---|---|---|
| Scope | General purpose | Specialized |
| Context | One shared conversation | Its own context window |
| Tools | All available | Restricted set (if you restrict it) |
| Speed | Multi-task capable | Fast at one thing |
| Use | Exploration, learning | Repetitive, specific tasks |
Creating your first agent
Section titled “Creating your first agent”A subagent is one Markdown file in .claude/agents/ (project) or ~/.claude/agents/ (all your projects). The filename can be anything; the name field is the identifier. There is no AGENT.md file.
Basic structure
Section titled “Basic structure”YAML frontmatter configures the agent, and the Markdown body becomes its system prompt. Only name and description are required.
---name: code-reviewerdescription: Reviews code for bugs, performance, and security. Use proactively after code changes.tools: Read, Grep, Glob, Bashmodel: sonnet---
You are a code reviewer.
## PurposeReview code for:- Bugs and logical errors- Performance issues- Security vulnerabilities- Code style consistency- Test coverage
## InstructionsWhen reviewing:1. Run `git diff` to see the recent changes2. Check for null pointers and edge cases3. Look for performance bottlenecks (O(n²), nested loops)4. Scan for security issues (SQL injection, XSS)5. Verify tests cover the change6. Suggest improvements without being harshThe description tells Claude when to delegate; phrases like “use proactively” encourage automatic delegation. Field names are camelCase (disallowedTools, maxTurns) and must match exactly: Claude Code silently ignores a field it does not recognize. Fields such as type, auto_invoke, requires_approval and capabilities do not exist.
Other useful fields: disallowedTools, model (sonnet, opus, haiku, fable, a full ID, or inherit), permissionMode, maxTurns, skills, memory, isolation. See the frontmatter reference.
File location
Section titled “File location”Place it in your project:
my-project/└── .claude/ └── agents/ └── code-reviewer.mdClaude Code watches these directories, so a new or edited file is picked up within a few seconds.
Making it discoverable
Section titled “Making it discoverable”Claude chooses subagents from their description. You can also list them in your project CLAUDE.md so teammates know they exist:
## Available SubagentsAsk for them by name, for example: "Use the code-reviewer subagent on src/auth.js"
- **code-reviewer** - Code quality and security review- **test-writer** - Generate tests for codeAgent design patterns
Section titled “Agent design patterns”The patterns below are agent templates for common jobs. For architectural patterns (Observer, Mediator, Circuit Breaker and others mapped to Claude Code mechanisms), see AI Design Patterns.
Pattern 1: Quality checker
Section titled “Pattern 1: Quality checker”---name: quality-auditordescription: Audits code quality metricstools: Read, Grep, Glob, Bash---
# Quality Auditor
## PurposeCheck code for:- Test coverage (<80% = fail)- Type safety (TypeScript strict mode)- Code duplication- Cyclomatic complexity
## How to measure- Run the project's coverage command via Bash- Run the type checker- Read the code for duplication and complexity
## Output Format- ✅ Passed: [metric] = X- ⚠️ Warning: [metric] = X- ❌ Failed: [metric] = X
## ScoringScore /100 based on all metrics.Pattern 2: Security specialist
Section titled “Pattern 2: Security specialist”---name: security-auditordescription: Scans code for vulnerabilitiestools: Read, Grep, Glob---
# Security Auditor
## PurposeFind security vulnerabilities:- Injection attacks (SQL, NoSQL, command)- Authentication/authorization issues- Cryptography mistakes- Data exposure risks- OWASP Top 10
## Method- Read the code paths that handle input, auth and secrets- Grep for known risky patterns- Report findings only; do not modify files
## Severity Levels- CRITICAL: Stop work immediately- HIGH: Fix before merge- MEDIUM: Fix in next sprint- LOW: Consider fixingPattern 3: Documentation writer
Section titled “Pattern 3: Documentation writer”---name: doc-writerdescription: Generates documentation---
# Documentation Writer
## PurposeCreate or improve:- README files- API documentation- Architecture docs- User guides- CHANGELOG entries
## Output Format- Clear headings- Code examples for each feature- Link to related docs- Numbered lists for sequences
## Style- Beginner-friendly- No jargon without explanation- Show before/after examplesAgent capabilities & restrictions
Section titled “Agent capabilities & restrictions”Default capabilities
Section titled “Default capabilities”A subagent inherits every tool available to the main conversation (including MCP tools) unless you narrow it with tools or disallowedTools. Without a restriction it can read files, edit files, and run shell commands, subject to your permission rules.
Restricting tools
Section titled “Restricting tools”Use tools as an allowlist, or disallowedTools as a denylist:
---name: code-reviewerdescription: Reviews code without modifying ittools: Read, Grep, Glob---This agent can read and search but has no Edit, Write or Bash, so it cannot change anything.
Common restrictions
Section titled “Common restrictions”# Analyzer (read-only)tools: Read, Grep, Glob# No Bash, Edit or Write: cannot modify anything
# Refactoring agent (can edit and run tests, cannot touch the network tools)tools: Read, Grep, Glob, Edit, Write, BashdisallowedTools: WebFetch, WebSearch
# Full agent (inherits everything)# omit `tools` entirely# Can do anything the main session can (use with caution)Two traps
Section titled “Two traps”- Allowing
Bashis allowing the shell:tools: Read, Grep, Glob, Bashstill lets the agent change files and rungit pushthrough commands. The tool list controls tools, not commands. disallowedTools: Bash(git push *)does not block only pushes: an entry with a specifier removes the wholeBashtool. To block a specific command, add a deny rule in your settings (permissions.deny: ["Bash(git push *)"]) or enforce it with aPreToolUsehook (Module 06).
You can also set permissionMode in the frontmatter (default, acceptEdits, auto, dontAsk, bypassPermissions, plan). A subagent runs in bypassPermissions only when the main conversation does.
Using agents in your workflow
Section titled “Using agents in your workflow”Calling an agent
Section titled “Calling an agent”Use the code-reviewer subagent to review the changes I just made to src/auth.jsOr @-mention it from the typeahead:
@"code-reviewer (agent)" look at the auth changesClaude delegates to the subagent, which works in its own context window and returns a summary.
Chaining agents
Section titled “Chaining agents”Use agents sequentially by asking for each step in turn:
# Step 1: test-writer generates testsUse the test-writer subagent to write tests for src/utils/validators.js
# Step 2: code-reviewer checks the testsUse the code-reviewer subagent to review the tests that were just written
# Step 3: security-auditor scansUse the security-auditor subagent to check the tests and code for vulnerabilitiesYou can also chain them in one prompt: “Use test-writer for validators.js, then code-reviewer on the result.”
Agent with plan mode
Section titled “Agent with plan mode”For risky operations, run the whole session as the agent and start in plan mode:
claude --agent refactoring-specialistThen press Shift+Tab until plan mode is active (or type /plan) and describe the task:
Refactor the payment processing module to use async/awaitYou can also pin permissionMode: plan in the agent’s frontmatter.
Exercise: Create a test-writer agent
Section titled “Exercise: Create a test-writer agent”Step 1: Create the agent file
Section titled “Step 1: Create the agent file”cat > .claude/agents/test-writer.md << 'EOF'---name: test-writerdescription: Generates comprehensive tests. Use when asked to write or extend tests.tools: Read, Grep, Glob, Edit, Write, Bash---
# Test Writer Agent
## PurposeGenerate high-quality tests for:- Unit tests (pure functions)- Integration tests (component interactions)- Edge cases and error conditions- Performance tests
## Style- Arrange-Act-Assert pattern- Descriptive test names- Each test focuses on ONE behavior- 70%+ code coverage target
## Tooling- Use the project's existing test framework (Jest, pytest, etc)- Run the new tests with Bash before reporting
## Output- Tests in same directory as source- Naming: [file].test.js or [file].spec.js- Include setup/teardown codeEOFStep 2: Reference in CLAUDE.md
Section titled “Step 2: Reference in CLAUDE.md”## Available Subagents- test-writer: Generate tests for any function or module Usage: "Use the test-writer subagent on <file path>"Step 3: Use it
Section titled “Step 3: Use it”Use the test-writer subagent to write tests for src/utils/formatDate.jsThe agent will:
- Read formatDate.js
- Understand what it does
- Generate comprehensive tests
- Show you the test file
Step 4: Review
Section titled “Step 4: Review”Check the tests before accepting:
- Do they cover edge cases?
- Is naming clear?
- Do they actually run?
When to use agents
Section titled “When to use agents”Use agents when:
Section titled “Use agents when:”✅ You do the same task repeatedly (code review, testing, security audit) ✅ You want focused AI for one job ✅ You want to restrict tools (safety) ✅ You’re building team workflows ✅ The task has clear success criteria
Use regular Claude when:
Section titled “Use regular Claude when:”✅ You’re exploring/learning ✅ The task is novel ✅ You need general-purpose help ✅ You’re debugging something complex ✅ You want conversational back-and-forth
Best practices
Section titled “Best practices”✅ Give agents clear, narrow purposes
✅ Document output format in the agent definition
✅ Restrict tools you don’t need
✅ Test agents on sample tasks first
✅ Version control your agents (in .claude/agents/)
❌ Create agents with overlapping purposes (confusing)
❌ Make agents too general (defeats the purpose)
❌ Trust an agent completely (always review)
❌ Create an agent for a one-off task (just use Claude)
Validation: You’re ready if…
Section titled “Validation: You’re ready if…”✓ You’ve created at least one custom agent
✓ You understand the purpose of agents vs Claude
✓ You can restrict an agent’s tools (and you know Bash access is not command-level control)
✓ You know how to call an agent (name it in a prompt, @-mention it, or claude --agent <name>)
✓ You’ve tested your agent on a real task
What’s next?
Section titled “What’s next?”Module 05: Skills & Automation covers:
- Creating reusable skills (knowledge modules)
- Bundling capabilities for distribution
- Skill auto-invocation
- Building your custom knowledge base
This teaches you how to package knowledge so Claude remembers it across sessions.
Completed Module 04? → Ready for Module 05: Skills & Automation