Engineering the Agent Harness: From Basic Loops to Capability Building

    This article explores the engineering practices behind the LLM agent runtime, commonly known as the Agent Harness. It covers the core components of an agent environment—tools, knowledge, observation, action interfaces, and permissions—and delves into building the execution loop, task planning, on-demand knowledge loading, context compression, and sub-agent spawning to construct reliable and scalable AI agents.

    1. Why Study the Agent Runtime Independently?

    Large language models inherently only generate text. To enable them to perform tasks such as reading and writing files, executing checks, and splitting sub-tasks, an external program layer is required to translate the model’s output into specific actions and feed the results back to the model. This layer is commonly referred to as the Agent Harness. It dictates what the model can see, what it can call, what it can modify, and how it recovers from errors.

    The quality of the runtime environment directly impacts the agent’s reliability. The same model typically performs more stably in an environment with clear tool definitions, well-organized context, and explicit permission boundaries. Even a highly capable model will struggle to complete multi-step tasks without a suitable environment.

    2. Five Basic Components of the Agent Harness

    A functional agent runtime can be summarized as:

    Agent Harness = Tools + Knowledge + Observation + Action Interface + Permissions

    • Tools: Capabilities the model can invoke, such as executing commands, reading/writing files, modifying local content, finding files by pattern, searching content by keyword, and accessing web pages. Each tool requires a clear name, input structure, and return format.
    • Knowledge: Background information required by the model to complete tasks, including project specifications, domain documentation, and operational steps. Knowledge should be provided on demand to avoid flooding the context all at once.
    • Observation: How the environment feeds the current state back to the model, such as standard command output, error codes, file diffs, and test results. The more accurate and structured the observation, the more reliable the model’s subsequent judgments.
    • Action Interface: How the model’s output is parsed into specific operations. The interface must validate parameters and provide readable feedback for formatting errors.
    • Permissions: Defines which operations can be executed automatically, which require human confirmation, and which are strictly prohibited.

    These five components are interdependent. Without observation feedback, the model cannot determine tool results; without permissions, more tools equate to higher risk. All five should be considered simultaneously during setup rather than merely piling up tools.

    3. Starting with the Minimal Execution Loop

    The core of an agent is a loop: the model reads the current context and decides whether to call a tool; the agent harness executes the tool and appends the result to the context; the loop continues until the model provides a final answer or a stop condition is met.

    Schematic pseudo-code is as follows:

    1. messages = [system_instruction, user_task]
    2. while not stop_condition_met:
    3. reply = call_model(messages, available_tools)
    4. messages.append(reply)
    5. if not reply.contains_tool_call:
    6. break
    7. for call in reply.tool_calls:
    8. if not permission_check(call):
    9. result = rejection_reason
    10. else:
    11. result = execute_tool(call)
    12. messages.append(tool_result(call.id, result))

    This loop seems simple, but three practices must be implemented:

    1. Set a maximum number of turns to prevent infinite loops from draining resources.
    2. Tool results must be backfilled in a structured manner, including success status, truncated output, and error information.
    3. Log each turn for post-mortem analysis.

    It is recommended to run the loop with just two or three tools first, then gradually expand. A minimal closed loop quickly exposes issues in interface design.

    4. Task Planning and Dependency Management

    Complex tasks usually contain multiple interdependent steps. If the model only plans verbally in a conversation, the plan is easily lost or derailed in a long dialogue. A more robust approach is to write the plan into a structured task table, where each task includes an ID, description, status, and dependencies.

    Key design points for the task table:

    • Statuses are divided into To-Do, In Progress, Completed, and Blocked. Status changes are recorded by the agent harness.
    • Dependencies are represented as a directed graph. Subsequent tasks can only be picked up if prerequisite tasks are completed.
    • The task table is stored in persistent storage rather than just in the model’s context. Even if the context is compressed, the plan remains accessible.

    The value of planning capabilities lies in making the agent’s progress trackable. Humans can check task statuses at any time to determine if the agent is deviating from the goal.

    5. On-Demand Knowledge Loading

    Putting all background materials into the context at once consumes massive window space and distracts the model from key information. On-demand loading is a more rational approach.

    A common practice is to create skill files for each type of knowledge. The beginning of the file contains a brief summary, and the body includes operational steps, examples, and precautions. The model first sees the list of summaries and can read the full content via a tool call when needed.

    Consider the following during design:

    • Summaries must be specific enough for the model to judge when it needs to read them.
    • Skill files should be split by domain to prevent individual files from becoming too long.
    • Files should be placed under version control so changes can be traced.

    6. Context Compression and Long-Term Memory

    Long tasks generate massive tool outputs, and the context quickly approaches its limit. Three common coping strategies are:

    1. Truncation: For overly long outputs, retain only the head and tail, and annotate the number of omitted lines.
    2. Summarization: When context usage exceeds a threshold, use the model to generate periodic summaries to replace earlier conversation turns. Summaries should retain goals, completed items, unresolved issues, and key file paths.
    3. External Storage: Write intermediate results to files or databases, keeping only reference identifiers in the context to be read when necessary.

    Long-term memory differs from context compression. Long-term memory stores information that remains valid across sessions, such as project conventions, user preferences, and known bugs. It should have explicit write conditions and expiration mechanisms to prevent incorrect information from accumulating indefinitely.

    7. Sub-Agent Spawning

    When a sub-task requires extensive exploration, such as reading multiple modules or searching for all occurrences of an issue, executing it directly in the main context rapidly consumes space. In such cases, a sub-agent can be spawned to complete the task in an independent context, returning only the conclusion to the main agent.

    When spawning, four things must be clearly defined: the goal of the sub-task, the available tool scope, the format of the returned result, and the maximum execution turns. The tool scope of the sub-agent should be smaller than that of the main agent, adhering to the principle of least privilege.