What Is an Agent Harness?

Share on:

Why the same model gives you a different answer in the app and the API: what an agent harness is, its 7 parts, the agent loop, and a 5-minute check for any AI tool.

Part 1 of the Agent harness series · Next: build the agent loop in Python →

You ask Claude something in the app and the answer is great. Then you send the same question to the Claude API from your code and the answer looks totally different. Same thing with ChatGPT and the OpenAI API.

Try the smallest version: ask “What’s today’s date?” The app knows it and the plain API call doesn’t.

That’s not a bug. Anthropic publishes the system prompts of its apps, and they say the app gives Claude up-to-date information like the current date at the start of every conversation. The API doesn’t get those prompts.

So the model is the same, what changed is everything around it.

An agent harness is all the code around a model that turns it into an agent. It runs the loop, decides what the model sees, runs the tools, checks what’s allowed and stops the loop when a limit is hit.

Updated 22 November 2026

In this post I explain what an agent harness is made of, how its loop works turn by turn and how you can check the harness of any AI tool you use. Near the end there is a look under the hood at the actual requests, for anyone who wants it. In part 2 we build one.

What changes between the API and the app?

The harness is what changes.

With the API you send a message and you get text back, that’s it. With an app or a coding agent the model is wrapped in code, and this code does work before your message reaches the model and after the model replies.

Let me take a bigger example than the date. Ask both to fix the failing test in your project. The API can’t look at your project, it just writes what might be wrong. Claude Code has the same model inside but it goes and opens your files, runs the test, edits the code and then runs the test again to see if it passes.

Same model with and without an agent harness. Left, a plain API call: message, model, text back, with five red crosses: isn't told today's date, can't open your files, can't run the test, forgets everything after the reply, one answer with no checking. Right, the same model inside an agent harness that adds the date, rules, earlier chat, tools and limits, then loops: the model asks for a tool, the harness checks and runs it, the result goes back.
Same question and same model, the difference on the right is all harness.

Model, agent, agent harness: what’s the difference?

People mix these three words up a lot, so let me separate them.

The model is text in and text out. It can’t open a file by itself, it doesn’t know the date and it doesn’t remember yesterday.

An agent is a model that uses tools in a loop until the goal is done. Simon Willison has a one-line definition for it: an LLM agent “runs tools in a loop to achieve a goal.” You might know the same loop by its older name ReAct, reason then act, which I wrote about in my LangChain ReAct post.

The agent harness is the code around the model that makes that loop possible and keeps it safe.

A car is a good way to picture it. The model is the engine, strong but it can’t drive anywhere alone. The harness is the rest of the car. Tools are the steering, logs are the dashboard, limits are the brakes and permissions are the seatbelt.

If you’d rather watch, Barry Zhang from Anthropic explains this in 15 minutes. His agent is exactly this, a model with its tools and a loop.

“How We Build Effective Agents”, Barry Zhang, Anthropic (AI Engineer, April 2025, 15 min).

Which harnesses do you already use?

Probably more than you think, because every AI tool you type into is a model inside a harness.

The Claude app and ChatGPT put the model inside a system prompt with memory and tools like web search. Claude Code and Codex are harnesses made for code. mini-swe-agent is one you can read in an evening, its agent class is about 100 lines of Python and bash is its only tool.

They look very different from outside. Inside they are built from the same parts.

What is an agent harness made of?

Seven parts. Every agent harness has some version of each one, even when you never see them.

  1. Tools, what the agent can do, like read a file, run a test or search the web.
  2. Context, what the model sees on each turn, so the rules, the task and the history.
  3. Memory, what’s still there after the session ends.
  4. Permissions, what the agent can touch and what it can never touch.
  5. Approvals, when a person has to say yes first.
  6. Limits, on turns, time and money.
  7. Logs and undo, what it did and how to take it back.

The loop that runs all of this is the core, and I come to it in the next section.

Claude Code is a good harness to look inside, because its docs explain every part.

The 7 parts of an agent harness in a table: what you need from an agent, the harness part that gives it, and how Claude Code does it according to its docs. Tools: Read, Edit, Bash, Grep, WebSearch and more. Context: CLAUDE.md, files read, command output. Memory: CLAUDE.md and auto memory, loaded each session. Permissions: rules checked in the order deny, then ask, then allow. Approvals: asks before edits and most shell commands. Limits: max turns, max budget, a 2-minute Bash timeout. Logs and undo: checkpoints you can rewind, and a full transcript.
Every row is code in the harness, none of it depends on the model choosing to behave.

Four of these are worth a closer look.

Permissions are checked in a fixed order. Claude Code checks deny rules first, then ask and then allow, and the first rule that matches wins. In the default mode reading files in your project needs no approval, but editing them and most shell commands do.

Memory is a file you can open. Claude Code reads CLAUDE.md at the start of every session, so your project rules don’t depend on the model remembering them.

Limits are settings you set. For unattended runs --max-turns and --max-budget-usd cap the loop. The docs also say the budget is an estimate, so the spend can go a bit over it.

