Agent Harness Architecture: A Practical Guide to the Six Core Modules of AI Coding Agents
This article breaks down the agent harness, the application layer that wraps a large language model to improve its performance on complex coding tasks. It explains the core iterative loop of observe, inspect, choose, and act, then walks through six essential modules—tool layer, context management, memory, scheduling loop, safety and permissions, and observability—with a minimal pure-Python reference implementation called Mini Coding Agent. Readers will learn how these modules work together and how to build a minimal working system before expanding it.
1. Why the Same Model Delivers Very Different Results
In real-world software engineering, one often underestimated fact stands out: the external system built around a model can be just as important as the model itself. Tool set design, context organization, and memory read/write strategies are engineering factors that together determine the upper limit of an agent’s performance on real tasks. This also explains a common phenomenon—when the same underlying model is packaged into an AI coding tool, its ability to solve complex coding problems is noticeably better than when it is called directly in a plain chat interface.
The industry typically refers to this application-layer structure, which wraps around the model and handles scheduling and constraints, as an agent harness. It does not change the model weights. Instead, through prompt organization, tool exposure, state maintenance, and process control, it turns one-off text generation into a task execution process that can keep moving forward. This article uses six core modules as the thread and a minimal pure-Python reference implementation (referred to below as Mini Coding Agent) to fully explain how this mechanism works.
2. The Overall Shape: A Continuously Iterating Loop
The essence of an agent handling coding tasks is a loop that keeps converging, which can be abstracted into four stages:
observe → inspect → choose → act↑ │└────────────── feedback ─────────┘
- observe: receive the result of the previous round and form a new observation;
- inspect: combine the current goal and context to judge whether the information is sufficient;
- choose: pick the next strategy from candidate actions;
- act: call tools to actually change the environment (read files, modify code, run commands).
The fundamental difference from a “one question, one answer” conversation is that the loop allows failure, allows retries, and allows revising the plan based on real feedback. The model no longer just generates text; it makes decisions in an environment with feedback.
3. The Six Core Modules
1. Tool Layer: Turning Capability Boundaries into Callable Interfaces
Tools are the only channel through which an agent reaches the real world. Key design points include: moderate granularity (one tool does one verifiable thing), clear descriptions (the name and description are themselves part of the prompt), and structured returns (easy to parse in the next round). A common tool set includes file reading, snippet writing, directory traversal, command execution, and retrieval.
def tool_read_file(path: str) -> str:with open(path, "r", encoding="utf-8") as f:return f.read()TOOLS = {"read_file": tool_read_file}
2. Context Management: Making Trade-offs Within a Limited Window
The context window is a scarce resource. A mature approach is layered loading: system constraints stay resident, task goals and key constraints take priority, code keeps only relevant snippets rather than stuffing in the entire repository, and historical traces are compressed into summaries. The core principle is “load on demand, evict promptly,” so that every prompt focuses on the minimum sufficient information needed for the current decision.
3. Memory: Persisting State Across Rounds and Across Sessions
Memory is divided into short-term (intermediate conclusions of the current task, ruled-out options) and long-term (project structure conventions, user preferences, past pitfalls). In implementation, it can be carried by files or a lightweight key-value store. The key is that writes must be selective and reads must be retrievable, avoiding solidifying noise into “facts.”
4. Scheduling Loop: Turning Decisions into a Controllable State Machine
The scheduler drives observe→inspect→choose→act and manages termination conditions (task completion, step limit, unrecoverable errors). A sketch implementation:
for step in range(MAX_STEPS):observation = run_tool(last_action) # observedecision = model.decide(goal, observation) # inspect + chooseif decision.done:breaklast_action = decision.action # act
5. Safety and Permissions: Putting Brakes on Automation
Command execution and file writing are destructive and must have boundaries: path whitelists, interception of dangerous commands, previewing diffs before write operations, and requiring human confirmation when necessary. Safety is not optional; it is the prerequisite for automation to be trusted.
6. Observability: Making Every Failure Reviewable
Fully record each step’s action, parameters, return, and elapsed time, and combine this with logging services and monitoring alerts to quickly locate whether the problem is a prompt issue, a tool issue, or an environment issue. Observability determines iteration speed.
4. How the Modules Work Together: A Walkthrough of a Real Task
Take “fixing an error” as an example: the scheduler first has the agent traverse directories (tool layer) to locate relevant files, reads snippets and compresses them into the context (context management), writes conclusions into short-term memory to avoid repeated investigation (memory), selects an edit action and previews the diff (safety and permissions), executes a test command to get feedback, and returns to the loop if it fails, with the whole process recorded for review (observability). None of the six modules can be missing; it is precisely their coupling that makes the model exhibit “engineering capability.”
5. Engineering Implementation Advice
To build a similar system in a cloud environment such as Baidu AI Cloud, you can adopt a combination of general capabilities: use a container platform to host the runtime, object storage to save artifacts and memory snapshots, a message queue to decouple long tasks, and logging services and monitoring alerts to ensure observability. For implementation, it is recommended to first build a minimal closed loop (tools + loop + context), then gradually fill in memory, safety, and observability, avoiding over-designing everything at once.
