Harness Engineering
In this blog we will see what a harness is, and the tricks that let companies generate so much code with agents.
In February 2026, OpenAI wrote that three of its engineers had shipped about a million lines of code, and agents wrote every line.[1] The same month, LangChain moved its coding agent from the Top 30 to the Top 5 of Terminal Bench 2.0, without changing the model.[2]
Both teams changed the same thing: the code around the model. Engineers who let agents write code at that scale faced three problems:
- Agents lost track of long tasks once their context window filled up.
- Every tool and every step cost tokens, and the context ran out.
- Agent-written code piled up bloat over time.
In this blog we will see what a harness is, and the tricks that let companies generate so much code with agents.
1. The harness
The problem is that AI agents are unreliable by default. They hallucinate file paths, violate module boundaries, and accumulate dead code over time.
The solution is harness engineering. A horse is powerful but directionless. The harness channels that power into useful work.
LangChain's definition is one line: "Agent = Model + Harness".[3] Everything that isn't the model is the harness:
- The system prompt.
- The tools and their descriptions.
- The sandbox and the file system.
- The logic that hands work to subagents.
- The hooks that run on every step.
So the harness is the engineers' work. OpenAI's post puts that work in three verbs: design the environment, specify the intent, and build the feedback loops.
Its own summary is four words: "Humans steer. Agents execute."
2. Memory bases
Where does an agent learn what was done yesterday? It starts every session with an empty context window.
To keep context, we use long-term memory. The agent's previous chats are stored as versioned markdown files.
Chat history is compacted, preferences are stored as config files, and every design decision is an indexed document the agent can retrieve.
OpenAI keeps that knowledge in a docs/ directory. A short AGENTS.md, about 100 lines, goes into every session as a map of where each answer lives. A recurring "doc-gardening" agent finds docs that no longer match the code and opens pull requests to fix them.
Anthropic hit the same problem with agents that ran across many context windows.[4] Its first session sets up three things for every later one:
- A
claude-progress.txtfile, the log of what agents have done. - A feature list in JSON. Anthropic found the model less likely to overwrite a JSON file than a Markdown one.
- A first git commit. Every later session commits its work with a description.
Each later session reads the log and the git history, picks one feature, and finishes it. Working on one feature at a time stopped the agent from doing too much at once.
3. Prefix caching through a static prompt
Doesn't the same prompt, sent again and again, cost a fortune? The log, the feature list and AGENTS.md go into every session's prompt.
It doesn't, if the start of the prompt never changes. OpenAI's Codex agent loop is built for it: each new prompt is the old prompt with the new turn added at the end.[5] The old prompt is an exact prefix of the new one, so the model's work on it is reused from the cache.
This is called prefix caching. The rule is the one from our blog on prompt caching: put the static part on top. Instructions, examples and tool definitions go first, and what changes goes last.
OpenAI's post lists what breaks it in Codex. A change to the tools in the middle of a conversation causes a cache miss. So does a change of model, sandbox or working directory.
Its first support for MCP tools had a bug: it listed the tools in a different order each time, and every request missed the cache.
With cache hits, OpenAI writes, sampling the model is "linear rather than quadratic".
4. Tool search
What happens when an agent has fifty tools? A static prompt carries every tool's definition.
Anthropic measured it.[6] The definitions of 50 or more MCP tools took about 72,000 tokens before any work began.
So the agent gets one tool up front: a tool that searches the others. Every other tool is marked defer_loading, and only the three to five it finds for a task are loaded.
With that, the context used fell to about 8,700 tokens, a reduction of 85%. Opus 4.5 got more tool calls right too, 88.1% against 79.5%.
Here is the idea in a few lines of Python, with word overlap standing in for the search:
tools = {
"create_pull_request": "Open a GitHub pull request from a branch",
"list_issues": "List the open GitHub issues of a repository",
"send_message": "Send a Slack message to a channel",
"query_logs": "Search the production logs for an error",
"create_invoice": "Create an invoice for a customer in billing",
}
def search(task, k=2):
words = set(task.lower().split())
score = lambda name: len(words & set(tools[name].lower().split()))
return sorted(tools, key=score, reverse=True)[:k]
print("loaded:", search("open a pull request for my branch on GitHub"))
print("deferred:", len(tools) - 2, "of", len(tools))
loaded: ['create_pull_request', 'list_issues']
deferred: 3 of 5
The tool definitions the agent never needs never enter its context window.
5. Composing Tools
What about the results that come back from every call? Tool search cuts only what the agent reads before it starts.
With plain tool calling, each call is a full pass of the model, and every result lands in the context. Anthropic's programmatic tool calling lets the agent write code that calls many tools, processes their results, and returns only what matters. Twenty tool calls in one block of code save 19 passes of the model.
On Anthropic's complex research tasks, the tokens used fell from 43,588 to 27,297, by 37%. Its post on code execution with MCP shows the same idea at the extreme.[7] An agent that read tool definitions from files only when it needed them used 2,000 tokens where it had used 150,000.
6. Rules enforced through CODE
Composed tools still write code. What stops that code from being wired together the wrong way?
The second technique is architectural constraints. Code follows strict dependency layers: Types to Config to Service to UI.
Linters enforce this mechanically, so the agent cannot violate module boundaries.
Those layers are six in OpenAI's post, and code may only depend forward through them:
- Types
- Config
- Repo
- Service
- Runtime
- UI
Anything shared, such as auth or telemetry, comes in through one interface, called Providers. Custom linters and structural tests check every change, so an agent's wrong import never builds.
7. Cleaning up continuously
Who cleans up the code that piles up while every rule passes?
The third technique is entropy management. AI-generated code accumulates bloat. Stale docs, dead functions, and naming drift.
Background agents run continuously, scanning and cleaning the codebase.
At first, OpenAI's team spent every Friday, 20% of the week, cleaning up what it calls "AI slop". That didn't scale.
So it wrote its taste down as "golden principles", and background Codex tasks open small refactoring pull requests against them. Most can be reviewed in under a minute.
Summary
- A harness is everything around the model, from the prompt and the tools to the hooks.
- An agent's memory lives in the repository: its chats as versioned markdown, a short
AGENTS.mdas a map, and a feature list in JSON. - A static prompt prefix is cached. Changing the tools, the model or their order mid-conversation breaks the cache.
- Tool search loads only the tools a task needs. Anthropic cut the context used by 85%.
- Programmatic tool calling composes tools in code. Anthropic cut tokens by 37% on complex research tasks.
- Custom linters enforce the layers, so a wrong import never builds.
- Background agents clean up the code against written principles.
Sources
- Harness engineering: leveraging Codex in an agent-first world, Ryan Lopopolo, OpenAI, 11 February 2026.
- Improving Deep Agents with harness engineering, Vivek Trivedy, LangChain, 17 February 2026.
- The Anatomy of an Agent Harness, LangChain, 10 March 2026.
- Effective harnesses for long-running agents, Anthropic, 26 November 2025.
- Unrolling the Codex agent loop, Michael Bolin, OpenAI, 23 January 2026.
- Introducing advanced tool use on the Claude Developer Platform, Anthropic, 24 November 2025.
- Code execution with MCP: building more efficient agents, Anthropic, 4 November 2025.