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:
| Exit | Triggered by | Returns |
|---|---|---|
| Final answer, no tool calls | The model | The answer; marked unverified if no check exists |
| Step, token, cost or time budget spent | Harness counters | A partial summary from a reserved budget, plus the limit that fired |
| Per-tool limit or open circuit | Harness counters | The same, naming the failing dependency |
| Guard denies, or approval needed | Policy code | The refused call and reason, or a pending state and who decides |
| Unrecoverable failure | The dispatcher | The last good state and the error, for escalation |
| Cancellation or human takeover | The user or an operator | The 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.
Start here
What if a tool it depends on starts timing out?
Who decides the run is going nowhere?
The model sees each timeout, reasonably retries, and the loop never ends because the model never produces a final answer. The harness needs its own exits: a per-tool failure limit that opens a circuit after a few consecutive failures, plus overall step and time budgets.
Carry it forwardAny exit the model controls is optional from the harness's point of view.
Weak answerAdding 'stop after three failures' to the prompt.
Constraint changes
What if the user closes the chat mid-run?
Is anyone still waiting for this answer?
Cancellation is an exit too. Propagate it to the loop and to in-flight tool calls, stop spending, and record where the run stopped. A background job keeps running only if the product says it should.
Carry it forwardEvery caller deadline becomes a harness exit.
Weak answerLetting orphaned runs finish and bill because nothing told them to stop.
Constraint changes
What if the model says 'Done, the ticket is resolved' but the ticket system still shows it open?
Can the harness check the claim?
Then completion is a check, not a statement. Query the ticket's state after the final answer; if it is still open, send the model back with what the check found, or return the run as unverified.
Carry it forwardWhere completion can be verified, verify it.
Weak answerTrusting the final message because it sounds confident.
Constraint changes
What if the next action is a refund above the automatic limit?
The run is not finished and not failed.
The loop exits into a pending state: it records the proposed call, who must approve it, and enough context for them to decide, and it resumes only when the decision arrives, re-checking that the order is still eligible.
Carry it forwardWaiting for a person is a first-class exit with its own state.
Weak answerBlocking a thread for hours or asking the model to wait.
Now defend it
How do you summarise the design in one line for the interviewer?
The model may end a run by giving a final answer, and even that gets verified where possible; every other exit (budgets, guards, approvals, failures, cancellation) belongs to the harness, and every exit returns its reason with whatever partial result is honest.
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.