Undo is built in. Claude Code saves a checkpoint before each prompt and /rewind brings the files back. Changes made only through shell commands aren’t tracked though, so it doesn’t replace git.

Other harnesses have the same kind of gaps. Each guardrail covers some cases and not others, and good docs tell you which.

Is the agent loop the same thing as the harness?

No. The loop is the core of the harness, but not the whole harness.

OpenAI is clear about it when it describes Codex. It calls “the Codex harness” the part “which provides the core agent loop and execution logic” (Unrolling the Codex agent loop).

So the loop lives inside the harness. Around the loop, the same harness decides what goes into each request, which tools exist, what needs your approval, when to stop and what goes in the log.

Anthropic uses the word the same way. Its post on long-running agents calls the Claude Agent SDK “a powerful, general-purpose agent harness”.

How does the agent loop work, turn by turn?

The model never runs anything, it only replies. The harness does the rest, and it keeps going until the model stops asking for tools. One turn looks like this:

  1. The harness builds the request with the rules, the tool list and the whole history so far.
  2. The model replies. Either it asks for one or more tools, or it gives the final answer with no tool call.
  3. If it asked for tools, the harness checks each call is allowed, runs it and adds the result to the history.
  4. The harness checks its limits. Still under the max turns and the budget? Then back to step 1.

Take the failing test job from before, it could go like this. In turn 1 the model asks for run_tests, the harness runs it and 1 test fails.

In turn 2 it asks to read two files in the same reply, the failing test and the code it covers, and the harness reads both. In turn 3 it asks to edit the code, and in Claude Code that’s when you get asked to approve.

In turn 4 it asks for run_tests again and now everything passes. In turn 5 it replies without a tool call and tells you what it changed and why, and the loop ends.

So the model is the one that decides the job is done, by replying without a tool call. The Claude Agent SDK documents exactly this, turns continue until Claude replies with no tool calls and then the loop ends (How the agent loop works).

The harness is the safety net. If the model never stops, the harness stops it. The same SDK page lists max_turns and max_budget_usd, and a run that hits them ends with error_max_turns or error_max_budget_usd.

The agent loop inside an agent harness, in four boxes. 1 Build the request (harness): rules, tool list and the whole history. 2 Reply (model): either asks for tools, or answers with no tool call, which ends the loop. 3 Run the tools (harness): checks each call is allowed, runs it, adds the result. 4 Check the limits (harness): under max turns and budget, go round again. In the middle, a counter reads Turn 3 of 10.
Pink is the model and blue is the harness, all the model does is reply.

A few more details matter once you build one yourself.

One reply can ask for several tools, the Claude API calls it parallel tool use. The Agent SDK can run read-only tools at the same time, while Edit, Write and Bash run one after the other.

When a tool fails, that goes back to the model too. The harness sends the result with is_error set to true, and the model may try again or tell you what went wrong. That’s why the Claude docs say to limit how many retries your loop allows.

Not every tool runs in your harness. Claude’s web search and code execution are server tools, they run on Anthropic’s side and you only see the call and the result. Bash, file edits and your own tools run in your application.

And the loop itself is small. Thorsten Ball builds a working code editing agent in about 300 lines of Go with three tools. In part 2 we build one in Python.

Does the agent harness change how good the model is?

Yes, it changes how well the model works, not just which features you get.

In November 2026 a Hugging Face team wrote that the same model behaves differently in different agent harnesses, because each harness has its own tools, prompt and control flow. Their answer was to train a small add-on for every pair of model and harness. Part 13 goes into that.

What it means for you today is simple. When you pick a model, test it inside your own harness, because a leaderboard score was measured inside somebody else’s.

When don’t you need an agent at all?

When you can write the steps down in advance.

If the job is always “extract the fields, check them, save them”, a fixed workflow is cheaper, faster and easier to test than an agent. Anthropic makes the same point in Building effective agents, start with the simplest thing that works.

An agent is worth its cost when the steps depend on what it finds on the way. Fixing a bug is like that, filling in a form isn’t.

Under the hood: what does the agent harness send on each turn?

This part is for those who want to see the real requests. If you only wanted the idea, you can skip to the next section.

A plain API call has only one message and nothing else:

{
  "model": "<model>",
  "max_tokens": 1024,
  "messages": [
    { "role": "user", "content": "Fix the failing test in my project." }
  ]
}

Here is the shape of what a harness sends a few turns into the loop. I simplified it, but the field names are the real ones from the Claude tool use docs:

{
  "model": "<model>",
  "max_tokens": 1024,
  "system": "Today's date is ... You are working in the user's project. Run tests before you say a fix works.",
  "tools": [
    { "name": "read_file", "description": "Read a file in the project", "input_schema": { "...": "..." } },
    { "name": "run_tests", "description": "Run the test suite", "input_schema": { "...": "..." } }
  ],
  "messages": [
    { "role": "user", "content": "Fix the failing test in my project." },
    { "role": "assistant", "content": [
      { "type": "tool_use", "id": "toolu_01", "name": "run_tests", "input": {} }
    ] },
    { "role": "user", "content": [
      { "type": "tool_result", "tool_use_id": "toolu_01", "content": "1 failed: test_total ..." }
    ] }
  ]
}

