AI Coding Agent Architecture: Why Minimal Loops and Main/Sub-Agent Design Work

    Large language models have turned coding assistants from simple completion tools into agents that can understand requirements, edit code, run commands, and execute tests. This article examines why production coding agents often favor a simple single-agent loop and a two-level main/sub-agent structure over elaborate multi-pattern designs such as ReAct, Plan-and-Execute, Reflexion, and free-form multi-agent collaboration. It explains the engineering trade-offs, the role of context isolation and parallel subtasks, key implementation details around tools, context management, system prompts, and safety, and the principles these choices suggest for building reliable agent systems.

    In recent years, as large language model capabilities have improved rapidly, model-based coding assistants have evolved from simple code completion into agents that can autonomously understand requirements, modify code, execute commands, and run tests. Around agent implementation, the technical community has identified many design patterns, such as ReAct (alternating reasoning and action), Plan-and-Execute (plan first, then execute), Reflexion (self-reflection), and multi-agent collaboration. Yet observing the actual implementations of mainstream coding tools reveals an interesting contrast: their internal architectures often do not rely on complex combinations of multiple patterns. Instead, they use a very plain core loop, supplemented by a two-level main/sub-agent structure. This choice to “simplify” contains substantial engineering wisdom and deserves closer analysis.

    1. Why Complex Multi-Pattern Designs Do Not Dominate in Practice

    1.1 Theoretical Appeal and Engineering Cost of Multi-Pattern Approaches

    Take ReAct as an example. Its core idea is to have the model alternate between “thinking” and “acting” at each step, with action results fed back to the model to form a reasoning chain. Plan-and-Execute first decomposes a task into a plan list, then executes items one by one, with the plan dynamically revised during execution. These patterns perform well in papers and demos, but in real engineering environments they present several problems:

    • Control-flow uncertainty: An explicitly designed multi-stage state machine implies a large amount of branch logic. An abnormal output at any step can cause the flow to deviate, requiring additional state validation, retry, and rollback code.
    • Context bloat: Each additional pattern requires injecting more system prompts, format constraints, and intermediate artifacts into the model. Under long tasks, the context is quickly filled, which can instead harm output quality.
    • Error cascading: Deviations in the planning stage are amplified by the strict execution stage. When the planner and executor have inconsistent understandings of the task, there is no natural channel for correction.

    1.2 The Essence of the Minimal Loop: Give Complexity to the Model, Not the Framework

    The implementation core of mainstream coding tools can often be summarized as a “single agent loop”:

    1. while task is not complete:
    2. response = model(system_prompt + message_history + tool_definitions)
    3. if response contains tool_call:
    4. result = execute_tool(response.tool_call)
    5. append result to message_history
    6. else:
    7. output final answer and exit loop

    This loop looks no different from the most primitive tool-calling paradigm, but its power comes from three points. First, model capabilities are already strong enough to autonomously complete an implicit ReAct of “think—decide—act—observe” within one message history, without the framework explicitly modeling it. Second, when the toolset is well designed (reading and writing files, executing commands, searching code), the model can compose arbitrarily complex workflows. Third, less is more: the thinner the framework, the more room the model has for autonomous operation, and the fewer opportunities there are for framework-level errors.

    2. Main/Sub-Agent Two-Level Architecture: Engineering Task Decomposition

    2.1 Division of Responsibilities in the Two-Level Structure

    On top of the single loop, the two-level architecture introduces a clear separation of responsibilities:

    • Main agent: User-facing. It is responsible for understanding requirements, maintaining global context, and making high-level decisions (which files to modify, which tests to run, when to finish). It is the only entry point for dialogue with the user, ensuring interaction consistency.
    • Sub-agent: Spawned by the main agent when needed. It receives a self-contained subtask description (for example, “search the codebase for all locations that call a certain function and summarize them”), runs the same base loop independently, and returns only a result summary to the main agent when finished.

    A sub-agent is essentially a “one-shot loop instance with an independent context window.” The main agent creates it through a tool call similar to “dispatch a subtask,” which is no different at the protocol level from calling other tools and is very natural to implement.

    2.2 What Problems This Design Solves

    Context isolation and compression. Code exploration tasks (global search, reading multiple files) generate a large amount of intermediate information. If all this information remains in the main loop, it will quickly consume context and dilute key information. A sub-agent digests the raw details in its own context and returns only conclusions, equivalent to a structured information compression.

    Parallelization. Multiple independent subtasks (for example, locating problems in multiple modules at the same time) can be dispatched to multiple sub-agents for concurrent execution, with the main agent collecting results at a join point. This is far more efficient than serial exploration within a single loop.

    Single responsibility and testability. Sub-agent prompts can be customized for task types (retrieval, refactoring, testing), localizing the impact of prompt engineering and reducing tuning costs.

    2.3 Comparison with Multi-Agent Collaboration Patterns

    Compared with free-form conversational multi-agent collaboration (multiple peer agents discussing with one another), the two-level structure uses a strict tree topology: the main agent is the only decision and aggregation node, and sub-agents do not communicate directly. This avoids problems common in multi-agent systems, such as “discussion divergence,” “diffused responsibility,” and “difficulty converging.” All coordination cost is concentrated in the main agent, making control flow clear and predictable.

    3. Key Engineering Details Behind the Minimal Architecture

    Under the minimal top-level design, actual results depend heavily on a series of solid engineering details:

    1. Toolset design: Tool granularity should fit coding scenarios—file reading, targeted editing, command execution, code search, and so on. Editing tools should support precise replacement rather than whole-file rewriting to reduce the risk of damage. Command execution needs to be paired with a permission confirmation mechanism for safety.
    2. Context management strategy: For long tasks, message history needs to be summarized and compressed, retaining key decisions and results while discarding redundant process information, so the loop can run indefinitely without “forgetting.”
    3. System prompt quality: Under a minimal framework, much of the “intelligence” comes from behavioral conventions in the system prompt (how to search, when to confirm, how to report). The prompt itself is the most important “code.”
    4. Safety boundaries: Command execution and file writes must have sandboxing, confirmation, and rollback mechanisms. This is the foundation of trust for coding agents.

    4. Implications for Developers

    Several broadly applicable agent engineering principles can be extracted from these implementations: prioritize trusting the model’s autonomous reasoning ability, and keep the framework thin rather than thick; use structured task decomposition (two-level dispatch) instead of complex state machines; design context management as a first-class problem; and recognize that tool quality determines the ceiling of the agent. For teams building intelligent applications on the cloud, these principles apply whether they are building agents on large model services and function computing, or using cloud platform logging services and monitoring/alerting systems to ensure observability. The maturity of agent engineering is not marked by a wide variety of patterns, but by using the simplest structure to reliably solve the most complex problems.