PTENES
4.1 ~45 min Β· Intermediate

πŸ€– Subagents: Delegating Tasks

Learn to delegate complex tasks to parallel Claude Code instances, multiplying your capacity without increasing wait time.

Orchestrator Claude Code πŸ” Explore reading/analysis βš™οΈ General full access πŸ“š Research searches the docs πŸ’» Coding generates code Result aggregated 4 subagents in parallel β€” 4Γ— faster results

🧠 What is a subagent

One subagent is an independent instance of Claude Code launched by the main agent (orchestrator) to perform a specific task. It receives a well-defined prompt, runs with its own tools, and returns a summarized result.

The orchestrator uses the internal tool Task to create subagents. Each Task is isolated: it has its own conversation history, file context, and configured permissions.

πŸ’‘ Core concept

The subagent doesn’t share memory with the orchestrator. It only knows what’s in the prompt it received. This is a feature β€” isolation ensures one subagent doesn’t contaminate another’s context.

Subagent life cycle

1

The orchestrator formulates the task

Define the prompt with a clear scope, expected output format, and required permissions

2

Harness launches the instance

A new isolated session is created with the prompt as its initial context

3

Subagent runs with tools

Uses Bash, Read, WebSearch, and other tools as needed

4

Result returns to the orchestrator

Text compressed with the outputβ€”not the full log, just the response

🎯 When to delegate to a subagent

βœ“ Delegate WHEN

  • βœ“ Task has a well-defined scope and an atomic result
  • βœ“ Can run in parallel with other tasks
  • βœ“ Broad research (multiple sources, multiple files)
  • βœ“ Task repeated N times with different inputs
  • βœ“ Risk isolation (testing dangerous code)

βœ— DON'T delegate WHEN

  • βœ— Task requires access to the orchestrator's history
  • βœ— Result is trivial (2-3 lines of code)
  • βœ— Subagents need to communicate with each other in real time
  • βœ— Token costs outweigh the speed benefit
  • βœ— Task requires human approval midway through the process

πŸ’‘ Practical tip

The quickest criterion: can you describe the task in 1–2 sentences with clear input and output? If so, it’s a candidate for a subagent. If it needs several paragraphs of context, run it in the orchestrator instead.

⚑ Fan-out: the most powerful pattern

Fan-out is when an orchestrator launches N subagents simultaneously, each solving one part of the problem. The orchestrator remains blocked while waiting for them all to finish, then aggregates the results.

A search that would visit 8 sources in sequence (8 Γ— 30s = 4 minutes) becomes 8 parallel subagents (~30 seconds). 8Γ— speed at the same total cost.

// Example prompt for an orchestrator with fan-out
You are the orchestrator. Use the Task tool to create
4 subagents in PARALLEL:

Task 1: Research the official Claude Code documentation on hooks.
         Return: a list of available events with descriptions.

Task 2: Read the project's .claude/settings.json files.
         Return: current configuration structure.

Task 3: Research examples of hooks in public repositories.
         Return: 3 practical examples with code.

Task 4: Analyze the code in src/ to identify
         opportunities for automation with hooks.
         Return: a prioritized list of improvements.

// Wait for ALL of them to finish, then integrate the results.

πŸ“Š Real time savings

Series (4 tasks Γ— 3 min) 12 min
Parallel fan-out (4 agents) ~3 min
Gain 4Γ— faster

⚠️ Practical limit

Speed gains increase with N subagents, but so does cost. For tasks that take 1-2 minutes, fanning out to 4-8 agents is the sweet spot. Beyond 10 agents, coordination overhead reduces the return.

πŸ”¬ Subagent Types: Explore vs General

πŸ” Explore Type

Read-only mode β€” access only to observation tools (Read, read-only Bash, WebSearch). It can't create or modify files.

  • β†’Ideal for research, codebase analysis, and searching docs
  • β†’Safer β€” no risk of accidental modification
  • β†’Cheaper β€” smaller context, fewer tools

βš™οΈ General Type

Full access to tools β€” can read, write, run commands, and interact with external APIs.

  • β†’Ideal for implementation, testing, and deployment
  • β†’Requires carefully configured permissions
  • β†’Use when you need action, not just analysis

🚨 Principle of least privilege

Always use the most restrictive type the task allows. A research subagent with write permission is an unnecessary risk β€” if the model "hallucinates" an instruction, it could accidentally modify files.

πŸ’° Results, cost, and ROI

How the result is returned

A subagent's result reaches the orchestrator as plain text β€” a consolidated summary of what was found/done. The orchestrator receives no tool log, only the final response.

To ensure the result can be consumed, specify the output format in the prompt: Retorne: lista de bullets, Retorne: JSON com campos X e Y, etc.

// Estimated cost per subagent (Claude Sonnet)
Prompt do orquestrador:   ~500 tokens  ($0.003)
ExecuΓ§Γ£o do subagent:     ~2000 tokens ($0.012)
Output para orquestrador: ~300 tokens  ($0.002)
                           ──────────────────────
Total por subagent:        ~$0.017

// Com prompt caching (se CLAUDE.md ou contexto fixo):
Custo cache hit: ~$0.002 (10Γ— mais barato)

// ROI: 1h de trabalho humano = $50-100
// 100 subagents = $1.70 β†’ 30Γ— mais barato que humano

πŸ’‘ Quick ROI calculation

Compare: time saved Γ— the value of your time vs API cost. If you save 1h ($50) and the automation costs $0.50, the ROI is 100Γ—. This applies to any task you do more than once a week.

βœ… Best practices when using subagents

βœ“ What to DO

  • βœ“ Specify the output format in the prompt
  • βœ“ Use the most restrictive type possible
  • βœ“ Limit scope: one responsibility per subagent
  • βœ“ Enable prompt caching for repeated contexts

βœ— What to AVOID

  • βœ— Subagents that depend on each other in real time
  • βœ— Pass the orchestrator's entire history as context
  • βœ— Subagents with write access to the production database
  • βœ— More than 10 parallel subagents without estimating the cost first

🎯 Module 4.1 Summary

βœ“
Subagent β€” isolated Claude Code instance with its own context and permissions
βœ“
Parallel fan-out β€” N simultaneous subagents multiply speed at no extra cost
βœ“
Explore vs. General β€” always use the most restrictive type the task allows
βœ“
Summarized result β€” specify the output format to make aggregation easier
βœ“
Automation ROI β€” compare time saved vs API cost before implementing

Next Module:

4.2 β€” πŸͺ Hooks: automate the harness with PreToolUse, PostToolUse, SessionStart, and Stop