There are three things the plain call doesn’t have: a system prompt with the date and the rules, a tool list with what the model can ask for, and the history with everything that happened so far.

Now follow the id. When the model wants a tool, its reply stops with stop_reason: "tool_use" and has a tool_use block in it. The harness runs the tool and sends the output back as a tool_result with the same tool_use_id, so the model knows which call the result is for.

The model reads all of this on every turn, and that’s why the same model answers so differently.

Why does the context keep growing?

Because the model has no memory between turns. On every turn the harness sends the whole history again, plus the newest tool result.

Guess before you read on, in turn 5 of the failing test job how many messages does the harness send?

It’s nine. Your question, four replies that asked for tools and four tool results. By turn 20 it’s 39, and a test log or a big file read can add thousands of tokens in one step.

Good harnesses handle this in two ways and Codex describes both. One is caching. Codex keeps every new prompt as an exact extension of the last one, in their words “the old prompt is an exact prefix of the new prompt”, so the provider can reuse the work it already did on the part that didn’t change. That’s called prompt caching. The other is compaction. When the history gets too long Codex replaces it “with a new, smaller list of items”, basically a summary of what happened so far.

You don’t see any of this when you use a tool, but it’s a big part of why one agent feels fast and cheap while another slows down after twenty minutes. Part 6 is all about it.

Common mistakes when you build an agent harness

The most common one is putting rules only in the prompt. “Never delete production data” in a prompt is a request the model can ignore, so put the rule in a permission check.

Then trusting the model to stop. Most of the time it does, but set max_turns and a budget for the times it doesn’t.

Tool calls that aren’t safe to repeat are another one. Harnesses retry, and if “charge the card” gets retried the card gets charged twice. Give your write tools an idempotency key.

Adding every tool just in case is a mistake too. Each extra tool is one more way to go wrong and one more description in every request, so give the agent only the few it needs.

Treating tool results as trusted is a big one. A web page, an email or a file can have instructions in it aimed at the model. Claude’s docs say to treat that content as untrusted and keep it inside tool_result blocks, and part 10 covers the defenses.

And the last one is having no cap on spend across agents. One run has a budget, but a team of agents needs one as well, in front of the model, for example an LLM gateway.

Try it: check any agent’s harness in 5 minutes

No code needed. Take an AI tool you use every week and answer these seven questions:

  1. What tools can it use?
  2. What does it know before you type, like the date, files or rules?
  3. What does it remember tomorrow?
  4. What can it never touch?
  5. When does it ask you first?
  6. What stops it if it goes wrong?
  7. Where can you see what it did, and can you undo it?

If you can’t answer 4, 5 or 6, find out before you give it anything important.

What does this agent harness series cover?

This post is the map. Each part takes one piece and goes deep, with code where it helps and the best videos and docs from others placed where they fit. They’re in the order I’d learn them:

  • Part 2, build the agent loop in Python, 33 lines with a local model, three tools, a turn limit and every step printed.
  • Part 3, design tools an agent uses well, the names, descriptions, inputs and errors a model can actually work with.
  • Part 4, MCP in simple steps, how one protocol lets any agent use your tools.
  • Part 5, which agent SDK, Claude Agent SDK, OpenAI Agents SDK, LangGraph and smolagents on the same job.
  • Part 6, context engineering, what to keep, what to drop, caching and compaction.
  • Part 7, guardrails, permissions, approvals and sandboxing in code.
  • Part 8, budgets, retries and safe tool calls, limits that hold when things fail.
  • Part 9, evals and tracing, how you know your agent works and why it fails when it does.
  • Part 10, prompt injection, why private data, untrusted content and a way out should never meet.
  • Part 11, long-running and multi-agent, when to split the work and when not to.
  • Part 12, local models in depth, what breaks when you swap in small open models and how the harness can catch it.
  • Part 13, models trained for a harness, fine-tuning, harness adapters and what they change.

Every part goes into the Agent Harness category.

The whole post on one page

Save it or share it.

The agent harness check: seven questions to ask about any AI agent, one per harness part, from tools to logs and undo, with the agent loop in four steps at the top.

What’s next

In part 2 we build the loop ourselves in 33 lines of Python, with a local model, three tools, a turn limit and every step printed so you can watch it work.

Over to you, which AI tool surprised you most when you checked what it’s allowed to do?

Part 1 of the Agent harness series. Get each part by email: the newsletter.

Transactional Outbox Pattern in Python: Never Lose an Event

Your order saved, but the event never reached Kafka. Build the transactional outbox pattern in Python and Postgres: a SKIP LOCKED relay that’s safe on several workers, fixes for ordering and cleanup, and lessons from seven teams that hit the dual write.

Anup-Das-Anuptechtips

I write about System Design, Backend Architecture, GenAI Infrastructure, and scalable Python applications.