Diagram of a model at the center, surrounded by a harness of tools, context, memory, guardrails and verification feeding back into the loop
Back to Work & InsightsComprehensive Guide

Harness Engineering: The Code Around the Model

An agent is a model plus a harness: the loop, tools, context, memory, permissions, and checks that surround it. Two teams using the same model can get very different results. The difference is usually the harness.

TL;DR

  • Agent = model + harness. The harness is everything except the weights: the agent loop, the tools, what goes into the context window, what is remembered between sessions, what the agent may do, and how its work is checked.
  • Prompt engineering tuned one message. Context engineering chose what the model sees. Harness engineering designs the whole environment the agent works in, across many steps and sessions.
  • Most agent failures in production are harness failures: confusing tools, overflowing context, no memory of earlier progress, no way to verify work, or too much permission.
  • Treat the harness as a product: give agents clear tools and maps, durable progress notes, fast automated checks, and tight permissions, then improve it from traces and evals.
Agent reliability · Typical API wrapper

Write a longer system prompt and switch to a bigger model

Agent reliability · FDE architecture

Engineer the harness: tools, context, memory, guardrails, and feedback loops

01 · Definition

The parts of a harness

The model decides what to do next. The harness decides what the model can see, what it can touch, and how it finds out whether it was right.

Agent loop
think → call tool → observe → repeat

The control flow that runs the model step by step, handles tool calls, and decides when to stop, retry, or ask a human.

Tools
search_orders(customer_id, status)

The actions the agent can take. Names, parameters, descriptions, and error messages are the agent's interface, so design them like an API for a new teammate.

Context management
compact · summarize · load on demand

What goes into the context window at each step: instructions, relevant files, tool results. Old, noisy output is trimmed or summarized so the window doesn't fill with junk.

Memory & state
progress.md · task list · git history

Durable notes that survive between sessions, so a fresh context can pick up where the last one stopped instead of starting over.

Guardrails & permissions
sandbox · allowlist · approval for writes

Limits on what the agent can do and where. Reading is usually safe; deleting, paying, emailing, or deploying needs scoping or a human approval.

Verification
tests · linters · schema checks · evals

Automatic feedback the agent can run itself. An agent that can check its own work fixes its own mistakes; one that can't just reports success.

The term grew out of coding agents. Teams building with tools like Claude Code and Codex found that a better model helped, but the big gains came from the environment around it: clear repository maps, fast tests, progress files, and rules enforced by tooling rather than written in a prompt. The same lessons apply to any agent that works over many steps.

02 · Worked example

A long-running coding task, two ways

The task: add multi-currency support to an invoicing app. It touches the database, the API, the UI, and the tests. It won't finish in one context window.

“Add multi-currency support to invoices.” (a multi-hour task)
Bare model + big prompt
Starts strong, then drifts
  1. 01Reads a 3,000-word system prompt and half the repo
  2. 02Context fills with old file dumps and test logs
  3. 03New session starts with no memory and redoes finished work
  4. 04Declares “done” without running the tests
Engineered harness
Small steps, checked and recorded
  1. 01Short AGENTS.md map points to the docs for each area
  2. 02A feature list in a file; the agent takes one item at a time
  3. 03After each item: run tests, commit, update progress.md
  4. 04Next session reads progress.md and git log, then continues

Same model in both columns. The right side succeeds because the harness keeps the context clean, makes progress durable, and lets the agent verify its own work.

async function runAgent(task: Task, harness: Harness): Promise<Result> {
  let context = harness.initialContext(task)            // map, progress notes, task

  for (let step = 0; step < harness.maxSteps; step++) {
    const action = await model.next(context)
    if (action.type === "finish") {
      const check = await harness.verify(task)          // tests, lint, evals
      if (check.passed) return harness.saveProgress(task, check)
      context = harness.append(context, check.report)  // feed failures back
      continue
    }
    if (!harness.isAllowed(action)) {                   // permissions, sandbox
      context = harness.append(context, harness.denied(action))
      continue
    }
    const result = await harness.runTool(action)
    context = harness.compact(harness.append(context, result))
  }
  return harness.handOff(task, context)                 // ask a human
}
The core loop is small. Most harness work lives in the tools, the context policy, and the checks.
03 · Failure modes

Where agents fail because of the harness

Tools the model can't use

Forty overlapping tools with vague names; errors return a stack trace.

Fewer, clearly named tools with examples, and error messages that say what to do next.

Context rot

Every file read and test log stays in context until the important instructions are buried.

Trim or summarize old tool output, and load details on demand instead of up front.

Amnesia between sessions

A new context window starts from zero and rewrites work that was already done.

Keep progress notes, a task list, and commits the next session reads first.

Encyclopedia prompts

A giant instruction file covers everything, so the model follows none of it reliably.

A short map that links to focused docs, with hard rules enforced by linters and tests.

No way to check

The agent can't run the app or the tests, so it guesses and reports success.

Give it fast, deterministic checks it can run after every change.

Too much power

The agent has production credentials and runs a destructive migration.

Sandbox by default, least-privilege credentials, and human approval for irreversible actions.

04 · Decision

How much harness do you need?

Your score: 0 / 6

Keep it simple

For short, single-step tasks, a good prompt, a few well-described tools, and basic logging are enough. Don't build infrastructure you won't use.

05 · Trade-off

What it costs

Engineering time
Tools, checks, sandboxes, and memory are real software to build and maintain. The payoff comes from running the agent many times.
Speed vs. safety
Approval steps and sandboxes slow the agent down. Tier permissions so routine actions run freely and risky ones pause.
Model changes
Workarounds for one model's weaknesses can get in the way of the next model. Revisit the harness when you upgrade, and remove scaffolding that is no longer needed.
Observability
You can't improve a harness you can't see. Full traces of every step cost storage and need privacy handling, but they are where the improvements come from.

A useful rule: when the agent makes a mistake, ask what in the environment let it happen, and fix that, rather than adding another sentence to the prompt. A missing test, a confusing tool, or an unclear doc usually explains the failure better than the model does.

06 · Playbook

How to build a harness

  1. 01Pick one real, repeated task and collect 20 to 30 examples with known good outcomes.
  2. 02Run the agent on them with full trace logging, and label where each failure came from.
  3. 03Fix the tools first: clear names, fewer overlaps, helpful error messages.
  4. 04Add checks the agent can run itself, and make it run them before it reports “done”.
  5. 05For long tasks, add a task list and progress notes the agent updates and reads at the start of each session.
  6. 06Set permission tiers and a sandbox, re-run your examples after each change, and track the success rate over time.

Tooling options

Ready-made harness
Claude Agent SDK

The harness behind Claude Code as a library: agent loop, tools, context compaction, permissions, and hooks.

Agent framework
OpenAI Agents SDK

Agents, tools, handoffs, guardrails, and built-in tracing for multi-step workflows.

Tool interface
MCP

An open protocol for exposing tools and data to agents once, then reusing them across harnesses.

Traces & evals
LangSmith / Langfuse

Record every step an agent takes, review failures, and run evals as you change the harness.

More guides

Want this built into your workflow?

I help small and mid-size teams decide what to build, then build it, from the first eval set to production guardrails.

Talk through your use case