Coding Agent Architecture: From Large Language Models to Engineered Programming Agents

    This article explains how coding agents evolved from simple code completion into engineered software agents built around an Agent Harness. It covers the core layers for interaction, context management, planning, tool invocation, state, and memory; the typical execution loop; codebase retrieval strategies; controlled code modification and layered verification; security controls such as isolation, command allowlists, auditing, and human confirmation; common failure modes; and principles for building reliable coding agents that can operate transparently and safely in real software projects.

    In recent years, intelligent programming tools have gradually evolved from “code completion” into engineered agents that can understand tasks, analyze code, execute commands, modify files, and run tests. They do not simply send a user question to a large language model and display the model output. Instead, they add a complete application-layer control system around the model. This system is often called an Agent Harness, which can be understood as an agent runtime framework or agent control layer.

    The value of the Agent Harness is that the large model is responsible for understanding natural language, generating solutions, and reasoning about code, while the application layer provides context, manages tools, controls execution flow, records state, handles exceptions, and ensures that operations remain within safe boundaries. Only when the two are combined does the model gain the ability to handle real software engineering tasks.

    1. From Code Generation to Engineered Task Execution

    Traditional code generation usually follows a request-response pattern. A user enters a requirement, the model returns a code snippet, and the user manually copies, modifies, and verifies it. This approach is suitable for generating isolated functions, but it struggles with complex tasks in real projects.

    Real development tasks often involve multiple steps:

    1. Understand the user requirement and the existing project structure.
    2. Locate relevant modules, configuration files, and test code.
    3. Analyze call relationships and potential impact.
    4. Create a modification plan.
    5. Edit multiple files.
    6. Run builds, static checks, or tests.
    7. Continue fixing issues based on error messages.
    8. Summarize changes and remaining risks.

    Therefore, the essence of an intelligent programming tool is not “letting the model write more code,” but enabling the model to participate in a controlled software engineering process. The model needs to observe the environment, take actions, receive feedback, and adjust subsequent plans based on that feedback.

    This process can be abstracted as:

    1. User requirement
    2. ↓
    3. Task understanding and decomposition
    4. ↓
    5. Codebase retrieval and context construction
    6. ↓
    7. Action plan generation
    8. ↓
    9. Tool invocation and operation execution
    10. ↓
    11. Execution result reading
    12. ↓
    13. Plan correction and continued execution
    14. ↓
    15. Test verification and result summary

    2. Core Components of an Agent Harness

    A relatively complete coding agent typically consists of the following modules.

    1. Interaction Layer

    The interaction layer receives user instructions and displays the agent’s execution process. In addition to ordinary conversational input, it should support displaying:

    • The current task goal;
    • Completed operations;
    • Commands that are about to be executed;
    • File modification summaries;
    • Test and build results;
    • High-risk actions that require user confirmation.

    For developers, transparency is very important. If the system only returns the final result, users will find it difficult to judge whether the model truly understands the task, and they cannot quickly identify errors. Therefore, a good interaction layer usually presents the process as “plan—execute—verify.”

    2. Context Management Layer

    The input window and attention resources of large models are limited, while code repositories often contain many files. An agent cannot fit the entire project into the context at once. Instead, it needs to dynamically select information relevant to the task.

    Context management usually includes:

    • Project directory structure;
    • Key configuration files;
    • Relevant source code;
    • Type definitions and interface declarations;
    • Test files;
    • Build scripts;
    • Version change information;
    • Previous conversation content from the user;
    • Tool execution results.

    The difficulty of context management is not only “finding files,” but also determining which content is worth prioritizing for the model. For example, when modifying an interface, in addition to the interface declaration itself, it may also be necessary to inspect callers, data structures, serialization logic, and corresponding tests.

    A common approach is to perform lightweight retrieval first and then gradually expand the scope based on the retrieval results, rather than reading the entire repository at once. This reduces input cost and also minimizes interference from irrelevant information in the model’s judgment.

    3. Task Planning Layer

    Complex tasks should not be handed directly to the model for free-form execution. Instead, an executable plan should be formed first. The task planning layer is responsible for breaking natural language goals into several steps and clarifying the dependencies between each step.

    For example, “add a data export feature to the system” might be broken down as follows:

    1. Confirm the existing data model.
    2. Find existing query interfaces.
    3. Design the export service.
    4. Add format conversion logic.
    5. Add permission checks.
    6. Add exception handling.
    7. Write tests.
    8. Run builds and verification.

    Planning does not mean that all steps must be determined at once before execution. A more reliable approach is dynamic planning: generate an initial plan first, and adjust it during execution based on actual results.

    For example, the model may initially assume that a certain module exists, but after retrieval it discovers that the project uses a different directory structure. In this case, the system should allow replanning rather than mechanically continuing with subsequent steps.

    4. Tool Invocation Layer

    Tool invocation is the key difference between a Coding Agent and an ordinary chat model. The model itself cannot directly access the file system, run commands, or read test results. It must use controlled tools provided by the application layer.

    Common tools include:

    • File viewing tools;
    • Directory and symbol retrieval tools;
    • File creation and modification tools;
    • Patch application tools;
    • Code formatting tools;
    • Static checking tools;
    • Build tools;
    • Test execution tools;
    • Version diff viewing tools;
    • Documentation query tools.

    Tool invocation usually uses structured parameters, for example:

    1. {
    2. "tool": "read_file",
    3. "arguments": {
    4. "path": "src/service/example.py",
    5. "start_line": 1,
    6. "end_line": 120
    7. }
    8. }

    After receiving the request, the application layer needs to validate parameters, check permissions, execute the operation, and reformat the result into a form the model can understand. The system should not simply expose all system capabilities to the model. Instead, it should follow the principle of least privilege and explicitly restrict accessible directories, executable commands, and modifiable resources.

    5. State and Memory Layer

    When an agent executes a task, it needs to maintain current state, such as:

    • The user’s original goal;
    • Completed steps;
    • The file currently being processed;
    • The most recent tool invocation;
    • Errors returned by tools;
    • Content that has been modified but not yet verified;
    • Pending risks and issues.

    Short-term state supports the current task, while long-term memory can store project conventions, common commands, code style, and team agreements. However, long-term memory must be filtered and access-controlled. All historical content cannot be injected unconditionally into subsequent tasks, otherwise outdated information or incorrect conclusions may be introduced.

    3. Typical Agent Execution Loop

    A general agent execution loop can be represented as:

    1. while the task is not complete:
    2. read current state
    3. determine the next goal
    4. select an appropriate tool
    5. validate tool parameters
    6. execute the tool operation
    7. record the execution result
    8. update context
    9. determine whether replanning is needed

    This loop usually involves four key decisions.

    First, decide “what to do next.” Based on the goal and current evidence, the model needs to choose whether to continue retrieval, modify code, run tests, or ask the user a question.

    Second, decide “which tool to use.” Reading files and running tests are different types of actions. Choosing the wrong tool increases cost and may even lead to unnecessary modifications.

    Third, decide “whether to trust the result.” Tool results may be empty, malformed, or contain errors. The agent needs to combine them with context to determine whether the result is sufficient to support the next action.

    Fourth, decide “when to stop.” Task completion should not depend only on whether the model has generated an answer. It should also consider whether the code builds successfully, whether tests pass, whether changes meet requirements, and whether unresolved risks remain.

    4. Codebase Understanding and Retrieval Strategies

    Codebase understanding is a foundational capability of intelligent programming systems. Relying solely on keyword search is often insufficient, because concepts in real projects may appear under different names.

    More mature retrieval strategies usually combine multiple layers:

    1. Structural Retrieval

    First inspect the directory structure, module boundaries, and main configuration files to build an overall map of the project. This step helps avoid working in the wrong directory.

    2. Text Retrieval

    Search for relevant content by function names, class names, configuration keys, error messages, and business terms. This is suitable for locating explicit associations.

    3. Symbol Retrieval

    Search based on the definition and reference relationships of functions, classes, interfaces, and variables. This helps identify callers and impact scope.

    4. Semantic Retrieval

    When user descriptions do not match code naming, vectorization or semantic matching can be used to find implementations with similar concepts. However, semantic retrieval should only be used to narrow the scope. Final conclusions should still be confirmed through actual code and call relationships.

    5. Incremental Reading

    Initially read only the necessary fragments, and expand the context only after the model discovers new clues. An incremental strategy reduces irrelevant information and improves stability on long tasks.

    5. Code Modification and Verification Mechanisms

    Code modification should not focus only on generation speed; the controllability of changes also matters. The application layer can require the model to submit modifications as patches, structured edits, or explicit file operation descriptions, rather than directly overwriting a large number of files.

    At minimum, the following information should be retained before and after modification:

    • List of changed files;
    • Reason for modifying each file;
    • Scope of added, deleted, and adjusted code;
    • Whether public interfaces are affected;
    • Whether configuration or documentation needs to be updated in sync;
    • Whether tests have been added.

    Verification should also be performed in layers. Typically, formatting checks and syntax checks can be run first, followed by local tests, and finally a more complete build or integration test. This helps catch low-level errors quickly and avoids running high-cost processes for every minor change.

    When a test fails, the agent should not immediately rewrite code blindly. Instead, it should first analyze the failure type:

    • Is it a syntax error?
    • Is a dependency missing?
    • Is there a type mismatch?
    • Have test expectations changed?
    • Is it an environment configuration issue?
    • Or is the implementation logic actually wrong?

    Only by distinguishing the cause of failure can subsequent fixes be more reliable.

    6. Security Controls Are a Prerequisite for Production Deployment

    Because coding agents can read and write files and execute commands, security mechanisms cannot be treated as an optional feature.

    Common control measures include:

    1. Directory isolation: Restrict the agent to a specified workspace.
    2. Command allowlists: Block high-risk commands or require confirmation.
    3. Permission tiers: Use different permissions for reading, modification, deletion, and external access.
    4. Sensitive information protection: Avoid sending keys, tokens, and personal information to the model.
    5. Operation auditing: Record every tool invocation, parameter, and execution result.
    6. Human confirmation: Pause for confirmation in scenarios such as deleting files, modifying dependencies, or accessing external resources.
    7. Change rollback: Preserve version diffs to ensure that erroneous operations can be recovered.

    Prompt injection risk deserves particular attention. Code comments, documentation, or data files may contain content that induces the model to perform unsafe operations. The system should not treat all retrieved text as trusted instructions. It must distinguish between “project data” and “system-level task requirements.”

    7. Common Problems and Improvement Directions

    1. Context Overload Distorts Judgment

    The solution is to use layered summaries, on-demand reading, and result compression, keeping only information directly relevant to the current decision.

    2. Frequent Repeated Operations by the Model

    This can be improved by recording tool invocation history, limiting repetition counts, adding failure classification, and setting staged goals.

    3. Excessive Change Scope

    The model should be required to submit a plan first, then make small batches of changes, and run verification at each stage to avoid changing a large number of unrelated files at once.

    4. Tests Pass but Requirements Are Not Met

    Tests can only prove that some behavior meets expectations; they cannot fully replace requirement verification. Therefore, the system should also check the task checklist, interface compatibility, exception paths, and documentation updates.

    5. Tool Results Are Difficult to Interpret

    Tool output should be formatted and trimmed. For example, for long logs, prioritize errors, stack traces, and key context rather than inserting the entire output verbatim into the model input.

    8. Principles for Building Reliable Coding Agents

    A practical intelligent programming agent should generally follow these principles:

    • Let the large model handle reasoning and let the application layer enforce constraints.
    • Make tool invocations structured, verifiable, and auditable.
    • Give task execution planning, feedback, and retry mechanisms.
    • Keep code modifications small and rollback-capable.
    • Make test verification part of the closed loop.
    • Keep users informed of what the system is doing at all times.
    • Treat high-risk operations as cautious by default rather than allowed by default.

    Overall, a Coding Agent is not a standalone model capability. It is an engineering system composed of models, context, tools, state, permissions, and verification mechanisms. The large model determines the upper bound of the system’s understanding and generation, while the Agent Harness determines whether those capabilities can be reliably translated into actual development output.

    Future intelligent programming systems will place greater emphasis on codebase-level understanding, long-task state management, automated verification, and security governance. For enterprises and development teams, the real question is not whether a model can generate a piece of seemingly correct code, but whether the system can continuously, transparently, and controllably complete full software engineering tasks in real projects.