Skip to content

Interview question · Agent Loops & the Harness

Write the agent loop on the whiteboard, then list every way a run can end.

The loop is ten lines; the exits are the interview. A strong answer separates the model's opinion that it is done from the harness's reasons to stop, and says what each exit hands back.

easyEssentialNewUpdated Nov 2026

TL;DR: Write the four moves (assemble the context, call the model, dispatch the tool calls, append the results) inside a bounded loop, then name the exits: the model's final answer, budgets for steps, tokens, money and time, a guard refusing an action, an unrecoverable tool failure, and a person taking over. Every exit returns its reason, and only the first is the model's call.

How to approach it

Write the loop first and fast; interviewers use it to check you have built one, not to watch you invent it. Then spend your time on the exits, because that is where the question is. Before listing them, ask one clarifying question that changes the answer: is this an interactive assistant with a user waiting, or a background job? A user waiting adds wall-clock limits and a handoff; a background job adds cost ceilings and resumption.

A strong answer

The loop, in whatever language the room uses:

async function run(task: string, limits: Limits): Promise<RunResult> {
  const messages: Message[] = [{ role: "user", content: task }];
  const budget = new Budget(limits);         // steps, tokens, $, seconds, per-tool
  try {
    while (true) {
      const reply = await model.respond(messages, toolDefs);
      budget.chargeModel(reply.usage);               // throws BudgetSpent
      messages.push(reply.message);
      if (reply.toolCalls.length === 0) {
        return verify(reply.text)                     // a check, where one exists
          ? { status: "done", answer: reply.text }
          : { status: "unverified", answer: reply.text };
      }
      for (const call of reply.toolCalls) {
        budget.chargeTool(call.name);
        const decision = policy.check(call, session); // validate, authorise, limits
        if (decision.kind === "needs_approval") {
          return { status: "awaiting_human", call };
        }
        const result = decision.kind === "deny"
          ? { error: decision.reason }                // refusals go back as data
          : await runTool(call);                       // retries and timeouts inside
        messages.push(toolResult(call.id, result));
      }
    }
  } catch (e) {
    if (e instanceof BudgetSpent) {
      const partial = await summarise(messages);     // spends the reserve
      return { status: "stopped", reason: e.limit, partial };
    }
    throw e;
  }
}

Then the exits, each with what it returns:

ExitTriggered byReturns
Final answer, no tool callsThe modelThe answer; marked unverified if no check exists
Step, token, cost or time budget spentHarness countersA partial summary from a reserved budget, plus the limit that fired
Per-tool limit or open circuitHarness countersThe same, naming the failing dependency
Guard denies, or approval neededPolicy codeThe refused call and reason, or a pending state and who decides
Unrecoverable failureThe dispatcherThe last good state and the error, for escalation
Cancellation or human takeoverThe user or an operatorThe trace so far, so someone can continue

The sentence that earns the round: "No tool calls" means the model thinks it is finished; it is the only exit the model controls, and even that one gets verified where a check exists. Everything else lives in code the model cannot argue with.

What interviewers probe next

"Your step limit fires. What does the user see?" A summary of what was established and what is missing, produced from a reserved slice of the budget, with the reason the run stopped. Never the model's last half-sentence dressed up as an answer.

"How do you choose the step limit?" From the distribution of successful runs: a hard limit just above p99, an alert just above p95, re-derived whenever the model or tools change.

"The model returns two tool calls in one turn. Does your loop handle that?" Yes, it iterates over them; independent reads can run concurrently, but calls that write, or depend on each other, run in order.

"Where would you add a loop detector?" In the dispatcher, hashing each normalised call (tool name plus canonicalised arguments) and stopping or redirecting after the same call repeats without new information.

What if?

What if · interview follow-ups

Hold your answer as the constraints change

Your support agent loop has one exit: it stops when the model returns no tool calls. It works in the demo.

  1. Start here

    What if a tool it depends on starts timing out?

How it's scored

Weak: Writes an unbounded loop with a single exit on "no tool calls", or caps iterations but returns the model's last message as the answer when the cap fires.

Hire: Writes the loop cleanly, lists harness-owned budgets (steps, tokens or cost, time) and a guard on actions, and returns an exit reason.

Strong Hire: Treats the model's final answer as a claim to verify, adds per-tool limits, approvals and cancellation as exits with their own state, reserves budget for a partial summary, and sets limits from the distribution of successful runs.

Common mistakes

  • Using a step cap as the only protection: a run can spend its money long before it reaches the step limit, so count tokens and cost too.
  • Letting a tool exception crash the run, when most tool errors should go back to the model as a result it can act on.
  • Forgetting that a refused or pending action is not a failure: it needs its own return status so the caller can act on it.
  • Reporting "done" without saying whether anything was checked.
You just read a free answer. Every topic opens its first 5 answers to anyone. Sign in and the count per topic goes to 6, and the site remembers where you stopped in each one.Google sign-in · free · no payment details
HOW DID IT GO?
0
READING SIGNED OUT

Signing in raises your free answers from 5 to 6 per topic, adds an applied lesson in every topic and easy practice tests, and the site starts remembering you: mastery per topic, bookmarks, and a next-focus recommendation. Free, no card.

Sign in free
UP NEXT ON YOUR JOURNEY
DISCUSSION · 0

Nobody has commented. Post a correction or another way to answer.