How does an agent loop work, and how do you stop an agent from running forever?
Suggested answer
An agent loop is the code wrapped around a model that lets it work in several steps instead of answering once. Each round does four things:
- Send the whole
messageshistory to the model, along with the list of tools. - Append the model's reply, word for word, to the history.
- If the model did not ask for a tool (with Claude,
stop_reasonis nottool_use), that is the final answer and the loop ends. - If it did, run every tool it asked for, collect the results into one message, add it to the history and go around again.
The important idea is that the model decides the number of steps, not the code. For example, a customer writes "find my order by phone number, and if it hasn't shipped yet, add one more iced milk coffee": the model calls, in turn, find order, check status and add item, and only then answers. The code just repeats.
So the second half of the question is the part the interviewer really wants to hear. Mention:
- A limit on model calls (say 6). When the limit is hit, stop, return a safe reply and write a log entry for someone to look at, instead of throwing an error or looping on.
- Rules live inside the tools, not only in the prompt. The add-item function itself refuses an order that is already being delivered, even if the model calls things in the wrong order.
- Tool errors go back to the model (
is_error) so it can change course, but you need to guard against the model retrying the same call forever. - Cost grows with the number of rounds: every round resends the whole history, so a 4-step question costs more than 4 times a 1-step question in tokens.
Bonus points: add that a time limit or a token budget should sit next to the round limit, and that SDKs often ship a tool runner that runs this loop for you.