Education › Evals & Harness › Stage 3: Harness fundamentals

Anatomy of an agent harness

Model, loop, tools, context, state and environment — and why the harness often matters as much as the model.

Intermediate to advanced ~40 min read Module 9 of 20

A language model on its own only turns text into text. Everything that makes it an agent, such as reading files, running commands, remembering what it did, asking permission and stopping at the right time, is built around it by the harness. Two products using the same model can behave completely differently because their harnesses differ. Harness engineering is the discipline of designing that surrounding system. This lesson takes a harness apart into its components, follows one request through all of them, and explains which design decisions have the biggest effect on quality, safety and cost.

After this module you can
  • Define an agent harness and list its main components
  • Trace one user request through the loop, tools and context
  • Explain why the same model behaves differently in different harnesses
  • Identify the design decisions that matter most in a harness
  • Read an open-source harness's code and map it to the components

Agent = model + harness

The harness is the software that runs a model as an agent. It builds the prompt, offers tools, executes the tool calls the model requests, feeds results back, manages what fits in the context window, enforces permissions, isolates side effects, records what happened and decides when to stop. Coding agents in a terminal or IDE, browser-using agents and customer-support agents all contain the same components in different proportions.

ComponentResponsibility
System prompt and instructionsRole, rules, project conventions, output style
Tool layerTool definitions, argument validation, execution, result formatting
Agent loopCall model, run tools, repeat; stop conditions and budgets
Context managerWhat goes into each request; truncation, summaries, retrieval
PermissionsWhich actions run automatically, need approval or are denied
SandboxIsolates commands and files from the host and network
State and memorySession history, task lists, persistent notes
Extension pointsHooks, plugins, MCP servers, skills
TelemetryTraces of every step, tokens, cost, errors
InterfaceTerminal, IDE, web or API; streaming and interruption
taskrequesttool callsassembledcallallowedcommandsresultsModeltext in, calls outMCP serversextra toolsUserAgent loopbudgets, stopPermissionsallow, ask, denyTool layervalidate, runContext + statehistory, notesSandboxisolated commands
The harness wraps the model: the loop assembles context, sends it to the model, checks each requested tool call against policy, and runs allowed tools in a sandbox.

Following one request

  1. The user types "fix the failing date test". The interface sends it to the harness with the session state.
  2. The context manager assembles a request: system prompt, project instructions, tool definitions, recent history and the new message.
  3. The model replies with reasoning and a tool call: run the test suite.
  4. The permission layer checks the call. Running tests is allowed automatically; the command runs in the sandbox.
  5. The tool layer captures output, truncates it to fit, and appends it to the history as a tool result.
  6. The loop calls the model again. It reads the failing file, proposes an edit, and the permission layer asks the user to approve the write.
  7. After tests pass, the model replies with a summary and no tool call, so the loop stops. Telemetry records every step.

Every numbered step is a design decision. How much test output to keep, whether edits need approval, what the project instructions say, and when to stop all change the outcome more than many model upgrades do.

The minimal loop

The core of every harness, stripped down
python
def run_agent(task: str, tools: dict, max_steps: int = 30) -> str:
    messages = [{"role": "system", "content": SYSTEM_PROMPT},
                {"role": "user", "content": task}]
    for step in range(max_steps):
        reply = call_llm(messages, tools=[t.schema for t in tools.values()])
        messages.append(reply.as_message())
        if not reply.tool_calls:                 # no tool requested: final answer
            return reply.text
        for call in reply.tool_calls:
            if not permitted(call):
                result = "denied by policy"
            else:
                result = tools[call.name].run(**call.args)
            messages.append({"role": "tool", "id": call.id, "content": fit(result)})
        log_step(step, reply, messages)
    return "stopped: step budget reached"

Everything else in a production harness elaborates on this loop: permitted grows into a policy engine, fit into a context manager, tools into a registry with schemas and sandboxes, and log_step into tracing. Later lessons in this track build out each of those pieces.

Decisions that matter most

  • Tool surface: few, well-described, composable tools beat many overlapping ones.
  • Tool result design: concise, structured and actionable results (including helpful error messages) drive the next step.
  • Context policy: what is always present, what is fetched on demand, and what gets summarised or dropped.
  • Autonomy level: which actions need approval; too strict and the agent is useless, too loose and it is dangerous.
  • Isolation: whether commands run on the host, in a container, or in a remote sandbox.
  • Stop conditions and budgets: step, token, time and cost limits, and what happens when they are hit.
  • Observability: whether you can replay exactly what the agent saw and did.
Tip

To learn harness design fast, read the source of an open-source coding agent and find each component in this lesson's table. Note where the system prompt lives, how a tool is defined, how output is truncated and where approvals happen.

Why the harness is an engineering discipline

Harness changes are product changes, and they interact. Adding a tool changes which tools the model chooses; a longer system prompt pushes history out of the context window; a stricter permission rule changes how often users approve without reading. That is why harness engineering pairs naturally with the evals in the first half of this track: every harness change should be measured on a benchmark of realistic tasks, with success, cost and safety reported together.

Hands-on practice

Map a real harness

  1. Pick an open-source agent harness (any coding agent or agent framework on GitHub).
  2. Find where the system prompt is built and list what goes into it.
  3. Find one tool definition and its execution code; note how errors and long outputs are handled.
  4. Find the main loop and its stop conditions and budgets.
  5. Find the permission or approval logic and any sandboxing.
  6. Draw the components as a diagram matching this lesson's table, and list three design choices you would change and why.
Cheat sheet

Anatomy of an agent harness — at a glance

Main things to focus on

  • Agent = model + harness; the harness often matters as much as the model.
  • Components: prompt, tools, loop, context, permissions, sandbox, state, extensions, telemetry, interface.
  • The loop: call model, run requested tools, append results, repeat until no tool call or budget hit.
  • Tool surface and result design drive behaviour.
  • Autonomy and isolation decide the blast radius.
  • Measure every harness change with evals.

Components

system prompt + project instructionsBehaviour and rules
tool registry (schema, run, format)Actions
agent loop + budgetsControl flow
context managerWhat the model sees
permissions + sandboxSafety
tracingReplay and debug

Loop

reply.tool_calls empty -> stopFinal answer
append tool result with call idFeed back
max_steps / tokens / costBudgets
fit(result)Truncate to budget

Design levers

fewer, clearer toolsBetter selection
actionable errorsSelf-correction
approval for writesHuman control
container per sessionIsolation
on-demand contextSmaller prompts

Common pitfalls

  • Blaming the model for what is a harness problem.
  • Dumping full tool output into context.
  • No step or cost budget, so loops run forever.
  • Running agent commands directly on the host.
  • Changing harness behaviour without an eval.
Quiz

Check your understanding

5 questions · 4 to pass · answers are explained as you go. Your best score is saved on this device only.

Progress and quiz scores are saved in this browser only. Back up or restore on the hub.

Was this lesson useful? Tell me what to improve →