Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 12.7 — Retrieval as a Tool
RAG and Vector Search · Example 1 of 1

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.

01

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.

02

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

01

What kinds of question need more than one search?

02

How does the loop work, and where do the limits belong?

03

What new risks come from repeating searches?

04

When would you not use agentic retrieval?

A step-limited search loop with a scripted model
Sign in to run this example in your browser.

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