AI Agent Harness Engineering: The Runtime Around the Model

Tokyo AI Dad11 min read

ENGenAI math, plainlyAI investment research

Quick answer: AI agent harness engineering is the design of the runtime around a model: the context it receives, tools it may call, memory it retains, tests it must pass, permissions it holds, and points where a human approves work. Strong models need strong harnesses to perform reliably in real workflows.

The previous two pieces were about scaling laws: how model size, data, and compute shape the economics of AI training.

This piece moves one layer outward.

Lilian Weng's "Harness Engineering for Self-Improvement" is not mainly about making model weights larger. It is about the system around a model:

When the base model is already capable, how does the surrounding runtime help it do long tasks, check itself, remember failures, use tools, and improve over time?

The keyword is harness.

Think of a harness as the model's school, toolbox, notebook, exam system, and safety boundary.

The model is the brain. The harness is the system that decides what the brain sees, which tools it can use, what it remembers, how it checks work, and where a human must step in.

A harness is the runtime around the model

Figure 1: The base model supplies intelligence. The harness controls context, tools, memory, evaluation, permissions, and human oversight.

Why does this matter?

Near-term AI self-improvement may not begin with a model directly rewriting its own weights. A more practical path is:

Improve the system that lets the model work. Better work produces better traces, failures, evaluations, and memory. Those artifacts then improve the next harness.

That is the commercial importance of harness engineering.

What Is a Harness?

A simple formula is:

y=Hh(M,x)y = H_h(M, x)

Here:

  • MM is the base model.
  • xx is the task.
  • hh is the harness design.
  • HhH_h is the whole runtime wrapped around the model.
  • yy is the final output.

Plain chat is closer to:

y=M(x)y = M(x)

An agent harness is closer to:

y=Hh(M,x,c,T,R,E)y = H_h(M, x, c, T, R, E)

The added parts matter:

  • cc: context, or what the model currently needs to know.
  • TT: tools, such as search, files, code execution, databases, and browsers.
  • RR: memory, including logs, failures, trajectories, and artifacts.
  • EE: evaluators, including tests, scores, audits, and regression checks.

The question is not only "how smart is the model?"

It is:

What kind of working environment is the model placed inside?

Why This Connects to Self-Improvement

Recursive self-improvement sounds dramatic. In practice, the near-term loop is more like disciplined software engineering:

  1. Current harness hth_t runs tasks.
  2. The system stores traces, errors, tool calls, diffs, and test results.
  3. An agent mines recurring failure patterns.
  4. It proposes a small harness edit hh'.
  5. The edit is tested on held-in and held-out tasks.
  6. It is accepted only if it fixes the known issue without causing regressions.

Mathematically:

