Beginner

Introduction to Sub Agents

Discover what sub agents are, how the parent-child agent relationship works, and why modern AI tools use them to tackle complex tasks efficiently.

What Are Sub Agents?

Sub agents are specialized AI agents that are spawned by a parent agent to handle specific subtasks autonomously. Think of them as assistants to the assistant - when a primary AI agent encounters a complex problem, it can delegate parts of that problem to child agents that focus on individual pieces.

This pattern is used extensively in modern AI tools like Claude Code CLI, Gemini CLI, and other agentic development environments. Instead of one agent trying to do everything sequentially, work is divided among specialists.

💡
Key concept: A sub agent is not a different AI model - it is a separate instance of an agent with its own context, instructions, and tool access. The parent agent creates it, gives it a task, and collects its results.

The Parent-Child Agent Relationship

The relationship between a parent agent and its sub agents follows a clear hierarchy:

Agent Hierarchy
Parent Agent (main conversation)
  |
  |-- Sub Agent A (research task)
  |     |-- Reads files
  |     |-- Searches codebase
  |     |-- Returns findings
  |
  |-- Sub Agent B (coding task)
  |     |-- Writes implementation
  |     |-- Runs tests
  |     |-- Returns code changes
  |
  |-- Parent aggregates results

The parent agent maintains the overall conversation context and orchestrates the workflow. Each sub agent operates in its own isolated context, focused solely on the task it was given.

Why Sub Agents?

There are several compelling reasons to use sub agents instead of having one agent do everything:

  1. Divide Complex Tasks

    Large tasks can be broken into smaller, well-defined subtasks. Each sub agent handles one piece, making the overall problem more manageable and the results more reliable.

  2. Parallel Execution

    Multiple sub agents can run at the same time. While one agent researches the codebase, another can be writing code, and a third can be running tests - dramatically reducing total completion time.

  3. Specialized Roles

    Each sub agent can be given specific instructions, tools, and constraints tailored to its task. A research agent gets read-only file access while a coding agent gets write access.

  4. Context Isolation

    Sub agents work within their own context window, preventing information overload. The parent agent does not need to hold every detail - it delegates and receives summaries.

Real Examples from Claude Code

Claude Code CLI uses sub agents extensively. Here are the primary agent types it spawns:

Agent Type Purpose Tools Available
Explore Agent Searches and reads files to understand the codebase Read, Glob, Grep
Plan Agent Analyzes tasks and creates implementation plans Read, Glob, Grep
General-Purpose Agent Handles coding, testing, and other complex work Read, Write, Edit, Bash, Glob, Grep
Example in action: When you ask Claude Code to refactor a large module, it might spawn an Explore agent to map all the files involved, then spawn a coding agent to make changes, and finally spawn a test agent to verify nothing broke - all coordinated by the parent agent.

Sub Agents in Other Tools

The sub agent pattern is not limited to Claude Code. It appears across the AI tool ecosystem:

  • Gemini CLI: Uses agent delegation for complex multi-step tasks, spawning specialized workers for research and code generation.
  • OpenAI Assistants API: Supports creating multiple assistant instances that can be orchestrated by application code to work on different aspects of a problem.
  • LangChain / LangGraph: Provides agent frameworks where parent agents route tasks to specialized sub-agents based on the type of work needed.
  • AutoGen (Microsoft): Multi-agent conversation framework where agents can be composed into hierarchies with manager and worker patterns.
  • CrewAI: Role-based agent framework where crews of agents collaborate, each with specific roles, goals, and tool access.
Important: Sub agents consume additional API tokens and cost. Each sub agent spawned is essentially a new conversation with the AI model. Understanding this cost model is essential - we cover it in detail in the Best Practices lesson.

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.