Beginner

How Sub Agents Work

Understand the complete lifecycle of a sub agent - from spawning to result delivery. Learn about context passing, tool access, memory isolation, and execution modes.

The Agent Spawning Lifecycle

Every sub agent goes through a predictable lifecycle from creation to completion:

  1. 1. Task Identification

    The parent agent recognizes that a task would benefit from delegation. This could be a research task, a coding task, or any work that can be scoped independently.

  2. 2. Agent Creation

    The parent spawns a new agent instance with a specific prompt describing the task, the expected output format, and any relevant context.

  3. 3. Execution

    The sub agent works autonomously, using its available tools to complete the task. It may read files, search code, write implementations, or run commands.

  4. 4. Result Return

    When finished, the sub agent returns its results to the parent. This typically includes a summary of findings, code changes made, or recommendations.

  5. 5. Cleanup

    The sub agent's context is discarded. The parent receives only the final output, keeping its own context lean and focused.

Lifecycle Flow
Parent Agent
    |
    v
[Identify Task] -- "I need to understand the auth module"
    |
    v
[Spawn Sub Agent] -- Creates agent with task prompt
    |
    v
[Sub Agent Works] -- Reads files, searches, analyzes
    |
    v
[Results Return] -- "Auth uses JWT with refresh tokens..."
    |
    v
Parent continues with knowledge

Context Passing

When a parent agent spawns a sub agent, it must pass enough context for the sub agent to do its job effectively. This context typically includes:

  • Task description: A clear, specific prompt describing what the sub agent should accomplish.
  • Relevant background: Key information the sub agent needs to know (e.g., project structure, coding conventions, constraints).
  • Expected output format: How the sub agent should structure its response so the parent can easily use it.
Example: Context Passed to a Research Agent
// Parent agent creates a sub agent with this prompt:

"Search the codebase for all authentication-related
files. Look for:
1. Where JWT tokens are created and validated
2. How refresh tokens are handled
3. Any middleware that checks auth status
4. Rate limiting on auth endpoints

Return a structured summary with file paths and
key findings for each area."
Best practice: The quality of a sub agent's output is directly proportional to the quality of the context it receives. Be specific about what you need, include relevant constraints, and define the expected output format.

Tool Access Per Agent Type

Not all sub agents get the same tools. Tool access is restricted based on the agent's role to enforce safety and prevent unintended side effects:

Agent Type Tools Granted Rationale
Research / Explore Read, Glob, Grep Read-only access prevents accidental modifications while allowing full codebase exploration
Planning Read, Glob, Grep Plans should be based on understanding the code, not changing it
Coding Read, Write, Edit, Bash, Glob, Grep Full access needed to implement changes, run builds, and verify results
Testing Read, Bash, Glob, Grep Can run tests and read code but typically should not modify source files

Result Aggregation

After sub agents complete their work, the parent agent must aggregate their results into a coherent whole. This process involves:

  • Collecting outputs: Gathering the text responses from each sub agent.
  • Resolving conflicts: If multiple agents touched related areas, the parent checks for conflicts or inconsistencies.
  • Synthesizing: Combining individual results into a unified response or action plan.
  • Reporting: Presenting the aggregated results to the user in a clear, organized format.

Memory Isolation vs Shared Context

A critical design decision in sub agent systems is how much context is shared between agents:

Memory Isolation

Each sub agent has its own independent context. It cannot see what other sub agents are doing or have done. This is the default in most implementations.

  • Prevents context pollution
  • Keeps each agent focused
  • Simpler to reason about
  • No cross-agent dependencies

Shared Context

Agents can access shared state, such as a file system or shared memory. Changes made by one agent are visible to others.

  • Enables collaboration
  • Agents build on each other's work
  • More complex coordination
  • Risk of conflicts
💡
In practice: Most sub agent implementations use memory isolation for the agent's conversation context but share the file system. This means agents cannot read each other's thoughts, but they can see each other's file changes (if they look).

Background vs Foreground Execution

Sub agents can run in two modes:

Foreground (Blocking)

The parent agent waits for the sub agent to finish before continuing. This is appropriate when the parent needs the sub agent's results to decide what to do next.

Foreground Execution
// Parent waits for result before continuing
result = spawn_agent("Research the auth module")
// Parent can now use the result
plan = make_plan(result)

Background (Non-Blocking)

The sub agent runs independently while the parent continues with other work. The parent checks for results later. This enables true parallel execution.

Background Execution
// Parent spawns multiple agents and continues
agent_a = spawn_background("Research auth module")
agent_b = spawn_background("Research database layer")
agent_c = spawn_background("Research API routes")

// Parent does its own work while agents run
// Results collected when agents finish
Watch out: Background agents that modify files can create conflicts if they work on overlapping areas. Use background mode primarily for read-only research tasks, or ensure agents work on completely separate files.

Ready to Go Deeper?

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