A step-limited search loop with a scripted model
The model is a script so the output is exact. Real models decide differently, which is why the limit exists.
Instead of searching once, let the model decide when to search and what to search for, look at the results, and search again. A step limit and a repeat check keep the loop from running away.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write parse_action(text) that splits a model reply of the form search: <query> or answer: <text> into a (kind, argument) pair, and returns ("invalid", text) for anything else. Print the result for three replies.
Write stop_reason(step, max_steps, queries, new_query) that returns "repeated query" when new_query is already in queries, "step limit" when step is at least max_steps, and None otherwise (check the repeat first). Print it for three situations.
Explain it without notes
What kinds of question need more than one search?
How does the loop work, and where do the limits belong?
What new risks come from repeating searches?
When would you not use agentic retrieval?
Expected output
Q: What happens if I return late, and what does the monthly pass cost?
step 1 search [bikes returned late] -> D6
step 2 search [monthly pass cost] -> D8
step 3 answer: Late returns cost 2 dollars per 15 minutes [D6]. The monthly pass costs 25 dollars [D8].
Q: Do you rent scooters?
step 1 search [scooter hire] -> nothing
step 2 search [scooter hire] -> nothing
step 3 search [scooter hire] -> nothing
stopped: step limit 3 reached