The Gap in Code Agents: How Engineering Systems Determine Performance
While model capabilities set the upper limit, the engineering system surrounding the model determines the actual performance of code agents. This article explores "harness engineering," covering layered architecture, state transition loops, context management, evaluation strategies, and execution safety boundaries. It provides practical checklists and design principles to help developers build robust and reliable code agents.
The Problem: What Determines Performance Beyond the Model?
Connecting the same large language model (LLM) to different code agents can result in vastly different completion rates in real-world tasks. The reason usually lies not in parameter size, but in the execution framework surrounding the model. This framework determines what information the model sees, which tools it can call, how it handles errors, when to stop, and the impact of its actions.
Model capability sets the upper limit, but the engineering system determines how much of that capability can be utilized. A model with strong reasoning capabilities will still go astray if it receives incomplete context or if tool errors are not properly fed back. This article discusses general engineering principles and public materials, without involving any non-public source code or judging the internal implementation of specific products.
Unifying Concepts: Harness Engineering
The industry sometimes refers to the framework running outside the model as “Harness Engineering.” There is no unified standard definition for this term in the community yet. In this article, it broadly refers to several mechanisms built around the model: tool protocols, execution loops, context organization, error recovery, permission control, and evaluation systems.
It differs from an Agent framework. A framework provides basic components, while harness engineering focuses on how these components collaborate in real-world tasks and how the system behaves when issues arise. Both can coexist, and a framework might only implement part of it.
Layered Architecture and Responsibilities
A functional code agent can usually be divided into four layers. Each layer has clear responsibilities and distinct failure modes, which should be investigated separately during troubleshooting.
| Layer | Main Responsibility | Input | Output | Typical Failure & Handling | Observable Metrics |
|---|---|---|---|---|---|
| Interaction Layer | Parse user intent, display results | Natural language, file references | Structured tasks, formatted responses | Intent ambiguity; should ask for confirmation first | Ratio of clarification requests |
| Loop Layer | Schedule model and tools, decide next step | Task state, model output | Tool call requests or end signals | Infinite loops; requires a step limit | Average steps, limit-exceeded count |
| Capability Layer | Execute edits, searches, commands, tests | Tool call parameters | Execution results, exit codes, logs | Timeout or permission denied; needs clear error feedback | Tool success rate, latency distribution |
| Context Layer | Select, compress, and retain memory | History records, project files | Assembled prompt content | Loss of critical information; anchors must be retained | Information recall rate, context length |
The value of layering lies in problem localization. When a task fails, check the capability layer’s tool logs first to confirm whether the command actually executed successfully. Then inspect the context layer to verify whether the model received the necessary file contents. Only consider the model’s reasoning issues last.
State Transitions in the Loop Layer
The loop layer is the scheduling core of the system and can be described using a finite state machine. Common states include: pending decision, executing action, waiting for observation, verifying result, recovering, and ending. Each state transition has explicit conditions, behaviors are reproducible, and testing is facilitated.
Here are three transition examples that can be directly written as test cases:
- Tool call times out, and retry limit is not exceeded: transition from “waiting for observation” to “executing action,” while writing the timeout information into the context to prompt the model to try another approach.
- Model outputs the same action for three consecutive rounds with no change in results: transition from “verifying result” to “recovering,” clear the repetitive history, and require the model to replan. Three rounds is just an example threshold and should be adjusted based on task types.
- Write operation involves a protected path: transition directly from “executing action” to “ending,” requesting user confirmation without executing the operation.
Each transition should have corresponding unit tests. Transitions lacking test coverage, once they occur in production, often require manual troubleshooting.
Termination conditions also need to be explicitly listed. Common ones include task completion, user interruption, consecutive failures exceeding a threshold, triggering safety intercepts, and context exceeding the budget. Among these, context exceeding the budget is the most easily overlooked. It usually does not throw an error but simply manifests as the model beginning to forget earlier constraints.
Context Management Checklist
The context window capacity is limited; choosing what to include is more important than how much to include. During design reviews, you can check item by item:
- Have you distinguished between information that must be retained (user constraints, key fragments of the current file, unfinished steps) and information that can be compressed (completed explorations, verbose logs)?
- For long files, are structural summaries prioritized (e.g., function signatures and line number ranges) rather than dumping the entire section at once?
- Do compressed contents retain traceable anchors (e.g., original file paths and line numbers) for the model to re-read when needed?
- Is tool output truncated and formatted to prevent large chunks of irrelevant logs from filling the window?
- Do project-level memories have write conditions and expiration mechanisms to prevent outdated conventions from misleading subsequent tasks?
- Is context usage monitored, with preset handling strategies executed when approaching the limit?
Compression itself can introduce errors. It should be treated as a potentially lossy step, and its impact confirmed through evaluation.
Evaluation: How to Tell if a Code Agent Actually Improved
Many teams judge performance using only a few demo cases after modifying prompts or switching models, which often leads to unreliable conclusions. A more robust approach is to establish a fixed task set and quantify the results.
Consider the following points when designing an evaluation set:
- The source of samples should be clear, such as internally desensitized historical tasks, issues from public open-source projects, or manually written typical scenarios. Ensure authorization and privacy requirements are met before using real code.
- Tasks should cover different difficulties, including single-file modifications, cross-file refactoring, fixes requiring test runs to confirm, and ambiguous requirements that should be rejected or queried.
- Metrics should include completion rate, test pass rate, average steps, manual rework rate, and unauthorized operation count. Looking only at the completion rate may mask problems where tasks are completed in high-risk ways.
- Run the same regression set for every change and record versions to compare differences.
- Model outputs are random; key metrics should be run multiple times, and the fluctuation range reported.
If the evaluation set is small, the conclusions should only serve as a reference and not as a direct basis for deployment.
Execution Safety Boundaries and Checklist
Code agents can run commands and modify files; therefore, execution boundaries must be enforced by the system and cannot rely solely on the model’s self-awareness.
- Is the execution environment isolated from the host machine (e.g., using containers or restricted runtime accounts)?
- Is the write scope restricted to the project directory? Do deletions, permission modifications, and external network transmissions require separate confirmation?
- Are high-risk commands intercepted by program rules rather than just written in prompts?
- Are keys and credentials kept out of the model’s context and logs?
- Is there an auditable record for every operation, including command, parameters, time, and result?
- Can changes be rolled back (e.g., based on version control) so that misoperations can be undone?
These are defensive design points. This article does not discuss how to bypass these restrictions or provide attack methods for specific systems.
Applicable Boundaries and Selection Advice
Harness engineering cannot replace model capability. For tasks where the model’s reasoning capability is insufficient, even the most perfect framework will limit the effectiveness. Conversely, a highly capable model placed in a poorly constrained framework may frequently deviate from the goal during long tasks.
When selecting, consider in order: first confirm the task type and risk level, then evaluate whether the model capability meets the standard, and finally check whether the framework provides loop control, context management, and execution isolation. For small teams and simple tasks, using a mature framework is usually more cost-effective than building from scratch. For tasks involving production environments or sensitive data, prioritize permission control and auditing.
