43An agent harness

mediumpython
45:00

43 An agent harness

An Anthropic ML engineer candidate got a tooling scenario in which the model plans and then calls a tool (1 report), and an OpenAI round that was billed as system design turned into agent harness and eval design (1 report). The harness is the plain code around the model: it runs the tools the model asks for, feeds the results back, and decides when to stop. This is practice in that shape, not a leaked question.

Set a 45-minute timer. Read one level at a time, and run only that level's tests until they pass: pytest problems/43_agent_loop -k level1. Earlier levels must keep passing.

Write run_agent(model, tools, task, max_steps=10, schemas=None, needs_approval=None, approve=None). It returns a dict with answer (a string, or None when the run stopped without one), stopped ("answer", "max_steps" or "loop") and transcript, the list of messages in order.

The transcript starts with {"role": "user", "text": task}. Each step calls model(transcript), which returns either {"answer": text} or {"tool": name, "args": {...}}.

Level 1. The loop. On an answer, append {"role": "answer", "text": text} and stop with stopped set to "answer". On a tool call, append {"role": "call", "tool": name, "args": args}, run tools[name](**args), and append {"role": "result", "tool": name, "ok": True, "content": str(output)}. The model gets at most max_steps turns. After that, stop with "max_steps" and no answer.

Level 2. Errors go back to the model. A failed call still appends a result, with ok set to False, so the model can read what went wrong and try again. A tool name that isn't in tools gives "unknown tool: <name>". A tool that raises gives "error: <the exception's message>". When schemas has an entry for the tool, mapping each argument name to its Python type, check the arguments before running the tool. Every listed argument must be present, no other argument is allowed, and each must be an instance of its type. Report the first problem found, in this order: "bad arguments: missing <names>", "bad arguments: unexpected <names>", or "bad arguments: wrong type for <names>", with names joined by ", " in the schema's order or, for unexpected names, the call's order. A call with bad arguments never reaches the tool.

Level 3. A person, and a loop. When needs_approval(name, args) is true, call approve(name, args) before running the tool. If it returns false, don't run the tool, and append the result "denied by a person" with ok false. Calls that don't need approval never ask. When the model makes the same call, same tool and same arguments, three times in a row (no other call between them), append that third call and stop with "loop" and no answer, without running it. The checks on a call go in this order: the repeat, an unknown tool, the arguments, then the person. So a person is never asked about a call that fails an earlier check.

Things to get right: errors are results, not crashes; nothing runs before its checks pass; and every stop has a reason. Follow-ups from the lesson: budgets in tokens and money, a refund that must not run twice, parallel calls, and grading an agent by its outcome.

solution.py
Run the tests to see which pass.