Sub-Agents
Sub-agents are parallel child processes spawned by a main AI coding agent to handle independent parts of a complex task simultaneously. Instead of processing everything sequentially, the main agent delegates sub-tasks to specialized child agents that run in parallel and report results back to the parent.
How sub-agents work
When a main agent encounters a task with multiple independent parts, it can spawn sub-agents to handle them in parallel. For example, if you ask "refactor the authentication, payment, and notification modules to use the new error handling pattern," the main agent might spawn three sub-agents—one for each module. Each sub-agent reads the relevant files, makes changes, and runs tests independently. The main agent collects results and ensures consistency across the changes.
When sub-agents are useful
- +Large refactoring tasks that span multiple independent modules
- +Researching different parts of a codebase simultaneously
- +Running multiple analysis tasks (security audit, performance check, style review) in parallel
- +Generating code for multiple independent components at once
- +Investigating complex bugs that may have multiple root causes
Sub-agents are most effective when the sub-tasks are truly independent—they do not modify the same files or depend on each other's output. When tasks have dependencies, sequential execution by a single agent is more appropriate to avoid conflicts.
Claude Code supports sub-agents through its Task tool. The main agent can spawn child agents with specific instructions and scoped permissions, then synthesize their results into a coherent response.
Do sub-agents share context with the main agent?+
How many sub-agents can run at once?+
Can sub-agents spawn their own sub-agents?+
When should I avoid using sub-agents?+
Master Claude Code in days, not months
37 hands-on lessons from beginner to CI/CD automation. Module 1 is free.
START FREE →