Skip to main content

Blog Post

Self-Building AI: Meta-Agents and Sub-Agent Architecture

•9 min read•By Brandon

Claude CodeSub-AgentsMeta-AgentsAI ArchitectureAutomation

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.

The Flow of Claude Code Agents
┌─────────┐      ┌───────────────┐      ┌──────────────┐
│   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:

Sub-Agent Configuration Structure
# 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.

Sub-Agent Invocation Example
# 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 you

Avoiding the Two Big Mistakes

Mistake #1: Misunderstanding Communication Flow

Wrong vs Right Sub-Agent Communication
# 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.

Context-Aware Sub-Agent Design
# 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:

Meta-Agent for Creating Sub-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:

Creating a TTS Summary 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 tasks

The meta-agent creates:

Generated TTS Summary Agent
# 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

Multi-Stage Agent Workflow
// 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 next

Pattern 2: Specialized Codebase Agents

Domain-Specific Agent
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 coverage

Pattern 3: Context Injection

Rich Context Sub-Agent
# 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

  1. Context Preservation: Each sub-agent operates in its own clean context
  2. Specialized Expertise: Fine-tune instructions and tools per agent
  3. Reusability: Store agents in your repo for team use
  4. Flexible Permissions: Lock down tools per agent
  5. Focused Performance: Single-purpose agents make fewer mistakes
  6. Simple Multi-Agent Orchestration: Build complex workflows easily

Trade-offs

  1. No Context History: Sub-agents start fresh every time
  2. Hard to Debug: Limited visibility into sub-agent operations
  3. Decision Overload: Too many agents confuse the primary agent
  4. Dependency Coupling: Changes to one agent can break workflows
  5. 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:

  1. Organize by Domain

  2. Version Control

    • Track agent changes in git
    • Document why agents were created
    • Review agent performance regularly
  3. Monitor Usage

    • Which agents get called most?
    • Which ones fail often?
    • Where are the gaps?
  4. 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.

I taught before I built, and it still shapes how I explain this work. I build production agentic AI systems and write about what I learn doing it.

Keep going

Continue to the next module in this path.

Next moduleOr get updates by email