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 with AGENT.md
- Restricting agent capabilities (sandboxing)
- Using agents for specific tasks
- When to use agents vs Claude directly
What are agents?
Section titled “What are agents?”An agent is a specialized version of Claude Code configured for one specific task.
Example: Code reviewer agent
Section titled “Example: Code reviewer agent”Instead of asking regular Claude for code reviews (which takes mental context-switching), you use:
/agent code-reviewerReview this function for bugs and performance issuesThe code-reviewer agent:
- Only handles code review
- Has review-specific tools
- Knows security vulnerability patterns
- Doesn’t get distracted by other tasks
Normal Claude vs agents
Section titled “Normal Claude vs agents”| Aspect | Normal Claude | Agent |
|---|---|---|
| Scope | General purpose | Specialized |
| Context | Remembers everything | Focused task memory |
| Tools | All available | Restricted set |
| Speed | Multi-task capable | Fast at one thing |
| Use | Exploration, learning | Repetitive, specific tasks |
Creating your first agent
Section titled “Creating your first agent”Agents are defined in .claude/agents/AGENT.md files.
Basic structure
Section titled “Basic structure”---name: code-reviewertype: agentdescription: Reviews code for bugs, performance, and securityauto_invoke: falserequires_approval: true---
# Code Reviewer Agent
## PurposeReview code for:- Bugs and logical errors- Performance issues- Security vulnerabilities- Code style consistency- Test coverage
## Tools- Code analysis- Git diff viewer- Test runner- Linting tools
## InstructionsWhen reviewing:1. Check for null pointers and edge cases2. Look for performance bottlenecks (O(n²), nested loops)3. Scan for security issues (SQL injection, XSS)4. Verify tests cover the change5. Suggest improvements without being harsh
## Example Usage/agent code-reviewerReview src/auth.js for security issuesFile location
Section titled “File location”Place it in your project:
my-project/└── .claude/ └── agents/ └── code-reviewer.mdMaking it available
Section titled “Making it available”In your project CLAUDE.md, reference it:
## Available AgentsRun agents with: /agent [name]
- **code-reviewer** - Code quality and security review Usage: /agent code-reviewer <description>
- **test-writer** - Generate tests for code Usage: /agent test-writer <file path>Agent design patterns
Section titled “Agent design patterns”Pattern 1: Quality checker
Section titled “Pattern 1: Quality checker”---name: quality-auditordescription: Audits code quality metrics---
# Quality Auditor
## PurposeCheck code for:- Test coverage (<80% = fail)- Type safety (TypeScript strict mode)- Code duplication- Cyclomatic complexity
## Tools- Code analysis- Coverage reporter- Type checker
## 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 vulnerabilitiesrequires_approval: true---
# Security Auditor
## PurposeFind security vulnerabilities:- Injection attacks (SQL, NoSQL, command)- Authentication/authorization issues- Cryptography mistakes- Data exposure risks- OWASP Top 10
## Tools- Static analysis- Dependency checker- Secret detection
## 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”All agents can:
- Read files (git-aware)
- Analyze code
- Write documentation
- Check syntax
- Run tests
Restricting capabilities
Section titled “Restricting capabilities”Use capabilities to sandbox an agent:
---name: code-reviewercapabilities: - read_files # Can read code - run_tests # Can run test suites - check_syntax # Can lint - write_comments # Can suggest changes but... - NO: commit # ...cannot commit - NO: push # ...cannot push to git---This agent can review but can’t accidentally push broken code.
Common restrictions
Section titled “Common restrictions”# Analyzer (read-only)capabilities: - read_files - run_tests# Can't modify anything
# Refactoring Agent (write, no push)capabilities: - read_files - write_files - run_tests - NO: commit - NO: push# Can change code but you review before pushing
# Full Agent (unrestricted)capabilities: - all# Can do anything (use with caution)Using agents in your workflow
Section titled “Using agents in your workflow”Calling an agent
Section titled “Calling an agent”/agent code-reviewerReview the changes I just made to src/auth.jsClaude switches to the code-reviewer agent and responds.
Chaining agents
Section titled “Chaining agents”Use agents sequentially:
# Step 1: Test Writer generates tests/agent test-writerWrite tests for src/utils/validators.js
# Step 2: Code Reviewer checks the tests/agent code-reviewerReview the tests that were just written
# Step 3: Security Auditor scans/agent security-auditorCheck the tests and code for vulnerabilitiesAgent with plan mode
Section titled “Agent with plan mode”For risky operations, use /plan within an agent:
/agent refactoring-specialist/planRefactor the payment processing module to use async/awaitExercise: 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 testscapabilities: - read_files - write_files - run_tests---
# 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
## Tools- Test framework (Jest, pytest, etc)- Mock libraries- Assertion libraries
## 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 Agents- test-writer: Generate tests for any function or module Usage: /agent test-writer <file path>Step 3: Use it
Section titled “Step 3: Use it”/agent test-writerWrite 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 capabilities (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 capabilities 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 agent capabilities
✓ You know how to call an agent (/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