Welcome back, engineers. Imagine starting your day: you open up the terminal, fire up Claude Code, then kick off a single prompt /cook that does the work it used to take you hours in minutes.
You're able to accomplish this with Claude Code sub-agents. You've created workflows of specialized agents that do one thing and do it extraordinarily well.
But here's where it gets wild: My agents are building my agents.
Code is a commodity. Your fine-tuned prompts can be valuable. And now your Claude Code sub-agents can yield extreme value for your engineering if you know how to avoid the big two mistakes engineers are making with sub-agents.
Understanding Sub-Agent Architecture
First things first: sub-agents don't work like you think they work. Let me explain.
┌─────────┐ ┌───────────────┐ ┌──────────────┐
│ YOU │ ───► │ PRIMARY AGENT │ ───► │ SUB-AGENT │
│ │ │ │ │ │
│ │ ◄─── │ │ ◄─── │ │
└─────────┘ └───────────────┘ └──────────────┘
│ │
│ ┌──────────────┐│
└────► │ SUB-AGENT ││
│ ││
└──────────────┘│
└──────────────┘
Key insight: Sub-agents respond to your PRIMARY agent, not to you!This flow is absolutely critical. You prompt your primary agent, and then your primary agent prompts individual sub-agents based on your original prompt. Your sub-agents respond not to you—they respond to your primary agent.
Big Mistake #1: Many engineers miss this fact, and it changes the way you write your sub-agent prompts. What you're writing is the system prompt of your sub-agent, not the user prompt.
The Anatomy of a Sub-Agent
Let's break down exactly what Claude Code sub-agents look like:
# agents/hello-world.yaml
name: hello-world
description: |
If they say "hi claude" or "hi CC" or "hi claude code", use this agent.
This agent provides a friendly greeting with current tech news.
tools:
- bash
- read_file
color: yellow
# The actual prompt (system prompt for the sub-agent)
prompt: |
## Purpose
You are a friendly greeting agent that responds with warmth and current tech insights.
## Instructions
1. Greet the user warmly
2. Share one interesting tech fact or news
3. Ask how you can help
## Report Format
IMPORTANT: Respond to the primary agent with:
"Please tell the user: [your message here]"
Remember: You're communicating with Claude (primary agent), not the user directly.Critical Understanding
The prompt you write here is the system prompt of your sub-agent. This is not what triggers the agent—it's what defines its behavior. Your primary agent will prompt this sub-agent based on the description field.
# In your terminal
$ claude
> Hi CC!
# What happens internally:
1. Primary agent reads your input
2. Checks all agent descriptions
3. Finds match: "If they say 'hi CC'..."
4. Invokes hello-world agent
5. Sub-agent executes and reports back
6. Primary agent relays the response to youAvoiding the Two Big Mistakes
Mistake #1: Misunderstanding Communication Flow
# WRONG - Direct user communication
prompt: |
Hi! I'm your friendly assistant.
How can I help you today?
# RIGHT - Primary agent communication
prompt: |
## Report to Primary Agent
When invoked, analyze the request and respond with:
"Please tell the user: [structured response]"
Never assume direct user communication.Mistake #2: Ignoring Context Limitations
Sub-agents start fresh—they have NO context of your conversation history. This is both a feature and a limitation.
# In your sub-agent prompt:
prompt: |
## Context Handling
You have NO prior conversation context.
The primary agent will provide all necessary information.
## Expected Input Format
The primary agent should provide:
- Current task description
- Relevant file paths
- Any necessary context
## Response Protocol
Always acknowledge what context you received and what you're doing with it.Building a Meta-Agent
Now for the exciting part—agents that build agents:
# agents/meta-agent.yaml
name: meta-agent
description: |
PROACTIVELY use this when user asks to "create a new sub-agent" or
"build an agent" or mentions needing automation for specific tasks.
tools:
- write_file
- read_file
- web_fetch
color: purple
prompt: |
## Purpose
Generate complete Claude Code sub-agent configurations from user descriptions.
## Process
1. Fetch latest Claude Code documentation on agents
2. Analyze the user's requirements
3. Design appropriate agent configuration
4. Generate the complete YAML file
## Agent Creation Guidelines
### Name
- Use kebab-case
- Be descriptive but concise
- Example: "code-reviewer", "test-generator"
### Description
- Include CLEAR trigger conditions
- Use phrases like "If they say X" or "When user requests Y"
- Add instructions for the primary agent on how to prompt this agent
### Tools
- Only include necessary tools
- Consider security implications
- Common: bash, read_file, write_file, edit_file
### Prompt Structure
Always include:
1. ## Purpose - What this agent does
2. ## Process - Step-by-step workflow
3. ## Best Practices - Important constraints
4. ## Report Format - How to communicate back
### Important Reminders
- Every word must add value
- No pleasantries
- Focus on efficiency
- Include "IMPORTANT: This agent has no conversation context"Real-World Example: Text-to-Speech Agent
Let's walk through creating a practical agent using our meta-agent:
# Step 1: Identify the problem
Problem: "When doing agentic coding at scale, I lose track of what agents have done"
Solution: "Add text-to-speech to agents so they notify me when done"
# Step 2: Use meta-agent to build solution
$ claude
> Build a new sub-agent that:
> - Summarizes completed work in one sentence
> - Uses 11Labs text-to-speech to speak the summary
> - Triggers when I say "tts summary" or after major tasksThe meta-agent creates:
# agents/work-completion-summary.yaml
name: work-completion-summary
description: |
If they say "tts", "tts summary", use this agent.
When you prompt this agent, describe exactly what work was completed.
IMPORTANT: This agent has no context of conversations between you and user.
tools:
- bash
- eleven_labs_tts
- play_audio
color: green
prompt: |
## Purpose
Provide concise audio summaries of completed work using text-to-speech.
## Process
1. Extract key accomplishment from input
2. Create one-sentence summary (max 15 words)
3. Generate speech using 11Labs
4. Play audio notification
## Best Practices
- IMPORTANT: Run only bash, pwd, and 11Labs MCP tools
- Keep summaries under 15 words
- Focus on WHAT was done, not how
- Use active voice
## Report Format
Respond with: "Audio summary delivered: [summary text]"Advanced Patterns
Pattern 1: Chained Sub-Agents
// In your custom command
const workflow = {
stage1: "analyze-codebase", // First sub-agent
stage2: "generate-tests", // Second sub-agent
stage3: "run-validation", // Third sub-agent
stage4: "create-pr" // Final sub-agent
};
// Primary agent orchestrates the chain
// Each agent's output feeds into the nextPattern 2: Specialized Codebase Agents
name: auth-system-expert
description: |
Use for ANY questions or changes related to authentication,
login, JWT tokens, or user sessions.
This agent has deep knowledge of our auth patterns.
prompt: |
## Specialized Knowledge
You are an expert in this codebase's authentication system:
- JWT implementation in src/auth/jwt.ts
- Session management in src/auth/sessions.ts
- OAuth providers in src/auth/providers/
## Always Check
1. Current auth flow before changes
2. Security implications
3. Existing test coveragePattern 3: Context Injection
# In primary agent's description field
description: |
When prompting this agent, always provide:
1. Current file path
2. Recent changes made
3. Overall goal
4. Any errors encountered
Format as JSON:
{
"context": {
"currentFile": "path/to/file",
"recentChanges": ["change1", "change2"],
"goal": "refactor authentication",
"errors": []
},
"request": "specific task"
}Benefits and Trade-offs
Benefits
- Context Preservation: Each sub-agent operates in its own clean context
- Specialized Expertise: Fine-tune instructions and tools per agent
- Reusability: Store agents in your repo for team use
- Flexible Permissions: Lock down tools per agent
- Focused Performance: Single-purpose agents make fewer mistakes
- Simple Multi-Agent Orchestration: Build complex workflows easily
Trade-offs
- No Context History: Sub-agents start fresh every time
- Hard to Debug: Limited visibility into sub-agent operations
- Decision Overload: Too many agents confuse the primary agent
- Dependency Coupling: Changes to one agent can break workflows
- No Nested Sub-Agents: Can't call sub-agents from sub-agents
Pro Tip: Start with 3-5 highly focused agents. Add more only when you have clear, distinct use cases. Quality over quantity.
Best Practices
1. Clear Trigger Conditions
2. Explicit Communication Protocols
3. Tool Minimization
4. Description as Documentation
Scaling Your Sub-Agent System
As you build more agents:
-
Organize by Domain
-
Version Control
- Track agent changes in git
- Document why agents were created
- Review agent performance regularly
-
Monitor Usage
- Which agents get called most?
- Which ones fail often?
- Where are the gaps?
-
Continuous Improvement
- Update descriptions based on misfires
- Refine prompts based on outputs
- Consolidate similar agents
The Meta-Agent Advantage: Once you have a meta-agent, creating new specialized agents takes minutes, not hours. You're not just automating tasks—you're automating automation itself.
The Future is Multi-Agent
We're moving from:
- Single agent → Multiple specialized agents
- Manual coordination → Automated orchestration
- Static tools → Self-improving systems
Sub-agents are your building blocks. Meta-agents are your architects. Together, they create systems that evolve with your needs.
Start simple: Create one sub-agent for a repetitive task. Then create a meta-agent. Then watch as your AI agents start building themselves.
The future isn't just about AI doing work—it's about AI systems that improve themselves. And with Claude Code sub-agents, that future is here.
Welcome to the age of self-building AI.