ht+1={hif Jin(h)>Jin(ht) and Jout(h)Jout(ht)htotherwiseh_{t+1} = \begin{cases} h' & \text{if } J_{\text{in}}(h') > J_{\text{in}}(h_t) \text{ and } J_{\text{out}}(h') \ge J_{\text{out}}(h_t) \\ h_t & \text{otherwise} \end{cases}

Meaning:

Upgrade only when the new harness fixes known failures and does not damage general performance.

Self-harness improvement loop animation

Figure 2: A self-improving harness should mine failures, propose bounded edits, regression-test them, and accept only safe improvements.

Three Design Patterns

Lilian's post organizes several harness design patterns. The three most important are workflow automation, persistent memory, and parallel sub-agents.

Workflow automation

An agent should not answer once and stop. Long tasks need a loop:

plan → act → observe/test → reflect → revise → act again

A coding agent works because it can read files, edit code, run tests, inspect errors, and try again. The product is not only a chat interface. It is a runtime loop.

File system as persistent memory

Long tasks generate too much information for the context window. Raw chat, tool outputs, code diffs, errors, and logs quickly become larger than the prompt can handle.

The harness should keep detailed history in files and place only the distilled state into the context window:

ct=compress(qt,Rt)s.t.ctWc_t = \text{compress}(q_t, R_t) \quad \text{s.t.} \quad |c_t| \le W

Here WW is the context-window limit, and RtR_t is persistent memory.

Context and memory lifecycle

Figure 3: Raw history grows quickly. A good harness keeps the working context small and stores full history in durable files.

Sub-agents and backend jobs

Humans do not research everything in one thread. We run parallel searches, experiments, and reviews.

Agent systems can do the same:

Tserial=itiT_{\text{serial}} = \sum_i t_i

but with parallel workers:

Tparallelmaxiti+TmergeT_{\text{parallel}} \approx \max_i t_i + T_{\text{merge}}

The harness then needs a small process manager: launch jobs, inspect logs, cancel bad runs, store results, and merge outputs.

Model Capability vs. Harness Quality

This is not either-or.

A strong model in a weak harness is like a smart student with no notebook, tools, feedback, or exam system.

A weak model in a strong harness still has a ceiling.

A useful toy function is:

Success=σ(aM+bH+cMHd)\text{Success} = \sigma(aM + bH + cMH - d)

Here MM is model capability, HH is harness quality, and MHMH captures the interaction.

Model and harness success surface

Figure 4: Model capability and harness quality multiply. The same model can feel very different in different products.

Harness Optimization

The optimization target has moved:

prompt → structured context → workflow → harness code → optimizer code

Early AI product work optimized prompts. But a prompt is only one part of the system. The deeper design surface includes:

  • what enters context,
  • when tools are called,
  • how failures are classified,
  • what logs are preserved,
  • what edits can be merged automatically,
  • where human review is mandatory,
  • how sub-agents split work,
  • how evaluators are built.

A harness objective can be written as:

J(h;M,D)=ExD[score(Hh(M,x))]λCost(h)μRisk(h)J(h; M, \mathcal{D}) = \mathbb{E}_{x \sim \mathcal{D}} \left[\text{score}(H_h(M,x))\right] - \lambda \text{Cost}(h) - \mu \text{Risk}(h)

So a good harness is not only high-scoring. It must also be cost-controlled and risk-controlled.

Context Engineering

Context engineering turns memory from a growing transcript into a structured, deduplicated, updateable playbook.

The ACE idea can be simplified as:

RoleWhat it doesStudent analogy
GeneratorProduces task trajectoriesSolves problems
ReflectorExtracts lessons from success/failureReviews mistakes
CuratorUpdates structured contextMaintains the notebook

Meta Context Engineering then asks not only "what should be in the notebook?" but "what is the best method for writing the notebook?"

The bi-level optimization is:

Inner: cs=argmaxcsJtrain(cs;s)\text{Inner: } c_s^*=\arg\max_{c_s}J_{\text{train}}(c_s;s)
Outer: s=argmaxsSJval(cs)\text{Outer: } s^*=\arg\max_{s\in\mathcal{S}}J_{\text{val}}(c_s^*)

Inner loop: find the best context for a given context-management skill.

Outer loop: find the best context-management skill.

Workflow Search

Workflow design can also be optimized.

A workflow can be represented as a graph:

W=(V,E)W = (V, E)
  • VV are actions such as model calls, code execution, tests, and review.
  • EE are transitions such as "if tests fail, return to edit."

The search objective is:

W=argmaxWJ(W)W^* = \arg\max_W J(W)

That is why systems such as ADAS and AFlow matter. They treat agent design as a search space, not just a hand-written prompt.

Self-Harness and Permission Boundaries

The Self-Harness pattern is powerful because it is bounded:

  1. Mine failure patterns.
  2. Propose narrow harness edits.
  3. Test on held-in tasks.
  4. Test on held-out tasks.
  5. Merge only if there is no regression.

The danger is equally clear. If a program can freely edit the system that evaluates and constrains it, abstraction boundaries break.

Production systems need at least three boundaries:

BoundaryPurpose
Editable surfaceOnly small parts of the harness can change
Permission controlCritical resources are outside the loop
External evaluationThe evaluator cannot be edited by the thing being evaluated

Self-improvement does not mean self-permissioning.

Evolutionary Search

Some harness questions are hard to optimize with gradients but easy to evaluate.

So we can keep a population of candidate harnesses, mutate them, evaluate them, and preserve the best ones.

One sampling rule can be written as:

P(hi)exp(βJ(hi))1+niP(h_i) \propto \frac{\exp(\beta J(h_i))}{1 + n_i}

High-scoring candidates are sampled more often, but candidates that already have many children are discounted to preserve diversity.

Evolutionary harness search animation

Figure 5: Evolutionary harness search should track score, risk, and diversity. The valuable object is the improving Pareto frontier.

This explains the appeal of AlphaEvolve and Darwin Gödel Machine-style systems. If the task can be evaluated, the system can generate candidates, test them, and keep the better ones.

The limits are just as important:

  • slow evaluation makes search expensive,
  • fuzzy evaluation makes it self-deceptive,
  • bad reward design creates reward hacking,
  • short-term scores can damage long-term maintainability.

The Hardest Part Is Evaluation

Self-improvement is not mainly about generating new ideas. It is about judging whether a new idea is actually better.

If the evaluator is unit tests, the agent may overfit tests.

If the evaluator is another model, it may learn to please that judge.

If the evaluator is a benchmark, it may exploit benchmark artifacts.

The agent optimizes:

hJproxy(h)\nabla_h J_{\text{proxy}}(h)

but the business wants:

hJreal(h)\nabla_h J_{\text{real}}(h)

The danger is:

JproxyJrealJ_{\text{proxy}} \ne J_{\text{real}}

This is Goodhart's law in agent form.

Business Implications

First, do not buy only a model. Buy the operating system around the model.

Enterprise value comes from repeatable workflows, auditable traces, recoverable failures, verified evaluation sets, permission systems, and tool connections to real business data.

Second, traces are assets.

Every agent run records what it read, which tools it used, where it failed, how it recovered, which tests exposed the issue, and which paths were useless. That is not junk logging. It is the raw material for harness improvement.

Third, AI organizations must move from prompt management to harness management.

Prompts can live in documents. Harnesses need version control, tests, monitoring, rollback, permissions, and audit trails.

Investment Lens

This is not investment advice. It is a framework.

If model APIs become more commoditized, compounding value shifts upward into the system layer.

AI moat matrix for harness engineering

Figure 6: Model weights and API access face commoditization pressure. Evaluation systems, trajectory data, workflow ownership, and permission/compliance layers may compound longer.

Ask these questions:

QuestionGood signalDanger signal
Does the company own evaluations?Held-out tests and regression suitesDemo-only claims
Does it store trajectory data?Failures, tool calls, repair pathsOnly final answers
Can the harness improve safely?Small edits, verification, rollbackAgents can freely edit the system
Are permissions externalized?Safety layer outside the loopThe optimized system edits its own evaluator
Is it embedded in workflow?Connected to real business systemsA chat-only surface
Is cost controlled?Routing, caching, parallelism controlEvery task uses the most expensive model

Long-term moats may come from workflow ownership, proprietary evals, task trajectories, deep tool integration, human review nodes, compliance systems, and systematic learning from failure.

One-Sentence Summary

Scaling laws explain how AI capability grows through model, data, and compute.

Harness engineering explains whether that capability can reliably do long work in the real world.

The realistic self-improvement loop is:

better harness → better task traces → better evaluation and memory → better harness

It is less cinematic than an AI rewriting itself overnight, but it may be much closer to where AI products compound today.

References

Related posts