跪拜 Guibai
← All articles
GitHub · Agent · Interview

Building an Agent Harness from Scratch: Loops, Constraints, and Context Engineering

By 粥里有勺糖 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Most Agent frameworks abstract the loop away, but understanding the Harness — the explicit validate-execute-write-back cycle — is what separates a reliable, stoppable Agent from a prompt that occasionally calls a function. The hard constraints and context assembly patterns described here are the difference between a demo and something that can run unattended.

Summary

An Agent is not just a model — it is a model running inside a Harness that orchestrates tool execution, context assembly, and loop control. The Harness parses structured tool_calls, validates and executes them, writes results back into the message history, and requests the model again until a termination condition is met. Without this loop, most so-called Agents are just chatbots.

Hard runtime constraints — max steps, timeouts, pre-execution validation, and confirmation hooks for dangerous operations — keep the loop from spinning out of control. Context Engineering assembles every request from system prompts, tool schemas, conversation history, and on-demand injections like Memory, RAG, or SKILL documents. SKILLs are SOP-style Markdown files that the Harness can inject explicitly, by keyword match, or by letting the model request them as a tool.

MCP integration standardizes external tool discovery and invocation through a long-lived client-server connection, with the Harness acting as the Host that registers MCP tools alongside local ones in a unified registry.

Takeaways
An Agent is the combination of a Model and a Harness; without the Harness, it is just a chat interface.
Function Calling lets a model output structured tool_calls instead of natural language descriptions, making tool execution parseable and reliable.
The core Harness loop is: assemble context, request model, parse tool_calls, validate and execute tools, write results back, and repeat until no tool_calls remain or a stop condition fires.
Hard runtime constraints — maxSteps, timeoutMs, AbortController signals, and onConfirm hooks for dangerous tools — prevent infinite loops and irreversible damage.
Context Engineering means every model request is assembled from system prompts, tool schemas, conversation history, tool results, the current user input, and on-demand injections like Memory, RAG, or SKILLs.
A SKILL is a Markdown SOP document, not a tool; it can be injected into context explicitly by the user, matched by the Harness via keywords, or loaded by the model through a tool call.
MCP standardizes external tool connections: the Harness acts as a Host, spawns long-lived Clients per Server, and unifies local and MCP tools in a single registry.
Tool results can carry a terminate flag to skip an unnecessary LLM round-trip when the answer is already structured.
Steering and follow-up messages let users inject new instructions mid-loop or just before the Agent finishes, without restarting the run.
Conclusions

Explicitly modeling the Harness as a separate runtime component makes Agent behavior debuggable and stoppable in ways that monolithic prompt chains are not.

The terminate flag on tool results is a small optimization that eliminates wasted LLM calls — a pattern worth adopting in any Agent loop that produces structured output.

Loading SKILLs through a tool call, rather than always injecting them upfront, keeps the context window lean and lets the model decide what SOP it actually needs.

Hard constraints like maxSteps and onConfirm are not optional polish; they are the only reliable guardrails when a model can hallucinate tool calls or loop indefinitely.

Concepts & terms
Harness
The runtime layer that orchestrates an Agent's loop: it assembles context, calls the model, parses tool_calls, validates and executes tools, writes results back, and enforces termination constraints.
Function Calling / Tool Calling
A model capability that returns structured tool_calls (name, arguments, call ID) instead of natural language, so the Harness can parse and execute them reliably.
Agent Loop
The automatic cycle of context assembly → model request → tool execution → result write-back → repeat, continuing until no tool calls remain or a stop condition is met.
Context Engineering
The practice of assembling the full prompt payload for each model request from system prompts, tool schemas, history, tool results, user input, and on-demand injections like Memory, RAG, or SKILLs.
SKILL
A Markdown-based SOP document that describes a multi-step workflow; it is injected into context to guide the model, but actual execution still happens through tool calls.
MCP (Model Context Protocol)
An open protocol that standardizes how AI applications discover and call external tools, using a Host-Client-Server architecture with JSON-RPC over stdio or HTTP.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