Writing
Harness Engineering: Control What Happens During a Run
The harness is the runtime around the model: permissions, sandboxing, state, timeouts, approvals, logs and recovery. What it should enforce even when the model makes a bad decision, worked through a coding agent fixing a sorting bug.
By Ka Lun Chan · Cloud & AI · AI / Architecture
What is the harness?
The harness is the runtime around the model. It starts the run, hands the model its instructions and tools, executes each tool call, enforces permissions and sandboxing, keeps state, applies timeouts and budgets, pauses for human approval, records what happened and decides what to do when something fails. Its most important job is enforcing the rules that must hold even when the model makes a bad decision.
Part of a series on what it takes to make an AI system reliable. A tool defines an operation, covered in tool engineering. The harness governs whether and how that operation runs.
Anthropic uses the term “agent harness” in its engineering writing, and Claude Code, Codex and similar coding agents are harnesses wrapped around a model. You can use one off the shelf, configure it, or build your own. Either way, the question this part answers is the same: which decisions do you leave to the model, and which does the runtime make for it?
The model decides how to do the work. The harness decides what the work is allowed to touch, how long it can take, what it can spend, when a person has to say yes, and what gets written down. None of those should depend on the model choosing well.
One run, start to finish
The illustrative scenario: a ticket says the Quantity column on the Orders page sorts as 1, 10, 2, and a coding agent is asked to fix it. The agent has five tools: search, read, replace text, run tests and open a draft pull request. Here is the harness around it.
| Concern | Setting | What it guarantees |
|---|---|---|
| Permissions | Read anywhere in the repo; write only under src/orders/ and src/table/; no shell | An edit to any other path is refused, whatever the agent intended |
| Sandbox | Runs in a container with no network and no credentials except the pull request token | An injected instruction to fetch or upload something has nowhere to go |
| State | A fresh worktree from the main branch, the environment files the build needs, a progress file, one commit per attempt | Every attempt can be inspected or reverted |
| Timeouts | Two minutes per test run, forty minutes per task | A hung process or a loop that never ends stops |
| Budget | A token and cost ceiling for the task | The run stops and reports instead of spending more |
| Approvals | A person approves before the draft pull request opens, and before any test file changes | The agent can propose; it can’t ship or rewrite the checks alone |
| Logs | Every model call and tool call, with inputs, results, timing and tokens | Someone can answer “why did it do that?” later |
| Recovery | Failed runs keep their worktree and log; the next attempt starts from the last good commit | A failure leaves evidence instead of a mess |
As configuration, that might look like the sketch below. The format is invented; most harnesses have an equivalent for each line.
task: fix-orders-quantity-sort
workspace:
worktree_from: origin/main
copy_ignored: [.env.test]
permissions:
read: ["**"]
write: ["src/orders/**", "src/table/**"]
require_approval: ["**/*.test.*", "open_draft_pr"]
sandbox:
network: none
limits:
tool_timeout: 120s
task_timeout: 40m
max_cost_usd: 5
on_failure:
keep_worktree: true
notify: ownerClaude Code is a useful real reference for several of these. Its permission rules are evaluated deny first, then ask, then allow, and its documentation says plainly that the rules are enforced by Claude Code, not by the model. Its sandbox for shell commands is enforced by the operating system, with network traffic going through a proxy. That sandbox is off by default and covers shell commands only. The file tools, hooks and MCP servers run outside it, which is exactly the kind of detail you have to read before trusting a boundary.
What should the runtime enforce even when the model decides badly?
Anything whose failure would be expensive, irreversible or invisible: which files and systems the agent can change, which credentials it holds, where it can send data, how long and how much it can run, which actions need a person’s approval, and whether its claim of success is checked independently. A prompt can ask for those things. Only the runtime can guarantee them.
Four bad decisions the sorting agent could plausibly make, and what stops each one:
- The first fix fails a test, and the agent decides the test is wrong and changes the expected order to match its output. The harness requires approval for any change to a test file, so the edit stops at a person who can see what happened.
- The ticket, filed through a public form, contains text asking the agent to “also update the deploy settings.” The agent can’t write outside two directories and has no network, so the injected instruction has no way to act.
- The agent keeps trying variations of a broken idea. The task timeout and cost ceiling end the run, and the failure is reported with the log attached.
- The agent reports that it fixed the bug. The harness runs the verification itself instead of accepting the report, because Anthropic’s writeup on long-running harnesses names exactly this failure: agents declaring work finished, or marking a feature complete, without proper testing.
OWASP’s guidance on excessive agency says to enforce authorization downstream and require human approval for high-impact actions. The harness can require those approvals even when the model’s instructions are wrong.
Hooks are a useful mechanism here, with a caveat. Claude Code lets a hook run before a tool call and deny it, which is a good place for a rule like “no edits to test files without approval.” Its hook types also include prompts to a model, and a model-based check is advisory, not a hard boundary. For rules that must hold, use a deterministic check.
State and the environment
Give each run its own working copy. Separate Git worktrees keep agents from picking up one another’s half-finished changes and make it easier to attribute a diff to a run.
A fresh worktree contains tracked files. Provision its gitignored environment separately and validate the rendered output with the intended configuration. For example, omitting production configuration can change a prerendered contact page without causing a compilation failure.
State within the run matters too. Anthropic’s long-running harness keeps a progress file, a feature list in JSON where each item starts as failing, and a git commit after each piece of work, so a later session can see what was done and revert a bad change. For a bug fix the same idea is smaller: a progress note with the root cause and what has been tried, and one commit per attempt.
Timeouts and budgets
Put a timeout on each call and a separate one on the run. A hung browser process and repeated attempts at the same fix are different failure modes, and either can exhaust a run’s budget.
The loop decides how many attempts a task deserves. The harness independently enforces its time, permission and spending limits.
Logs and recovery
The log is how you debug an agent. Record every model call and tool call with its inputs, result, duration and token count, and keep it with the run. When the sorting agent opens a pull request that changes the wrong file, the first question is what it read before it decided, and only the log can answer that.
Logs also feed evaluation. A failed run’s transcript, with the input that caused it, is the raw material for a regression case.
Recovery is the last thing the harness decides. A failed run should say that it failed, keep its worktree and log for inspection, and leave nothing half-applied in a shared place. The next attempt starts from the last good commit, with a note of what was tried. A run that dies silently and leaves a branch with half a fix on it is worse than one that never started.
Sources
Checked in November against the documentation below. The run configuration and the bad decisions are illustrative. The worktree environment-file problem happened on this site.
Short answers
What is an agent harness?
The harness is the software that runs an AI agent: it gives the model its instructions and tools, executes the tool calls, enforces permissions and limits, keeps state between steps, pauses for approvals, records what happened and decides what to do when something fails. Claude Code and similar coding agents are harnesses around a model.
What is the difference between a tool and a harness?
A tool defines one operation and its inputs and outputs. The harness decides whether that operation may run right now, under what limits, with what credentials, and what gets recorded. A tool can refuse a path outside the repository; the harness can require approval before any pull request opens.
Before an agent runs without you
List the decisions you are currently trusting the model to make well. Move every one that would be expensive, irreversible or invisible into the runtime.
Parallel runs make worktree setup and isolation more important. What Is Orca? examines a tool for coordinating those runs.