Subagents 101: Specializing Your Claude Code Workflow
An introduction to subagent architecture for Claude Code. How to use specialized AI assistants for different tasks in your development workflow.
Subagents 101: Specializing Your Claude Code Workflow
When you use Claude Code for a complex project, you're asking one AI to be expert at everything: database design, API development, frontend components, testing, documentation, security review. That's a lot to ask.
Subagents offer a different approach: specialized AI assistants that each handle specific domains. The main agent orchestrates, while subagents execute in their areas of expertise.
Let me show you how this works and why it matters.
What Are Subagents?
A subagent is a Claude Code instance configured for a specific task or domain. Instead of one generalist AI doing everything, you have:
- A main orchestrator that understands the overall project and coordinates work
- Specialized subagents that handle specific tasks with focused expertise
Think of it like a software team. You have an architect who designs the system, but you also have database specialists, frontend developers, security engineers. Each brings deep expertise to their domain.
Why Subagents Matter
Context Management
Claude Code's context window is large (200K tokens) but not infinite. Complex projects can exhaust it quickly when you're trying to hold the entire codebase in context.
Subagents help by compartmentalizing. The database subagent only needs to see schema files and migration code. The frontend subagent only needs component code and API contracts. Each works within a focused context.
Domain Expertise
A generalist prompt works reasonably well for most tasks. But a prompt optimized for database work—with knowledge of indexing strategies, normalization rules, query optimization—produces better results for database tasks.
Subagents let you craft specialized prompts for each domain.
Parallel Execution
When tasks are independent, subagents can work simultaneously. While the database subagent creates migrations, the frontend subagent can build components. The main orchestrator merges the results.
Basic Subagent Architecture
Here's a simple subagent setup:
Main Orchestrator
├── Database Subagent
│ └── Schema design, migrations, queries
├── API Subagent
│ └── Endpoint implementation, validation
├── Frontend Subagent
│ └── Components, state management
└── Test Subagent
└── Unit tests, integration tests
The Orchestrator
The main agent reads the spec, breaks work into domain-specific tasks, and delegates:
claude --print "
You are the project orchestrator. Read docs/spec.md.
For each requirement:
1. Identify which domain it belongs to (database, API, frontend, test)
2. Create a task file in tasks/[domain]/[task-id].md
3. Include all context the subagent will need
4. Track task dependencies
Do not implement anything yourself. Your job is coordination.
"
The Subagent
Each subagent runs with a domain-specific prompt:
# Database subagent
claude --print "
You are a database specialist. Your expertise:
- PostgreSQL schema design
- Migration strategies
- Query optimization
- Index selection
Read your task from tasks/database/current.md
Implement it in src/db/
Run validation with 'npm run db:validate'
When complete, write status to tasks/database/current.status
"
Practical Subagent Configurations
Database Subagent
# Database Subagent Configuration
## Expertise
- PostgreSQL schema design and migrations
- Prisma ORM operations
- Query optimization and indexing
- Data integrity constraints
## Available Commands
- npm run db:migrate -- Create migration
- npm run db:push -- Push schema changes
- npm run db:seed -- Seed test data
- npm run db:validate -- Validate schema
## Conventions
- Migrations in prisma/migrations/
- Always include rollback strategy
- Use UUID for primary keys
- Soft delete (deletedAt) instead of hard delete
## Context Files
- prisma/schema.prisma
- src/db/queries/
- docs/data-model.md
API Subagent
# API Subagent Configuration
## Expertise
- Express.js route handlers
- Request validation with Zod
- Error handling patterns
- Authentication middleware
## Available Commands
- npm run dev -- Start dev server
- npm run test:api -- Run API tests
- npm run lint -- Check code style
## Conventions
- Routes in src/routes/
- Validation schemas in src/validation/
- Error responses follow RFC 7807
- All endpoints require authentication unless marked public
## Context Files
- src/routes/
- src/middleware/
- src/validation/
- docs/api-spec.md
Frontend Subagent
# Frontend Subagent Configuration
## Expertise
- React 18+ with hooks
- TypeScript strict mode
- Tailwind CSS styling
- React Query for data fetching
## Available Commands
- npm run dev -- Start dev server
- npm run test -- Run component tests
- npm run storybook -- Launch component viewer
## Conventions
- Components in src/components/
- One component per file
- Use function components only
- Props interface defined above component
## Context Files
- src/components/
- src/hooks/
- src/styles/
- docs/design-system.md
Test Subagent
# Test Subagent Configuration
## Expertise
- Jest and Testing Library
- API testing with Supertest
- E2E testing with Playwright
- Test coverage analysis
## Available Commands
- npm test -- Run all tests
- npm run test:coverage -- With coverage report
- npm run test:e2e -- End-to-end tests
## Conventions
- Tests next to source files (*.test.ts)
- One test file per source file
- Describe blocks match function names
- Mock external dependencies
## Context Files
- src/**/*.test.ts
- src/__mocks__/
- tests/e2e/
Orchestration Patterns
Sequential Execution
When tasks have dependencies:
#!/bin/bash
# 1. Database first
echo "Running database subagent..."
claude --prompt "$(cat prompts/db-subagent.md)" \
--context "task: Create user schema"
# 2. API depends on database
echo "Running API subagent..."
claude --prompt "$(cat prompts/api-subagent.md)" \
--context "task: Create user endpoints"
# 3. Frontend depends on API
echo "Running frontend subagent..."
claude --prompt "$(cat prompts/frontend-subagent.md)" \
--context "task: Create user components"
# 4. Tests depend on implementation
echo "Running test subagent..."
claude --prompt "$(cat prompts/test-subagent.md)" \
--context "task: Test user functionality"
Parallel Execution
When tasks are independent:
#!/bin/bash
# Run independent subagents in parallel
claude --prompt "$(cat prompts/db-subagent.md)" \
--context "task: Create analytics schema" &
PID_DB=$!
claude --prompt "$(cat prompts/api-subagent.md)" \
--context "task: Create auth middleware" &
PID_API=$!
# Wait for both
wait $PID_DB $PID_API
# Continue with dependent tasks
echo "Parallel tasks complete, continuing..."
Cross-Model Review
Use different models to check each other's work:
# Implementation with Claude
claude --prompt "$(cat prompts/implement.md)"
# Review with different perspective
claude --prompt "
Review the code in src/auth/.
Look for:
- Security vulnerabilities
- Edge cases not handled
- Performance issues
Be critical. Find problems.
"
Real-World Example: Building an E-Commerce Feature
Let me walk through a real example: adding a product catalog to an e-commerce site.
Step 1: Orchestrator Creates Tasks
claude --print "
Read docs/specs/product-catalog.md
Create tasks for each subagent:
- Database: Product schema, categories, images
- API: Product CRUD, search, filtering
- Frontend: Product list, detail view, search UI
- Test: All test coverage
Write tasks to tasks/[subagent]/
"
Step 2: Database Subagent Executes
claude --print "
You are the database specialist.
Read tasks/database/product-schema.md
Implement:
1. Product table with all fields
2. Category table with hierarchy
3. Product-Category many-to-many
4. Image storage references
5. Proper indexes for search
Validate with 'npm run db:validate'
Write status to tasks/database/product-schema.status
"
Step 3: API Subagent Executes
claude --print "
You are the API specialist.
Read tasks/api/product-endpoints.md
Read the database schema from prisma/schema.prisma
Implement:
1. GET /products (with filtering, pagination)
2. GET /products/:id
3. POST /products (admin only)
4. PUT /products/:id (admin only)
5. DELETE /products/:id (admin only)
Test with 'npm run test:api'
Write status to tasks/api/product-endpoints.status
"
Step 4: Results Merged
The orchestrator reads all status files and verifies the integration works end-to-end.
Resources
Awesome Claude Code Subagents
The community has built a library of specialized subagents for common tasks:
Categories include:
- Database specialists (PostgreSQL, MongoDB, Redis)
- Framework experts (Next.js, FastAPI, Django)
- Language specialists (TypeScript, Python, Rust)
- Domain experts (Security, Performance, Accessibility)
Building Your Own
Start with these questions:
- What domains exist in your project?
- What expertise does each domain require?
- What files does each domain touch?
- What commands does each domain use?
Build a prompt file for each domain. Start simple and refine based on results.
When to Use Subagents
Good Fit
- Large projects with multiple domains
- Teams with established conventions per domain
- Projects requiring specialized expertise
- Parallel work opportunities
Overkill
- Small projects (just use one Claude Code instance)
- Single-domain work
- Exploration/prototyping phase
- When context fits comfortably in one instance
The Bigger Picture
Subagents represent a shift in how we think about AI-assisted development. Instead of one omniscient AI, we have a team of specialists coordinated by an orchestrator.
This mirrors how human teams work. And as AI capabilities grow, the subagent pattern scales: more specialized agents, better coordination, complex projects decomposed into manageable pieces.
At sightful, I use subagent patterns for larger client projects. The orchestrator is me—understanding requirements, breaking down work, validating results. The subagents handle implementation in their domains.
It's a powerful model for building software efficiently.
Ready to explore how subagent architecture could work for your project? Book a free consultation and let's discuss your specific needs.
Weekly Insights on Building with Claude Code
Get practical tips on AI-assisted development, Claude Code patterns, and building software faster.
No spam. Unsubscribe anytime.