In short
- The harness decides most of an agent’s quality: what the model sees, what it can touch, and when it stops.
- Start with one loop, a few sharp tools, a trace file and a recorded stop reason. Add the rest when a measured failure asks for it.
- Evaluate on tasks with a check in code. Run each one k times and report pass^k and cost per successful task next to the pass rate.
What a harness is
An agent is a loop: build a context, ask the model for the next step, run the tools it asks for, append the results, repeat until something says stop. The model is one call in that loop. Everything else is the harness: the system prompt, the tools and the shape of their output, what gets dropped when the context fills up, what needs permission, and when the run ends.
Swap the model under a good harness and you get a slightly better or worse agent. Swap the harness and you can get a different product.
Build it in this order
1. The loop. It should fit on one screen. Trace every turn to JSONL from day one, and record why the run stopped: “done” and “out of turns” can end with the same message.
def run_agent(
task: str,
model: Model,
tools: Toolbox,
trace: Path,
max_turns: int = 30,
) -> Run:
"""Run one task until the model stops calling tools or hits the turn limit."""
run = Run(messages=[{"role": "user", "content": task}])
with trace.open("a") as log:
for turn in range(1, max_turns + 1):
reply = model.complete(run.messages, tools=tools.specs())
run.messages.append(reply.message)
entry = {"turn": turn, "message": reply.message, "usage": reply.usage}
log.write(json.dumps(entry) + "\n")
if not reply.tool_calls:
return run.finish("done", turns=turn)
for call in reply.tool_calls:
run.add_tool_result(call.id, tools.execute(call))
return run.finish("max_turns", turns=max_turns)2. Tools. The model knows them only by name, description, schema and output, so design them like an API for a reader who cannot ask questions.
- Few tools, one job each. Tools that overlap make the model guess.
- Errors go back as text and say what to try next. The loop never crashes on a tool failure.
- Truncate long output and say so. A silent cut looks like the whole answer.
def execute(self, call: ToolCall) -> str:
"""Run a tool call and return text for the model. Never raises."""
tool = self.tools.get(call.name)
if tool is None:
return f"error: no tool {call.name!r}. Available: {', '.join(self.tools)}"
try:
output = tool.run(**call.args)
except Exception as e: # noqa: BLE001 - the model has to see every failure
return f"error: {call.name} failed: {e}. Expected: {tool.schema}"
if len(output) <= LIMIT:
return output
note = f"[truncated: showed {LIMIT:,} of {len(output):,} characters]"
return f"{output[:LIMIT]}\n{note}"3. Context. Large results go to a file; the model gets the path and a summary. When you compact, keep the task and the last few turns verbatim and summarise the middle. Log tokens per turn.
4. Safety and stopping. Run in a container or a throwaway worktree, without credentials it does not need. Writes outside the workspace go through a permission check. Stop when the model says it is done, at a turn limit, at a token or time budget, or when the same tool call repeats three times.
Evaluate tasks, not prompts
Reading a few transcripts and tweaking the prompt until they look better is not evaluation. You need tasks and a number. A task is a starting state, an instruction and a check written in code: the tests pass, the query returns these rows, nothing else changed.
@dataclass(frozen=True)
class Task:
id: str
instruction: str
setup: Callable[[Path], None]
check: Callable[[Path], bool]- Take tasks from real failures. Forty from real use beat a thousand generated ones.
- Treat the set as a dataset: versioned, with the source of each task, and a held-out slice you never tune on.
- Check the final state, not the transcript. Where code cannot check the outcome, use a model as judge with a rubric, and trust it only after it agrees with your own labels on thirty or so examples.
Measure reliability, cost and noise
Agents are not deterministic. Run every task k times (five is a good start) in a fresh workspace and report:
- pass@1: the average success over all runs.
- pass^k: the share of tasks that passed every run. This is what users and unattended jobs feel. At 80% per run, pass^5 is about 33%.
- cost per successful task: the agent’s unit economics, and the number to compare with what the work costs today.
def score(results: dict[str, list[bool]]) -> tuple[float, float]:
"""Return (pass@1, pass^k) from each task's run outcomes."""
rates = [sum(runs) / len(runs) for runs in results.values()]
pass_at_1 = sum(rates) / len(rates)
pass_all = sum(all(runs) for runs in results.values()) / len(results)
return pass_at_1, pass_allWeight tasks by how often they happen and what a failure costs; a rare task that corrupts data matters more than a common one that wastes a minute. And mind the noise: with 40 tasks near 60%, the standard error is about 8 points (√(0.6 × 0.4 / 40)), so a 5-point gain from one run means nothing. Compare the same tasks with the same k, task by task.
Read the failures
The number says whether you improved; the traces say why you did not. Put every failed run in a bucket (confusing tool output, lost instruction, stopped early, looped, flaky environment, wrong task), fix the biggest bucket and rerun. Most fixes land in the harness, not the prompt: a clearer error, a truncation notice, a tool split in two. Fix flaky tasks too; they poison every comparison.
Checklist
- One loop, few tools, every turn traced, the stop reason recorded.
- Errors that say what to try next; truncation that says it truncated.
- Large results in files, and a context budget.
- A sandbox, a permission check on writes, more than one way to stop.
- 30 to 50 tasks from real failures, checks in code, a held-out slice.
- k runs per task; pass@1, pass^k and cost per successful task.
- Read the failures after every run and fix the biggest bucket first.