How Sub-Agents Are Spawned in AI Coding Tools: Runtime Architecture and Design Patterns

    This article examines how production-grade AI coding tools implement sub-agent spawning at the runtime architecture level. It explains why a sub-agent is better understood as an independent runtime instance rather than a function call, describes the layered Session and Thread design, outlines the control plane and registry used to manage an agent tree, and walks through the complete spawn flow. It also compares three common implementation approaches—function-call style, independent-instance style, and external-orchestration style—and offers practical guidance for developers building multi-agent applications.

    When discussing how AI coding tools work internally, a common question is: how do different tools differ in the technical path they use to implement sub-agent spawning? Answering this question requires going beyond surface-level feature comparisons and looking at the runtime architecture: what a “sub-agent” actually is in engineering terms, how it is created, and how it cooperates with a parent agent. This article uses a typical multi-agent implementation approach from the industry as an example, breaks down its internal mechanisms, and summarizes reusable general design patterns.

    Sub-agents Are Independent Runtime Instances, Not Function Calls

    Many developers encountering multi-agent concepts for the first time assume that dispatching a sub-agent simply means the parent agent makes an additional model request during reasoning or calls a wrapped function. This understanding may barely hold in simple scenarios, but it is not accurate for production-grade AI coding tools.

    In mature multi-agent architectures, a sub-agent is closer to a complete, independently runnable agent instance. It has its own thread, Session, context, internal state, event stream, and full lifecycle. This means:

    First, a sub-agent has its own message history rather than sharing the same conversation record with the parent agent. Whether parent and child inherit context is an explicit design decision, not default behavior.

    Second, a sub-agent can independently call tools, initiate model requests, and produce event output. Its execution is not blocked by the parent agent’s reasoning loop unless the parent explicitly waits.

    Third, a sub-agent has its own lifecycle states, including creation, running, waiting, completed, and closed. These states need to be tracked and managed by a unified control plane.

    This point is critical because it determines where system complexity is distributed: the hard part of a multi-agent system is not “making the model perform one more reasoning step,” but managing a set of concurrently running, stateful, long-lived execution entities.

    Core Runtime: Layered Session and Thread Design

    In this type of implementation, each agent has a Session behind it as its runtime container. The Session carries everything the agent needs at runtime: context management, input processing, tool-call scheduling, model request sending, event stream output, and state maintenance. A Session can be understood as the “operating system process” of an agent—it is the carrier of resources and state.

    Thread is the externally exposed wrapper layer. Internally, a Thread holds a reference to the actual agent runtime instance and provides operation interfaces externally. This abstraction exists because upper-level scheduling logic—such as the parent agent’s spawn action—should not directly manipulate low-level Session details. Instead, it should control the agent through a stable handle such as Thread. This “container plus handle” layering is very similar to the relationship between a process and a process handle in an operating system, and it is a classic approach to managing long-lived tasks.

    Control Plane Design: Unified Management of the Agent Tree

    In multi-agent scenarios, agents do not exist in isolation. They are organized into a tree: each agent can further spawn its own sub-agents, forming a hierarchical structure. Managing this tree requires a centralized control plane component, which typically has the following responsibilities:

    1. Spawn: receive spawn requests, validate parameters, and trigger the creation process for a new agent.
    2. Send: deliver messages and tasks between parent and child agents.
    3. Wait: allow a parent agent to block and wait for a child agent’s execution result.
    4. Status query: maintain metadata for the entire agent tree and support querying the runtime status of any node.
    5. Close: reclaim completed or abnormal child agents and release the sessions and computing resources they occupy.

    Alongside the control plane is an in-memory registry responsible for recording the topology of the agent tree and the metadata of each node, such as creator, creation time, task description, and current state. The control plane handles “actions,” while the registry handles the “ledger.” Separating the two makes independent evolution and testing easier.

    The Complete Spawn Flow

    Connecting the modules above, a complete sub-agent spawn flow looks roughly like this:

    Step 1: During reasoning, the parent agent decides that a task needs to be split and initiates a request through a built-in spawn tool. The request includes parameters such as task description, required capabilities, and context inheritance strategy.

    Step 2: The control plane receives the request, registers a new node in the registry, and establishes the parent-child relationship.

    Step 3: The thread manager creates new Thread and Session instances and completes initialization of the runtime environment, including toolset assembly, permission configuration, and event channel setup.

    Step 4: Based on spawn parameters, the system decides whether to fork context from the parent agent’s history. This involves a key component: persistent history, referred to in some implementations as rollout. It records the complete event sequence of an agent from startup to the present. Forking context essentially copies or truncates a portion of the parent agent’s persistent history and uses it as the child agent’s initial input. This mechanism serves two purposes: initializing the child agent’s context and supporting fault recovery—any agent can reconstruct its state from the history record.

    Step 5: Through the inter-agent communication channel, the specific task is delivered to the sub-agent, and the sub-agent enters its own reasoning-action loop.

    Step 6: The parent agent can choose to continue its own work in asynchronous mode, or call wait to wait for the sub-agent’s result in synchronous mode. When the sub-agent completes, the result is returned through the communication channel, and the control plane updates status and eventually reclaims resources.

    Comparison with Other Implementation Paths

    Returning to the original question: where do different AI coding tools differ in their implementation paths? From the perspective of architectural abstraction, they can be roughly grouped into three routes:

    The first is “function-call style”: a sub-agent is merely a model request within the parent agent’s tool call, with no independent state. This is suitable for short, simple subtasks but cannot handle complex work that is long-running and parallelizable.

    The second is “independent-instance style”: the route described in detail in this article. The sub-agent has a complete runtime and supports fine-grained control such as concurrency, inheritance, waiting, and closing. It offers the strongest capabilities but also the highest engineering complexity, requiring solutions for resource scheduling, state consistency, fault recovery, and related problems.

    The third is “external-orchestration style”: collaboration between agents is driven by an external workflow engine, while the agents themselves remain stateless. This route moves complexity outward and is suitable for integration with existing cloud-native infrastructure such as container platforms, message queues, and task scheduling services. However, flexibility is limited by the orchestration engine’s expressive capability.

    Practical Recommendations

    For developers who want to build multi-agent applications, this article offers several recommendations. First, clarify whether your subtasks require independent state and concurrency capabilities; this determines which route to choose. Second, regardless of the route, pay attention to the design of persistent history, because it is the foundation for context inheritance and fault recovery. Finally, the separation of control plane and registry, along with the layering of Thread and Session, are proven reliable patterns worth reusing in your own system. For enterprise deployment, general cloud capabilities can also help—for example, using container platforms to host agent runtimes, message queues for inter-agent communication, and logging services with monitoring and alerting for observability—to reduce the cost of building infrastructure yourself.

    The essence of a multi-agent system is turning “one smart brain” into “an organized team.” Understanding the thread, session, and control plane design behind the spawn mechanism means mastering the methodology for assembling that team.