The Agent Harness: A Practical Guide to Building the Layer Around Your LLM
This article explains the agent harness, the engineering layer that turns a raw large language model into a usable coding agent. It breaks down the harness's core responsibilities—context management, tool invocation and permission boundaries, file change tracking, terminal execution, and self-correction loops—and presents a layered reference architecture with team roles. It also covers evaluation metrics, observability, and common anti-patterns, showing why the harness, not the model, determines whether an agent reliably reaches its capability ceiling.
1. Why You Need a Harness: Bridging Model Capability and Product Capability
A large language model does exactly one thing: given an input sequence, it produces a probability distribution and samples the next chunk of text. It has no persistent memory, no awareness of the file system, no ability to actually execute commands, and no way to retry on its own after a failure. A usable coding agent product, by contrast, must read files, modify code, run tests, inspect logs, and correct itself inside a real workspace. The gap between the two is precisely the reason the harness exists.
A widely cited formula captures the relationship:
Model + Harness = Agent
The model handles reasoning and generation; the harness handles everything else that is engineering work. It is more accurate to think of the harness as the control and orchestration system surrounding the model: it manages context, schedules tools, constrains output formats, captures execution results, and drives the next round of decisions.
It is worth stressing that a harness is not a collection of prompt templates, nor is it a function-call wrapper. It is a stateful, observable, and rollback-capable runtime environment.
2. Breaking Down the Core Responsibilities of a Harness
2.1 Context Management
The context window is a scarce resource. In engineering practice it is usually handled in layers:
- System layer: role, safety boundaries, output specifications—kept stable over the long term;
- Project layer: repository structure, dependency manifests, coding conventions—loaded on demand;
- Task layer: the current goal, completed steps, and hypotheses still to be verified;
- Observation layer: tool return values, error stacks, test output.
The key strategy is “summarize + retrieve + evict.” When history exceeds the budget, early conversation is compressed into a structured summary, and large file contents are replaced with paths and retrieved snippets, rather than being simply truncated.
def build_context(system, project, task, observations, budget=120000):parts = [system, project, task]used = estimate_tokens(parts)for obs in reversed(observations):cost = estimate_tokens([obs])if used + cost > budget:parts.append(summarize(obs)) # compress instead of droppingelse:parts.append(obs)used = estimate_tokens(parts)return parts
2.2 Tool Invocation and Permission Boundaries
Tools are the only means by which an agent acts on the world. Design should follow the principle of least privilege: read operations are allowed by default, write and network operations require explicit authorization, and delete or deploy operations require a second confirmation. Every tool needs clear input schema validation and timeout control so that invalid parameters generated by the model never reach the execution layer.
2.3 File Reading/Writing and Change Tracking
A reliable coding agent must be able to answer “what did I change?” In practice, the recommendation is: all changes first go into a workspace copy or a branch, a diff view is generated, and only then is a decision made about whether to commit them to disk; snapshots of critical files are retained to support rollback.
2.4 Terminal Execution and Result Return
The terminal is both a capability amplifier and a risk entry point. The recommended approach is a command allowlist + sandboxed execution + output truncation and structured extraction (for example, returning only error lines and the exit code), which both controls context bloat and reduces the probability of accidental operations.
2.5 The Self-Correction Loop
The essence of self-correction is “iteration backed by evidence”: the model proposes a hypothesis, tools produce facts, and the harness compares the two and decides whether to continue. Termination conditions must be defined explicitly—for example, a maximum number of rounds, a number of consecutive rounds without progress, or a test pass-rate threshold—otherwise the process easily degenerates into an unproductive loop.
3. Reference Architecture and Team Structure
A layered structure that can be put into practice looks like this:
- Access layer: a desktop client or command-line interface, responsible for interaction and authorization prompts;
- Orchestration layer: task planning, step scheduling, termination decisions;
- Capability layer: tool registry, sandbox, retrieval and indexing services;
- Infrastructure layer: logging services, monitoring and alerting, metrics collection, and audit trails.
In terms of organization, such a team typically has three roles: researchers, engineering developers, and product managers. Researchers explore planning and evaluation methods, engineers deliver the runtime and toolchain, and product managers define the interaction and explainability experience. When working across locations, evaluation baselines, interface contracts, and documentation should come first—these are the key to reducing collaboration overhead.
Over a longer time horizon, a lab that is primarily open-model oriented and moves toward productization usually goes through a shift in its evaluation system, from “model metric driven” to “task success rate and retention driven,” and it typically takes 6 to 12 months of iteration before the product is formally usable.
4. Evaluation and Observability
A harness without evaluation cannot evolve. Three tiers of metrics are recommended:
- Task level: end-to-end success rate, average number of rounds, human intervention rate;
- Step level: tool call validity rate, parameter error rate, timeout rate;
- Cost level: context token consumption, cache hit rate, time per task.
All traces must be replayable. Replay is the only reliable way to determine whether the model was wrong or the orchestration was wrong.
5. Common Anti-Patterns
- Stuffing all history into the context indiscriminately, which raises cost and dilutes attention;
- Tool granularity that is too coarse, with one tool taking on too many responsibilities, making errors hard to attribute;
- Missing termination conditions, so self-correction degrades into a random walk;
- Permissive permission design, where write operations lack confirmation and auditing;
- Evaluating only the final output without recording intermediate traces.
6. Conclusion
The harness is the engineering center of gravity for an agent product: the model determines the ceiling of capability, while the harness determines whether that ceiling can be reached reliably. Making context, tools, execution, rollback, and evaluation into an observable and reproducible system is the dividing line between a demo and something genuinely usable.
