# Every AI Agent Runs This Loop (Most Engineers Don't Know It)

# AI Agents = Model + Harness

**Watch the full walkthrough:**

%[https://www.youtube.com/watch?v=-xEPoj5AfGU]

---

For a long time, AI discussions revolved around one thing: **the model**.

GPTs. Claudes. Geminis, etc.

But if you’ve built even a single production-grade agent, you’ve probably realized something:

> **The model is only one part of the system.**

Today, a much better definition is emerging:

> **Agent = Model + Harness**

The model provides intelligence.

The **harness** makes that intelligence reliable.

And that’s where most of the engineering happens.

---

## What is Harness Engineering?

Harness Engineering is the software architecture surrounding an LLM that transforms it into a production-ready autonomous system.

![](https://substackcdn.com/image/fetch/$s_!TrTr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6ff48ac-a977-462b-8294-dd62537e1c78_2497x1307.png)

Think about everything that exists **outside** the model:

- Prompts
- Tool calling
- Feedback loops
- Memory
- Context management
- Human approval
- Guardrails
- Multi-agent orchestration

All of these components work together to create an agent that can reason, act, recover, and complete tasks reliably.

The harness is what connects all these pieces.

---

# Layer 1: The Loop

I begin with what I believe is the most fundamental layer:

**The Loop.**

Its responsibilities are surprisingly simple:

1. Call the model.
2. Evaluate the response.
3. If the task isn’t finished, call the model again.

Repeat until the goal is complete.

Without a loop, an LLM performs exactly one inference:

**Input → Output**

That’s useful for chat.

It isn’t enough for autonomous agents.

---

## The Anatomy of One Iteration

![](https://substackcdn.com/image/fetch/$s_!dlZf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8743c3e7-901e-443b-82e9-d1bcfeedf1d1_2475x855.png)

##

Every iteration of an agent loop follows the same fundamental lifecycle.

### 1. Assemble Context

Before the model is called, the harness gathers everything the model needs:

- User request
- Conversation history
- Previous tool results
- System prompt
- Available tools

This becomes the complete context for the next decision.

---

### 2. Invoke the Model

The harness sends the assembled context to the LLM.

At this point, the model has only two real choices:

- Return a final answer.
- Request one or more tool calls.

Nothing else.

---

### 3. Observe the Response

The harness inspects the model’s output.

If the response contains a tool call, it extracts:

- Tool name
- Arguments
- Metadata

If it’s a normal response, it may already be time to terminate the loop.

---

### 4. Execute the Tool

If the model requested a tool, the harness—not the model—executes it.

This could involve:

- Querying a database
- Calling an API
- Running code
- Searching the web
- Reading files

The model never executes tools directly.

It only decides **which tool should run**.

---

### 5. Update the Context

The tool result isn’t immediately returned to the user.

Instead, the harness appends it to the conversation history.

Now the model knows:

> “I asked for this information, and here’s what I received.”

This allows the model to continue reasoning using fresh information.

---

### 6. Decide Whether to Continue

Finally, the harness determines whether another iteration is needed.

If the model has everything it needs, it responds to the user.

Otherwise, the loop repeats.

---

## Every Major Agent Uses the Same Pattern

![](https://substackcdn.com/image/fetch/$s_!C1np!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf35c887-4cd4-432c-a8f8-6b7f7c34a77e_2527x940.png)

Whether it’s:

- Claude Code
- Codex CLI
- Hermes
- OpenClaw
- OpenAI Agents

...they all implement essentially the same loop.

The details differ.

The architecture doesn’t.

At a high level, every loop looks like this:

1. Assemble context.
2. Call the model.
3. Check whether it wants to use a tool.
4. Execute the tool if needed.
5. Add the result back into context.
6. Repeat until a stopping condition is reached.

Once you understand this pattern, reading agent frameworks becomes dramatically easier.

---

## Production Loops Need Guardrails

![](https://substackcdn.com/image/fetch/$s_!-orq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa32c8ddc-b64a-4be5-ad8d-2f81ca49bb56_2510x1201.png)

A production loop can’t simply run forever.

Reliable systems need explicit termination conditions.

Some of the most important ones include:

- Maximum iteration limits
- Wall-clock timeouts
- Goal completion checks
- Repeated tool-call detection
- Handling unrecoverable errors
- Token budget limits

These aren’t optional.

They’re what prevent runaway loops, excessive API costs, and unreliable agent behavior.

---

## The Loop Leaves Behind State

When a loop finishes, it doesn’t just return an answer.

It also leaves behind valuable execution state:

- Tool history
- Context
- Intermediate reasoning
- Progress
- Execution checkpoints

That state becomes the foundation for durable execution, resumable agents, and human-in-the-loop workflows.

In other words:

A good loop doesn’t just solve problems.

It creates the foundation for every layer that comes after it.

---

# The Bigger Picture

This article is the first in a series on **Harness Engineering**.

We’ll progressively build the software architecture that surrounds modern LLMs:

- Layer 1 — Loops ✅
- Layer 2 — Tools & Permissions
- Layer 3 — Context Engineering
- Layer 4 — Durable Execution
- Layer 5 — Multi-Agent Orchestration

By the end of the series, you’ll understand not just how to use agent frameworks—but why they’re built the way they are.

If you’re serious about building production-grade AI agents, understanding the harness may be even more valuable than understanding the model itself.
