Architecture

Sub-Agents

Specialized AI agents spawned by a primary agent to handle specific tasks, enabling complex workflows through delegation and parallel execution.

What are sub-agents?

Sub-agents are specialized AI agents that are spawned by a primary agent to handle specific tasks. Instead of one agent doing everything, work is delegated to focused sub-agents that excel at particular capabilities.

Think of it like a manager delegating to specialists:

  • Primary agent: Understands the overall goal, coordinates work
  • Sub-agents: Handle specific tasks, report results back

This pattern enables complex workflows while keeping each agent's role simple and focused.

Why use sub-agents?

Separation of concerns Different tasks require different contexts, tools, and personalities.

Parallel execution Multiple sub-agents can work simultaneously, completing tasks faster than sequential processing.

Reduced complexity Each agent has a narrow scope, making prompts shorter and behavior more predictable.

Specialized optimization Use different models, context, and tools for each sub-agent based on their specific task.

Error isolation If a sub-agent fails, it doesn't crash the entire system. The primary agent can retry or use alternatives.

Sub-agent patterns

Coordinator pattern One primary agent coordinates multiple specialized sub-agents:

  • Research Sub-Agent → Search and return findings
  • Analysis Sub-Agent → Compare and summarize
  • Writing Sub-Agent → Draft final document

Pipeline pattern Sub-agents process work in sequence: Extract → Transform → Generate

Fan-out pattern One task spawns multiple parallel sub-agents (e.g., summarize 10 documents simultaneously)

Expert panel pattern Multiple sub-agents provide diverse perspectives that the primary agent synthesizes

Implementing sub-agents

With Clawdbot/OpenClaw Use sessions_spawn to create sub-agents that run independently and report results back.

With Claude Code Create sub-agent files in your project (researcher.md, coder.md, reviewer.md) and reference them in workflows.

With frameworks (LangChain, CrewAI) Define agent roles, goals, and backstories, then create crews that coordinate them.

Best practices

Define clear interfaces Specify exactly what each sub-agent receives and returns.

Keep sub-agents focused One sub-agent = one responsibility.

Handle failures gracefully Detect failures, retry with different parameters, fall back to alternatives.

Monitor usage Track how often each sub-agent is used, success/failure rates, time and cost.

Test independently Each sub-agent should work correctly in isolation before integrating into multi-agent systems.