Module 04: Agents & Specialization
Module 04: Agents & Specialization
Section titled “Module 04: Agents & Specialization”Time: 1.5 hours | Complexity: ⭐⭐ Intermediate
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