Sub topic 11
Knowing when to stop
This chapter assumes you have read the earlier chapters on the agent loop and on grading results. You should be comfortable with tool calls, message lists, and a loop that runs the model repeatedly.
Why an agent needs a stopping rule
A classic RAG pipeline always ends after one retrieval. An agent does not. It keeps looping while the model asks for tools. If nothing limits the loop, a model that never finds good evidence can search forever. That costs money and time, and the user waits the whole while.
Three ways a loop ends
- Final answer. The model replies with no tool calls. This is the normal exit.
- Step budget. The loop has run a fixed number of rounds. This is a hard cap you set in code.
- Stall detection. The model repeats a search it already ran, so more rounds will not help.
The model controls only the first. Your code must enforce the other two.
The step budget
The loop from the earlier chapter uses for _ in range(MAX_STEPS). When the range runs out, the code does not simply return an error. It adds a user message and calls the model once more with an empty tool list:
messages.append({
"role": "user",
"content": "Step limit reached. Answer now with what you have found and say what is still missing.",
})
return call_model(messages, [])[“content”]Passing no tools matters. The model cannot ask for another search, so it must write text. Asking it to say what is missing gives the user an honest partial answer instead of a guess.
Choosing the number
Pick the budget from your questions, not from habit. A rough way:
- Run your evaluation questions with a high cap, such as 10.
- Record how many rounds each question used.
- Set the cap just above the 90th percentile.
Most lookups finish in one or two rounds. Comparisons across sources need more. A cap of 4 to 6 suits many documentation assistants.
Detecting a stall
A budget stops a loop late. A repeated query shows the loop is stuck early. Track the queries already run and refuse duplicates:
seen_queries = set()
def guarded_search(query, top_k=4):
key = query.strip().lower()
if key in seen_queries:
return {"error": "You already ran this query. Try different wording or answer with what you have."}
seen_queries.add(key)
return search_docs(query, top_k)Return the message as a tool result. The model reads it on the next round and can change course. Create seen_queries fresh for each question, or one user's searches will block another's.
Other limits to add
- A wall-clock timeout, because a slow tool can use up time even when the step count is low.
- A cap on total tokens in the message list. Later chapters cover context control.
- A limit on tool calls per round, in case the model requests many at once.
Exercise
Your agent has MAX_STEPS = 3. On step 3 the model still asks for a search. What should your code do next, and why call the model with an empty tool list?
Show answer
Stop running tools, add a message saying the limit was reached, and call the model once more with no tools. With none available, the model must write a final answer, and the message asks it to state what evidence is still missing.