The harness Guide 05 of 25
The agent harness: everything around the model
Two products built on the same model can behave like different species. The difference is the harness — the loop, tools, context, permissions, and checks that turn a model into a worker.
Key takeaways
- Every agent is two things: a model and the harness around it. Changing only the harness lifted one coding agent from 52.8% to 66.5% on a public benchmark.
- The core loop is a few lines of code. Production harnesses differ in context management, sandboxing, recovery, verification, extension points, and cost control.
- Keep agent state in files, git, and pass/fail checklists rather than the conversation, so long runs survive compaction and context resets.
- Never let the working agent be its own judge. Separate evaluators, end-to-end tests, and traces catch confident but wrong claims of done.
- Build, adopt, or buy is a harness decision. Own the loop when control matters, adopt a vendor SDK to move faster, and re-test either on every model upgrade.
On this page 9 sections
In a write-up published on 17 February 2026, LangChain’s engineers reported raising their coding agent’s score on Terminal Bench 2.0, a benchmark of 89 command-line tasks, from 52.8% to 66.5%. They kept the model fixed at GPT-5.2-Codex and changed only the code wrapped around it. The model was identical; the agent was not.
An agent is a model that chooses its own next step and tool in a loop; our guide to what an AI agent is draws the line between agents and fixed workflows. This guide is about everything wrapped around that loop: the harness. It covers the tools the model may use, the context it sees, the permissions it runs under, and the checks that decide when work is finished.
For engineering leaders, this reframes the build-or-buy decision. “Which model?” is half the question. The other half — write the harness, adopt a vendor’s, or buy a finished agent — is mostly ordinary software engineering.
What is an agent harness?
A large language model (LLM) on its own turns text into more text. It cannot run a command, open a file, or remember last week. The harness grants those abilities, limits them, and records what happened.
A July 2026 source-code study of eleven production coding agents, including Claude Code and the command-line agents Codex CLI and Gemini CLI, frames it as a simple sum: agent equals model plus harness. Coding agents dominate the public evidence, but support, finance, and operations agents need the same parts.
The study maps seven subsystems that every harness implements in some form. Here they are in our words, with the failure you see when each is thin:
| Subsystem | Its job | Typical failure when it is weak |
|---|---|---|
| Agent loop | Calls the model, runs requested actions, feeds results back, decides when to stop | Runaway runs; no recovery after an error |
| Model integration | Connects to the model provider, assembles each request, and handles caching and reasoning settings | Slow, costly calls; broken caching |
| Tools and actions | Declares the actions on offer, validates each call, and carries it out | The model describes work it cannot perform |
| Memory and context | Budgets the window and carries knowledge from one session to the next | Lost decisions, repeated work, decay on long tasks |
| Safety and permissions | Sorts each action into allowed, needs approval, or blocked, and confines execution | Destructive commands, leaked data |
| Orchestration | Hands subtasks to sub-agents and collects their results | One overloaded context doing everything |
| Extensibility | Lets teams add tools, instructions, and hooks without forking | Every change needs a new release |
The leanest system in the study, mini-SWE-agent, covers all seven in roughly 100 lines of Python. On SWE-bench Verified, a benchmark built from real GitHub issues, its self-reported results sit near those of harnesses with roughly a thousand times more code, though the figures come from different models and dates. The authors conclude that safety, recovery, cost management, and extensibility, not task completion, separate that floor from production systems.
The agent loop: a simple core with expensive habits
OpenAI’s walkthrough of the Codex agent loop (23 January 2026) shows the pattern every harness shares. The harness sends instructions, context, and tool definitions to the model. If the reply requests a tool, the harness runs it, appends the output, and calls the model again; a plain-text reply ends the turn.
messages = [instructions, env_context, user_request]
for step in range(MAX_STEPS):
reply = model.generate(messages, tools=TOOL_SCHEMAS)
# A plain-text reply ends the turn.
if not reply.tool_calls:
return reply.text
for call in reply.tool_calls:
if permission_gate(call) == "deny":
result = "Refused: " + policy_reason(call)
else:
# May pause here for human approval.
result = run_in_sandbox(call)
# Append only: keeps the cached prefix intact.
messages += [call, result]
if token_count(messages) > COMPACT_AT:
messages = compact(messages)
escalate_to_human("step limit reached")
Three costs hide in those lines. First, every call resends the whole, ever-longer history, so the data sent over a run grows roughly quadratically with the number of steps. Prompt caching, which reuses work the provider already did on an identical prefix, keeps that affordable; OpenAI says cache hits make Codex’s sampling cost linear instead.
Second, the window fills, so Codex compacts automatically past a set threshold, replacing history with a shorter summary. Third, the loop needs explicit exits. Step limits, spend limits, and a defined escalation path stop a confused agent from burning budget all night.
Model integration: one provider’s dialect, or many
Caching only works if each request begins with exactly the same tokens as the last, which makes cache discipline a harness job. The Codex post names swapping the tool list, the model, or sandbox settings mid-conversation as cache breakers, so Codex records such changes as new messages instead of editing earlier ones.
Harnesses also tune prompts per model; Codex bundles model-specific instructions. The arXiv study describes the trade-off: Claude Code and Codex exploit provider features such as caching and extended thinking at the cost of lock-in. Provider-agnostic harnesses give those optimizations up. LangChain saw the effect directly: Claude Opus 4.6 scored 59.6% on an earlier version of its harness, which LangChain attributes to not running the same tuning loop with Claude.
Tools and the tool registry
The tool registry is the set of definitions the harness sends with each request: a name, a description, and a parameter schema for every action. In Codex, it merges built-ins such as a shell, provider-hosted tools such as web search, and tools from Model Context Protocol (MCP) servers. Claude Code, in the study’s July 2026 snapshot, had 43 typed tools but loaded only a core subset up front and let the model search for the rest.
Every definition costs tokens and is one more decision the model can get wrong, so a few sharp tools beat a sprawling catalog. Error messages are part of the interface, too. An OpenAI team that built an internal product entirely with Codex uses custom linters whose failure messages tell the agent how to fix the problem.
Treat tool output as untrusted input. A fetched web page, email, or ticket can carry instructions aimed at the agent, which is why the permission layer matters even for tools that only read.
Context, memory, and long-running agents
The context window is the harness’s scarcest resource, and deciding what fills it at each step is a discipline of its own, covered in our guide to context engineering. Memory is whatever survives outside the window: progress files, task lists, commit history, and notes the next session can read.
Anthropic’s March 2026 write-up shows how that design evolves. Its earlier harness used an initializer agent to turn a product spec into a task list, then a coding agent that built one feature at a time and left handoff artifacts for the next session. Claude Sonnet 4.5 wrapped up early whenever it sensed its context limit approaching, so that harness used full context resets with a structured handoff rather than compaction alone.
Resets cost extra orchestration, tokens, and latency. Claude Opus 4.5 largely lost the early-exit habit, so the newer harness dropped resets and ran as one continuous session, with the Claude Agent SDK’s automatic compaction handling growth.
Orchestration and extensibility
Orchestration hands a subtask to a sub-agent with a fresh context and takes back only the result. Done well, it keeps the main context clean; done early, it multiplies tokens and coordination failures. The arXiv study advises staying single-agent until a breadth-first phase, such as wide research, clearly benefits from parallel contexts; our guide to multi-agent systems weighs that evidence.
Extensibility is how a team changes behavior without forking the harness. By July 2026, the study found lifecycle hooks — code that runs before or after a tool call, or when the agent tries to stop — in nine of its eleven systems. Nine also supported skills: folders of instructions, scripts, and metadata that load only when a task needs them. MCP servers are the third extension point, adding tools without a release.
The pattern is not specific to coding. Shopify’s Sidekick, an assistant that acts on a merchant’s store, hit a wall as its tools grew into the dozens and its system prompt filled with special cases. Its fix was to return instructions alongside tool output, exactly when they apply, which kept the core prompt focused and let rules change without breaking prompt caches. Our guide to agents in production collects more lessons like it.
Permissions, sandboxing, and human checkpoints
The permission layer decides, for every action, whether to run it, ask a human, or refuse. Asking about everything fails predictably: people stop reading the prompts. Anthropic’s Claude Code sandboxing (20 October 2025) replaced most prompts with boundaries — writable directories and approved network hosts — enforced by the operating system for every child process.
In Anthropic’s internal use, that cut permission prompts by 84%. Neither boundary works alone: file limits without network limits still let a hijacked agent send secrets such as Secure Shell (SSH) keys off the machine. Check what a sandbox covers, too. In Codex, as of January 2026, the sandbox wraps the built-in shell tool, while tools supplied by MCP servers must enforce their own guardrails.
Reserve human checkpoints for irreversible, expensive, or customer-visible actions — payments, deletions, outbound messages, deploys — and for agents stuck after repeated failures. Elsewhere, let sandboxes and tests do the policing.
Verification, traces, and evals
Agents grade their own work generously. In Anthropic’s March 2026 experiments, agents reviewing their own output praised mediocre results, and a separate, skeptical evaluator proved far easier to build. On Claude Opus 4.5, a planner–generator–evaluator harness ran six hours and cost $200, against 20 minutes and $9 solo — but only the harnessed game actually responded to its controls.
LangChain’s gains came from the same instinct. Its most common failure was an agent rereading its code, deciding it looked fine, and stopping, so a hook now intercepts the exit and demands verification. Loop detection nudges an agent that keeps editing one file, and spending the most reasoning effort on planning and verification, with less in between, produced the final 66.5%.
Observability is how you find fixes like these. LangChain stores every agent action with its latency, token count, and cost, then has agents analyze failed traces and propose harness changes, which a human can review. The arXiv study found Gemini CLI emitting OpenTelemetry traces with per-token cost, and the OpenAI Agents SDK builds tracing in. The all-Codex team at OpenAI even gave the agent its own logs, metrics, and traces, so it could check its own performance targets.
A model’s benchmark score predicts little about the agent, and one good run proves little; our guide to evals explains how to measure consistency.
Build, adopt, or buy the harness?
The arXiv study draws the key distinction. A framework is a library you import to build an agent, while, in its words:
“a harness is a runtime the developer works inside of”
None of the eleven agents it examined imported a general-purpose framework such as LangGraph or Google’s Agent Development Kit (ADK); each ran a hand-written loop. By mid-2026 the categories were merging: Anthropic’s Claude Agent SDK packages Claude Code as a library, and LangChain shipped Deep Agents, a harness built on LangGraph. As of October 2026, the choice looks like this:
| Option | Examples | What you get | What you take on |
|---|---|---|---|
| Your own loop | About 100 lines to start | Full control of context, permissions, and traces | You own compaction, sandboxing, recovery, and tracing |
| A vendor SDK | Claude Agent SDK, OpenAI Agents SDK | A tested loop plus tools, hooks, sub-agents, and tracing | The SDK’s defaults, and provider lock-in where it serves one vendor’s models |
| A finished agent | Claude Code, Codex | The fastest start, with the vendor’s harness tuning | The least control over behavior, data flow, and cost |
The OpenAI Agents SDK, for example, ships the loop with tools, MCP support, handoffs between agents, guardrails, sessions for memory, and tracing. Pick by risk: a support or finance agent that touches customer data usually needs your own permission model and audit trail, even when an SDK runs the loop.
What this means for your team
- Evaluate the system, not the model. Pin model and harness versions together, and treat a model swap as a release that reruns your evals. A harness tuned on one model is not neutral for the next.
- Start thin and add parts for observed failures. A loop, a few tools, step limits, and logging are enough to learn from. When the agent struggles, find the missing tool, guardrail, or document, as OpenAI’s Codex team does.
- Keep state outside the conversation. Progress files, pass/fail checklists, git commits, and tickets survive compaction and resets. Chat history does not.
- Separate doing from judging. Use tests, end-to-end checks, or a dedicated evaluator. An agent’s claim that it is finished should never close a task on its own; coding agents show the discipline clearly.
- Prefer boundaries to prompts. Sandbox the filesystem and network first, and reserve approvals for irreversible actions. Inventory every tool, MCP servers included, by what it could damage.
- Watch cost and caching. Keep instructions and tool lists stable within a session, log tokens per task, and decide what a successful run is worth.
- Re-test the scaffolding on each upgrade. Anthropic’s team dropped its sprint structure once Claude Opus 4.6 could handle the work without it. Parts that once compensated for a weakness become overhead when the weakness goes.
The unglamorous parts are the product. When we build production agents at SurgeX Labs, most of the engineering time goes into this layer, not the prompt.
Sources
-
Improving Deep Agents with harness engineering (opens in new tab) — LangChain, 2026 langchain.com
-
Harness Engineering: Anatomy, Architecture, and Evolution of Coding Agents — A Source-Code Study of Eleven Systems (opens in new tab) — Barbaste et al., Wavestone AI Lab (arXiv), 2026 arxiv.org
-
Unrolling the Codex agent loop (opens in new tab) — OpenAI, 2026 openai.com
-
Harness engineering: leveraging Codex in an agent-first world (opens in new tab) — OpenAI, 2026 openai.com
-
Harness design for long-running application development (opens in new tab) — Anthropic Engineering, 2026 anthropic.com
-
Beyond permission prompts: making Claude Code more secure and autonomous (opens in new tab) — Anthropic Engineering, 2025 anthropic.com
-
Building production-ready agentic systems: Lessons from Shopify Sidekick (opens in new tab) — Shopify Engineering, 2025 shopify.engineering
-
OpenAI Agents SDK documentation (opens in new tab) — OpenAI, 2025 openai.github.io