Building an Agent Harness from Scratch: Engineering Practices
This article walks through the engineering practice of building an Agent Harness from the ground up. It explains why model capability does not equal product capability, introduces a restrained architecture philosophy, and presents a minimal Agent Loop kernel with essential guardrails. It then covers layered capability evolution, including task planning with dependency graphs, on-demand knowledge loading, long-term context management, multi-agent collaboration with workspace isolation, and permission governance. A reference project structure, a four-week rollout plan, and an acceptance checklist are included to help readers implement a stable and observable agent runtime.
Building an Agent Harness from Scratch: Engineering Practices
1. The Starting Problem: Why Model Capability Is Not Product Capability
Over the past two years, the reasoning and coding abilities of general-purpose large models have kept improving. Yet when teams connect these models to real business workflows, they run into the same issue: demos look impressive, but the systems become highly unstable in production. The reason is not mysterious. The model is only responsible for deciding the next step, while the environment around it determines whether that decision can be executed, verified, and reused.
The industry has gradually reached a consensus: what truly sets the ceiling for an agent is not a clever prompt in a single turn, but the runtime environment surrounding the model. This environment is commonly called the Agent Harness. It determines what the model can see, what it can call, how execution results are returned, and how out-of-bounds behavior is intercepted.
A memorable governance formula is:
Harness = Tools + Knowledge + Observation + Action Interfaces + Permissions
That is, tools, knowledge, observation channels, action interfaces, and permissions must be deeply combined to form the working environment an agent depends on. If any one of them is missing, the system degrades into a “script that can chat.”
2. The Core Idea: A Philosophy of Restraint
When many teams build agents, their first instinct is to add logic: more branches, more state machines, and more rules that say “correct the model if it judges wrongly.” In practice, the result is often the opposite. The denser the rules, the harder it is for the model to perform, and the more fragile the system becomes.
A more mature architecture philosophy is restraint. The framework does not make decisions for the model. Instead, it paves the way through a standardized governance structure and then steps aside. It guarantees only three things: actions are executable, results are observable, and violations can be blocked. Whether to read code first or run tests first, or whether to change an interface first or add test cases first, is left to the model to decide in a clean environment.
This design brings two direct benefits. First, behavior is explainable: when something goes wrong, you can trace it to a specific tool and permission configuration instead of burying it across multiple layers of conditions. Second, capability can evolve: adding a tool or a type of knowledge does not require rewriting the decision tree.
3. The Minimal Viable Kernel: The Agent Loop
Everything starts with a loop. A minimal kernel contains only four steps: receive a goal, let the model reason, execute an action, and return an observation.
def agent_loop(goal, tools, model, max_steps=30):history = [{"role": "user", "content": goal}]for _ in range(max_steps):action = model.decide(history, tool_schema(tools))if action.type == "finish":return action.payloadobservation = tools[action.name](**action.args)history.append({"role": "assistant", "content": action.raw})history.append({"role": "tool", "content": observation})return {"status": "step_limit_reached"}
This code is deliberately “dumb”: there is no planner and no reflection template. Its value is to establish a contract. Actions must be structured, observations must be written back into history, and the loop must have a termination condition. All later capabilities are increments on top of this contract, not rewrites.
Guardrails that need to be added immediately include: a per-step timeout, a cap on per-step output length, structured returns for tool exceptions (treat errors as observations rather than crashes), and a fallback path triggered by the step limit.
4. A Layered Roadmap for Capability Evolution
4.1 Task Planning and Dependency Graphs
When a goal contains multiple subtasks that can run in parallel, you need an explicit task system. A task node contains a title, status, dependency list, and output summary. The dependency graph lets the model “see” the full picture and choose the currently executable set instead of pushing forward linearly.
{"id": "t3", "title": "Add unit tests", "status": "pending", "deps": ["t1", "t2"]}
The key design is that the framework maintains state while the model proposes priorities. This avoids drift caused by the model trying to remember task state on its own across long text.
4.2 On-Demand Knowledge Loading
Stuffing all documents into the context at once is the most common performance and cost trap. A more reasonable approach is to skill-ify knowledge: each capability unit is described in a separate file that specifies trigger conditions, inputs and outputs, and constraints. At runtime, only a small number of entries relevant to the current task are loaded, while the rest remain “discoverable but not loaded.”
This requires the framework to provide retrieval and listing capabilities and to allow the model to explicitly request loading a knowledge unit. The context budget therefore shifts from “pre-allocated” to “requested on demand.”
4.3 Long-Term Context Management
Context will inevitably overflow; the only question is when. Three workable strategies exist. The first is compression: summarize historical observations, keep the conclusions, and discard the process. The second is externalization: write large artifacts to workspace files and keep only the path and a summary in history. The third is replay: when details are needed, read the file again instead of relying on memory.
[compressed] Read 12 files, confirmed the configuration entry is in the service bootstrap module, see notes/bootstrap.md
4.4 Multi-Agent Collaboration and Parallel Isolation
Complex tasks can spawn sub-agents, each with its own context and tool subset, exchanging conclusions through an asynchronous message channel (similar to a mailbox), with the main agent aggregating the results. To avoid overwriting each other, parallel execution should be paired with workspace isolation: each branch makes changes in a separate directory or a version-control working copy, and the changes are merged after completion.
Isolation brings an additional benefit: rollback. If a parallel attempt fails, you only discard the corresponding working copy without affecting the main line.
4.5 Permission Governance
Permissions are the last gate. A practical approach is to grade them by the “impact scope of the action”: reads are allowed by default; writes to restricted directories require confirmation; outbound actions such as network access and package installation require explicit authorization; destructive operations are forbidden by default. All authorization decisions and execution results go into a unified logging service for auditing and traceback.
5. A Reference Structure and a Rollout Rhythm
A maintainable engineering structure usually contains a reference implementation directory (numbered by stage, from the minimal kernel to the full form), a multilingual documentation directory, an interactive demo directory, a skill definition directory, and continuous integration configuration (type checking and build validation).
A suggested four-week rhythm: in week one, get the loop working with three basic tools (read, write, execute); in week two, add the task system and on-demand knowledge loading; in week three, complete context compression and workspace isolation; in week four, refine permission grading, logging, and monitoring alerts, and run a failure-injection drill.
6. Acceptance Checklist
- The loop has three limits: step count, timeout, and output length, and exceptions are returned as structured observations;
- Task state is persisted by the framework and can be recovered after a restart;
- Context is automatically compressed at thresholds without losing key conclusions;
- Parallel tasks run in isolated workspaces, and merge conflicts can be located;
- Unauthorized actions are intercepted and recorded, and audit logs can be searched by session;
- A failure of any single tool does not crash the whole system.
7. Conclusion
The difficulty of building an Agent Harness lies not in algorithms but in trade-offs: give decision-making back to the model, and give determinism to the framework. When tools, knowledge, observation, action interfaces, and permissions are combined stably, the model can keep producing in an environment that is both controlled and free. If you need elastic compute and supporting capabilities, you can use a container platform to host parallel workspaces, object storage to store artifacts, and logging and monitoring services for observability, so that your energy stays focused on the architecture itself.
