Agent Harness vs. Agent Loop: A Layered Architecture for AI Agent Engineering

    This article explains why an agent harness and an agent loop are distinct responsibilities, and how to split them across three layers: an execution foundation, an orchestration loop layer, and a domain capability layer. It covers five core capabilities of the execution foundation—session isolation, permission control, tool registration with parameter validation, trace collection, and circuit breaking with rate limiting—along with common design mistakes, a five-question selection checklist, and a phased evolution path for teams deciding whether to build this layer in-house.

    1. Where the Problem Starts: A Frequently Misused Formula

    The industry often abstracts an agent as “model + execution harness”: the model handles reasoning and decisions, while the execution harness safely applies those decisions to a real environment and records the full process. The abstraction itself is not wrong, but it is easily misread as “adopt one more agent framework and you get complete capabilities.” For architecture decisions, the more important observation is this: such a foundation deliberately leaves upper-layer capabilities—loop, orchestration, evaluation, memory—outside its own scope, and only turns session isolation, permission control, tool integration, trace collection, and circuit breaking/rate limiting into a stable base. The intelligent coding tool in the official examples is just one assembly result built on this base, not the boundary of its capabilities.

    So it is worth reframing the selection question. Instead of asking “can it deliver a complete agent for me,” ask “in my agent engineering, which low-level work is being reimplemented across business lines, and which risks is each team handling on its own.” If the answer is session isolation, permission allowlists, tool parameter validation, execution traces, and cost caps, then this layer is worth building independently. If the project is only a demo with a few fixed workflows, the benefit of adopting it will likely not outweigh the assembly and operations cost.

    2. The Execution Harness and the Loop Are Not the Same Thing

    The term “harness” comes from software testing, where it originally meant a test fixture: placing the system under test, input cases, assertion logic, and result collectors into a fixed container, running them in batches, and automatically producing reports. Traditional model evaluation takes exactly this shape—load a dataset, call the model API, compute generic metrics, output a report—a one-way flow that terminates without iteration.

    In agent engineering, the semantics shifted in a critical way: the harness went from “a fixture that tests models” to “a fixture that runs models.” The former cares about metrics; the latter cares about controlled execution and becomes the container for an agent’s entire execution lifecycle: an isolated runtime environment, standardized tool integration, strict permission control, a complete audit channel, and reliable circuit-breaking rules.

    The “loop” is a separate responsibility. It decides when to continue, when to retry, when to converge, and when to ask a human. A common mismatch: a team wants automatic retries, so it pushes waiting and retry logic into the execution harness. As a result, retry conditions end up scattered across business code, making them impossible to observe or govern consistently. The correct split is that the harness only exposes the capability and result of “executing one controlled action,” while the loop policy is held by the upper orchestration layer.

    3. A Layered Reference Architecture: The Foundation Prevents Incidents, the Upper Layers Get Things Done

    A practical split is three layers, with boundaries drawn by two criteria: whether a capability is reusable, and whether it needs unified governance.

    • Execution foundation layer: session and runtime isolation, identity and permissions, tool registration and parameter validation, execution trace collection, quotas and circuit breaking. It treats all businesses identically, changes infrequently, and must be stable and auditable.
    • Orchestration loop layer: task decomposition, state machines, retry and fallback, parallelization and aggregation, human-in-the-loop nodes. It changes quickly with the business and needs to be testable and releasable behind gradual rollouts.
    • Domain capability layer: memory and knowledge, evaluation and regression, prompts and policies, business rules. It sits closest to the scenario and allows differentiation and even redundancy.

    An illustrative responsibility interface (pseudocode, expressing boundaries only, not pointing to any specific product):

    1. # Foundation: only promises "one controlled execution," unaware of business loops
    2. result = harness.run(
    3. session_id="s-123", # session isolation and context boundary
    4. tool="file_write", # tool must be registered in the allowlist
    5. args={"path": "a.txt"}, # args validated against a schema
    6. policy=Policy( # permissions and quota
    7. allow=["workspace/a.txt"],
    8. max_cost=0.2,
    9. timeout_ms=3000,
    10. ),
    11. )
    12. # Upper layer: the loop policy is held here
    13. for attempt in range(3):
    14. if result.ok:
    15. break
    16. if result.reason in RETRYABLE:
    17. sleep(backoff(attempt))

    The direct payoff of this split: retry, fallback, and parallel strategies can iterate quickly on the business side, while security, audit, and rate limiting are guaranteed uniformly by the foundation, without being reimplemented in every business line.

    4. Implementation Notes for the Five Foundation Capabilities

    1. Session isolation. Isolation is not just “different variables for different requests”; it is multi-dimensional isolation of the filesystem view, environment variables, network egress, temporary credentials, and cache. A good practice is to assign each session its own working directory and short-lived credentials, then reclaim and clean up leftovers when the session ends, so context and data do not bleed across sessions.

    2. Permission control. Use default-deny with allowlist-based access, and split permissions into three levels: tool, resource, and operation. The tool level decides “can it be called,” the resource level decides “which paths or records can be touched,” and the operation level decides whether actions such as read, write, delete, and outbound send are permitted. High-risk actions should require a second confirmation or human approval.

    3. Tool integration and parameter validation. Tool descriptions should include a structured schema, and validation should happen in the foundation rather than relying on the model’s self-discipline. Failures should return machine-readable error codes so the upper loop can distinguish “retryable,” “needs different arguments,” “needs a different path,” and “must go to a human.”

    4. Trace collection. A trace should contain at least an input summary, the basis for the decision, tool calls and arguments, execution results, and elapsed time and cost. Collection must be redacted, written to append-only storage, and associated with the session identifier for replay and accountability. Common cloud categories such as logs, object storage, and monitoring/alerting are enough to carry this; no proprietary component is required.

    5. Circuit breaking and rate limiting. Set four types of thresholds at once—rate, concurrency, cost, and error rate—and degrade to read-only or hand off to a human once triggered. Thresholds should be configurable per business tier, and a manual force-stop channel should remain available.

    5. Common Pitfalls and a Decision Checklist

    • Pitfall 1: treating the foundation as a framework and expecting built-in multi-agent collaboration. The value of the foundation is stability and governability; collaboration strategies belong in the upper layer.
    • Pitfall 2: pushing the loop into the foundation, which makes policies impossible to roll out gradually. The test is simple: anything that changes frequently with the business should not be pushed down.
    • Pitfall 3: collecting traces but not enabling replay. An audit without replay still leaves incident reviews to verbal accounts.
    • Pitfall 4: implementing only tool-level permissions. Without resource and operation levels, unauthorized reads and writes can still happen.

    The selection checklist compresses into five questions: is isolation and authentication being reimplemented across businesses; is there a missing unified trace and replay capability; is there already a clear need for cost caps; has the number of tools and their rate of change exceeded what humans can manage; and does the team have the capacity for the long-term investment of maintaining a foundation. If most answers are no, getting a closed loop running with a lightweight approach first is the better deal.

    6. A Suggested Evolution Path

    Phase one does only three things: session isolation, a tool allowlist, and trace persistence, with the goal of being “traceable.” Phase two adds permission tiers plus quota and circuit breaking, with the goal of being “controllable.” Phase three introduces replay and evaluation regression, turning quality into a sustainable pipeline, with the goal of being “evolvable.” Each phase should be accepted based on whether it reduces duplicated implementations and shrinks the incident surface, not on how many components were integrated.

    Ultimately, the complexity of agent engineering lies not in the model itself but in “making decisions act safely on the real world.” Only by settling controlled execution into a foundation and keeping the loop and domain policies in the upper layers can teams spend their energy where real differentiation happens. Baidu AI Cloud’s practices in common capabilities such as container platforms, log services, and monitoring/alerting also offer reference infrastructure thinking for this kind of layered implementation.