Your tool loop runs as long as the model asks for tools. That means the model decides when your loop ends. In this lesson a customer writes "Has order 5 shipped yet? Keep checking until it has.", and the model does exactly that: it asks for lookup_order again and again. Every round is a call you pay for.
A loop that something else controls needs a step limit: a counter, and a check that leaves the loop with break when the counter reaches the limit.
MAX_TRIES = 3
tries = 0
while not door_is_open():
if tries == MAX_TRIES:
break
tries = tries + 1
knock()
print("Tries:", tries)
The check comes first in the loop's body, so a fourth knock never happens. After the loop the program doesn't know which way it ended, so it looks again: is the door open, or did it give up?
Your limit is 5 rounds. A round is one reply in which the model asks for tools, together with the results you send back. After five rounds the program stops asking and hands the customer over to a person. Your program must print:
Model: Let me check order 5.
Tool: lookup_order {'order_id': 5} -> {'id': 5, 'ordered': '2026-05-07', 'status': 'paid'}
Tool results sent in one message: 1
Model: Order 5 hasn't shipped yet. I'll check again.
Tool: lookup_order {'order_id': 5} -> {'id': 5, 'ordered': '2026-05-07', 'status': 'paid'}
Tool results sent in one message: 1
… (three more rounds like the second one) …
Rounds: 5
Answer: Let me pass this to a person.
The lesson's check then prints that the model was called 6 times.
Your task: create the constant MAX_ROUNDS with the value 5 and a counter rounds, count every round of the loop, and leave the loop with break when rounds equals MAX_ROUNDS. After the loop, print "Rounds:" and the counter. Then print the model's answer if the model finished by itself, and "Let me pass this to a person." otherwise.
Assignment
Follow the Coding Tutorial to give the tool loop a step limit of 5 rounds and a clear handover when the limit is reached.
Hint
The check with break is the first thing in the loop's body, above the line that adds 1 to rounds. After the loop, reply.stop_reason tells you how it ended: if it is still "tool_use", the limit stopped the loop and there is no answer from the model to print.
Free plan: 30 tutor messages. PRO is unlimited.
