← All posts

AI agents › Harnesses and loops

What is harness engineering? Designing where AI agents work

Harness engineering means designing the environment an AI agent works in, from instruction files and permissions to checks and hooks, and why the term spread.

If you’ve been reading about AI coding agents this year, you’ve probably run into the word harness. It shows up in engineering posts from OpenAI and Anthropic and in an article on Martin Fowler’s site, and it travels around in a short formula: Agent = Model + Harness. But if you’re hearing it for the first time, it may not be obvious what the word actually covers.

Harness engineering is designing the environment an AI agent works in. It covers the instruction files the agent reads first and the tools it may use, along with their permissions. It also covers the tests and linters that tell it when it’s wrong, the hooks that run your scripts at set moments, and the sandbox that limits what its commands can reach. In everyday English, a harness is the gear that lets a person guide a horse’s strength. In the same way, the model brings the ability, and the harness keeps that ability pointed at the work. That’s why many explainers describe an agent as a model plus a harness. The phrase spread quickly in early 2026, perhaps because agents began taking on longer work with less supervision and more people asked what should surround the model so its results can be trusted.

What the word harness covers

The broadest definition comes from The Anatomy of an Agent Harness, which Vivek Trivedy wrote for LangChain on March 10, 2026. If you’re not the model, it says, you’re the harness, so every piece of code, configuration, and execution logic that isn’t the model itself counts as harness. The Claude Code glossary is more concrete, describing Claude Code as the harness and Claude as the model inside it. By that account, file access, shell execution, permission gating, memory loading, and the loop that chains actions together are the harness’s job.

Birgitta Böckeler of Thoughtworks adds one more distinction in Harness engineering for coding agent users on martinfowler.com. Part of the harness, such as the system prompt, is already built in by the people who make the tool. On top of it, users build an outer harness for their own project. From here on, harness mostly means that outer layer, since it’s the part you can shape yourself.

How the term spread

The word harness itself was in use before the phrase. Anthropic, for example, had already published Effective harnesses for long-running agents in November 2025. The phrase harness engineering, though, started appearing in one post after another in February 2026.

First, on February 5, developer Mitchell Hashimoto titled step five of My AI Adoption Journey Engineer the Harness. “I don’t know if there is a broad industry-accepted term for this yet, but I’ve grown to calling this ‘harness engineering,’” Hashimoto writes. The idea is that whenever an agent makes a mistake, you take the time to engineer a solution so that the agent never makes that mistake again. The post names two forms, updating AGENTS.md and building actual tools, such as scripts that run filtered tests.

Six days later, on February 11, OpenAI published a post by Ryan Lopopolo called Harness engineering. Böckeler presents it as an OpenAI team documenting what their harness looks like. In that summary, the team enforced a layered architecture with custom linters and structural tests. Böckeler also quotes its conclusion: “Our most difficult challenges now center on designing environments, feedback loops, and control systems.” Böckeler’s own memo came out on February 17, and the full article replaced it on April 2. That article opens by explaining why the topic matters: to let coding agents work with less supervision, we need ways to increase our confidence in their result.

What goes into a harness

Lined up by role, the pieces these sources keep returning to look like this. The right column shows where each one lives in Claude Code.

Piece What it does In Claude Code
Instruction files Tell the agent the project’s rules before it acts CLAUDE.md, AGENTS.md
Tools and permissions Decide what it may do and what needs asking first --allowedTools, allow and deny rules
Checks Tell it when it’s wrong Tests, linters, type checkers
Hooks Run your own scripts at fixed moments PreToolUse, PostToolUse
Isolation Limit which files and hosts its commands reach The Bash sandbox
Records Carry what happened over to the next session Progress files, git commits

Böckeler sorts these into two kinds. Guides set the direction before the agent acts, and sensors look at what it did afterward so it can correct itself. An instruction file is a guide, while tests and linters are sensors.

Each piece holds the agent in a different way, though. AGENTS.md describes itself as a README for agents, and Claude Code reads it as the project’s instructions when a repository has no CLAUDE.md. But according to the Claude Code memory docs, Claude treats these files as context, not enforced configuration. So a rule that must never be broken belongs in a setting or a hook, which the client enforces. Permission rules are checked in the order deny, then ask, then allow, so a narrow allow rule can’t open a hole in a broad deny rule. The Bash sandbox lets the operating system limit which files and network domains shell commands reach. Records matter because, as the Anthropic post explains, each new session begins with no memory of what came before.

Trying a small harness with a hook

Let’s attach one small sensor to Claude Code. A hook is a command Claude Code runs automatically at a set point in a session, and PostToolUse is the event that fires right after a tool succeeds. Here is hooks.json, written in the format the hooks reference uses.

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "python3",
            "args": ["${CLAUDE_PROJECT_DIR}/.claude/hooks/check_money.py"]
          }
        ]
      }
    ]
  }
}

The matcher Edit|Write runs the hook only after a file is written or edited, and ${CLAUDE_PROJECT_DIR} becomes the project folder. The rule is one I made up for this example: money stays in decimal.Decimal, so the code shouldn’t call round() or float(). This is .claude/hooks/check_money.py.

