How Code Agents Work: A Breakdown of Harness Engineering and Architecture

    This article breaks down the engineering architecture behind code agents, explaining why most of their real-world capability comes from the client-side harness rather than the underlying model. It covers the core components of harness engineering: the agent loop, tool calling, context engineering, and guardrails with permission control. Developers will learn how these layers turn a general-purpose LLM into a reliable agent that can complete multi-step software tasks.

    Introduction: What an Accidental Source Code Leak Revealed

    Not long ago, the client source code of an AI coding tool was accidentally exposed after a sourcemap file was uploaded along with the npm package. Hundreds of thousands of lines of core implementation code became publicly searchable overnight. The incident itself is easy to joke about, but for developers, the real value lies in a fact that this source code confirms: for an agent that can do work, modify code, and run commands, most of the complexity lives in the client-side engineering wrapper, not in the model.

    One common misunderstanding needs to be cleared up first: what leaked was the “client” code, meaning the scheduling and execution logic wrapped around the large language model, not the weights of the underlying model itself. It is precisely this long-overlooked “outer layer” that explains why the same model can perform wildly differently across products. Understanding it is the key to understanding modern code agents.

    What Is “Harness Engineering”

    Over the past couple of years, a new term has emerged in the industry: harness engineering. The name sounds sophisticated, but at its core it simply re-emphasizes common sense.

    We have long attributed an agent’s performance entirely to the model: “the model isn’t smart enough” or “the model keeps hallucinating.” Harness engineering offers a different judgment: instead of praying every day for the model to get smarter, put a harness on this wild horse of compute. The harness is the entire engineering system outside the model that constrains behavior, defines lanes, orchestrates tools, and manages context. The model is responsible for “thinking and speaking,” while the harness is responsible for “when it can think, what it can see, what it can do, and how mistakes get intercepted.”

    This also explains why the same model, fitted with different harnesses, can show order-of-magnitude differences in real productivity. Let’s break down the core components of this harness layer by layer.

    1. The Agent Loop: The Backbone That Drives Everything

    The most fundamental structure of a code agent is a continuously iterating loop, commonly called the Agent Loop. It is not a one-shot question-and-answer exchange, but a multi-round process of “observe, think, act, observe again.” The classic implementation combines reasoning and acting:

    1. while task is not complete:
    2. 1. Read the current conversation and tool execution results (observe)
    3. 2. The model decides the next step based on context (think)
    4. 3. If a tool is needed, invoke it (act)
    5. 4. Append the tool's return value to the context
    6. 5. Decide whether to finish (give a final answer or continue)

    This loop is the heart of the entire agent. A truly reliable agent usually adds substantial engineering hardening on top of this skeleton: for example, setting a maximum number of iterations to prevent infinite loops, forcing intervention when the model repeatedly calls the same failing tool, or breaking long tasks into multiple sub-goals that converge step by step. Without a loop, the model is just a chatbot that answers one question at a time; with a robust loop, the model gains the foundation for autonomously completing multi-step tasks.

    2. Tool Calling: The Agent’s Hands and Feet

    The model itself can only generate text. What actually lets it “take action” is the tool calling mechanism. An agent’s capability boundary is almost entirely equal to the set of tools it has mounted.

    A code agent typically exposes these categories of tools:

    • File operations: reading, writing, editing, and searching files in the codebase.
    • Command execution: running shell commands such as builds, tests, and scripts, and collecting standard output and error messages.
    • Structured retrieval: symbol lookup, dependency analysis, and semantic search in large repositories.
    • Network and external integrations: accessing external APIs or documentation on demand.

    The key point is that the tool definitions themselves must be carefully designed. The industry commonly uses something like JSON Schema to describe each tool’s name, purpose, inputs, and return values, so the model can accurately judge “which tool to use in which scenario and how to fill in the parameters.” Vague tool descriptions lead to misuse; clear ones significantly improve the model’s selection accuracy. In other words, the engineering quality of an agent is largely reflected in the abstraction granularity and description quality of its tool interfaces.

    3. Context Engineering: Managing the Agent’s “Memory”

    If tools are the hands and feet, context is the agent’s workbench and memory. But the model’s context window is a limited and expensive resource, which gives rise to context engineering as a core discipline.

    The problems to handle in practice include:

    • Information selection: a codebase can easily contain thousands of files, and it is impossible to stuff them all into the context. Retrieval and filtering mechanisms are needed to feed only the snippets most relevant to the current task into the model.
    • History compression: in multi-turn conversations, early content keeps accumulating. As the window limit approaches, the history needs to be summarized or truncated, keeping key decisions and results while discarding redundant process.
    • Externalized state: recording intermediate state, task lists, and completed steps outside the model (in files or structured storage), so the model can read them back on demand each loop rather than relying on long conversation memory.

    High-quality context engineering essentially answers one question: at each step, how do you let the model see exactly the information it needs, no more and no less. Done well, the model stays stable and efficient; done poorly, the model either “loses memory” and drifts off course or gets drowned in irrelevant information.

    4. Guardrails and Permissions: Actually Putting the Harness on the Wild Horse

    The one part of harness engineering that cannot be omitted is safety guardrails and permission control. A code agent can read and write files and execute commands, and once it loses control, the damage is real.

    Engineering constraints are usually established along several directions:

    • Tiered permissions: distinguishing read-only operations from high-risk writes, deletions, and system-level commands, and requiring explicit confirmation for dangerous actions.
    • Operation sandboxing: restricting command execution to a controlled environment to isolate potential impact on the main system.
    • Behavioral constraint prompts: clearly defining the agent’s behavioral boundaries, the allowed scope of operations, and prohibited actions in the system prompt.
    • Auditing and rollback: logging key operations for traceability and preserving the ability to revert file changes.

    This part corresponds exactly to the “harness” metaphor: the model is a wild horse, fast and untamed; the guardrails determine that it can only gallop within a defined track rather than trampling the entire grassland. An agent’s reliability is less a property of the model than a property of the guardrail design.

    5. Why Engineering Matters More Than the Model

    Stringing the above modules together, a clear conclusion emerges: the competitiveness of a modern code agent comes mostly from the engineering system, not from the underlying model’s standalone capability.

    The same model, connected to a well-designed harness, can stably run dozens of steps of complex tasks; connected to a crude harness, it may fall into an infinite loop or delete files by mistake within three steps. This is the deeper reason harness engineering is emphasized again and again: it marks a maturing of industry understanding. Instead of betting everything on “the next generation of models will be smarter,” it acknowledges that engineering constraints, tool design, context scheduling, and safety protection are the bridge that turns model capability into real productivity.

    For developers who want to build similar systems, a few pragmatic principles can be distilled:

    1. Make the Agent Loop solid first, with iteration limits and failure detection.
    2. Carefully abstract tool interfaces, using clear schemas to describe each tool’s applicable scenarios.
    3. Treat context engineering as a first-class citizen, actively doing retrieval, compression, and state externalization.
    4. Build tiered permissions and operation guardrails from day one, rather than patching them in afterward.

    Conclusion

    That accidental source code leak was less a spectacle to watch and more an unexpected technical textbook. It confirms that the real moat of a code agent lies not inside the model, but in the invisible engineering harness around it. Understanding and mastering how to build this harness is the right path for any team to turn large model capability into a stable, reliable, and deliverable agent.