# PostToolUse hook: lint the Python file Claude just wrote or edited.
import ast, json, os, sys

event = json.load(sys.stdin)
path = event["tool_input"]["file_path"]
if not path.endswith(".py"):
    sys.exit(0)

problems = []
for node in ast.walk(ast.parse(open(path).read())):
    if isinstance(node, ast.Call) and getattr(node.func, "id", None) in ("round", "float"):
        problems.append(
            f"{os.path.basename(path)}:{node.lineno}: {node.func.id}() used on money. "
            "This project keeps money in decimal.Decimal. Round with "
            "Decimal(...).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)."
        )

# Keep a record of every check, so you can see what happened later.
log = os.path.join(os.environ["CLAUDE_PROJECT_DIR"], "checks.log")
with open(log, "a") as f:
    f.write(f"{os.path.basename(path)}: {'FAIL' if problems else 'OK'}\n")

if problems:
    print("\n".join(problems), file=sys.stderr)
    sys.exit(2)  # exit 2 on PostToolUse: Claude Code shows stderr to Claude

When a PostToolUse hook exits with code 2, the file it checked stays written, but Claude Code shows the hook’s stderr to Claude. That’s why the message also says how to fix the problem. Böckeler calls linter messages like this a positive kind of prompt injection. Each result also goes into checks.log.

Then I asked for a function, without mentioning the rule in the prompt or in any instruction file.

claude -p "Write pricing.py with a function apply_discount(price, percent) that returns the price after a percent discount, rounded to cents." \
  --model haiku --settings hooks.json --allowedTools "Write" "Edit" < /dev/null

--settings loads the hook for this run only, and --allowedTools lets the two file tools run without asking. In a -p run, a call that needs approval and isn’t on that list is denied by default (headless docs). Claude’s first version did its math in Decimal but turned the result back into a float with float(...) on its last line. The hook caught that line and sent back this message.

pricing.py:7: float() used on money. This project keeps money in decimal.Decimal. Round with Decimal(...).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP).

Claude then rewrote the file to return the Decimal as it is. Of the three paragraphs claude -p printed at the end, here is the second, where Claude talks about the hook.

The first version converted the result to `float`, and the project's `check_money.py` hook blocked that. Money stays in `Decimal` in this project, so the function now returns the `Decimal` directly. Callers that need a float will have to convert it themselves.

checks.log recorded one failure and one pass, and the whole run took under ten seconds.

pricing.py: FAIL
pricing.py: OK

Claude wrote that the hook blocked the first version, but a PostToolUse hook runs after the tool and can’t block it. It only hands feedback back, and the agent decides whether to act on it. And since this check looked only at how the code was written, a test that actually calls apply_discount would be the next sensor to add.

How it relates to prompt, context, and loop engineering

Prompt engineering shapes a single instruction, and context engineering shapes everything the model sees at each step. Böckeler describes building a harness for a coding agent as a specific form of context engineering, and context engineering as the means of making guides and sensors available to the agent. The hook message above reached Claude as context too. Still, permission rules and the sandbox stop an action whatever the model decides, so a harness also holds parts that work outside what the model sees. Loop engineering sits one step further out, deciding when an agent with its harness runs, how many times, and when it stops.

What a good harness needs, and common mistakes

The conditions come down to a few things. Hashimoto suggests giving the agent fast, high quality tools that automatically tell it when it’s wrong. Böckeler says to keep guides and sensors together, because with feedback alone an agent keeps repeating the same mistakes, and with guides alone it never finds out whether the rules worked. Both also expect a harness to grow out of real mistakes. Hashimoto writes that each line in an AGENTS.md from Ghostty, a terminal emulator, is based on a bad agent behavior.

The common mistakes sit on the other side. One is writing a rule that must always hold only in an instruction file, and another is writing a long instruction file up front. The Claude Code docs suggest keeping each CLAUDE.md under 200 lines, because longer files use more context and are followed less reliably. A check that only says “failed” leaves the agent to guess the cause, and a sandbox that is off by default and covers shell commands only won’t stop everything. Böckeler also notes that a good harness should not necessarily aim to fully eliminate human input, but to direct it to where our input is most important.

When it pays off and when it doesn’t

A harness pays off most when you work with agents on the same codebase again and again. Mistakes tend to repeat there, so a check you write once keeps working, and a project’s .claude/settings.json can be committed with its hooks so the whole team shares them. For a one-off task, perhaps it won’t repay the effort. The codebase matters too. As Böckeler puts it, a strongly typed language comes with type checking as a sensor, while legacy code with a lot of technical debt is where the harness is most needed and hardest to build.

If you’re trying it for the first time

If you’re new to this, I’d suggest starting small. The next time an agent repeats a mistake you’ve seen, first add one line about it to AGENTS.md or CLAUDE.md. When a rule must hold every time, connect a linter or test you already have to a PostToolUse hook as above, and make its message say how to fix the problem. The hooks guide has more examples, such as running a formatter after every edit or blocking edits to protected files. I ran this example on October 27, 2026, on macOS with Claude Code 2.1.304, where --model haiku ran as claude-haiku-5-5 and python3 was Python 3.13.

AI tools helped with the research for this post.