An agent loop with validation and a step limit
The model here is a script. Change the scripted actions to see how the loop reacts.
Tool calling lets a model ask your code to run a function: you describe tools with JSON schemas, the model replies with a tool name and arguments, your code validates and runs it, and the result goes back. An agent is that exchange in a loop with a step limit, an allow-list and human approval for risky actions.
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 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write validate_args(parameters, args) that returns a list of problems for missing, unknown and wrongly typed arguments.
Write run_agent(model, tools, max_steps) with a step limit that returns the final answer or "gave up".
Write safe_dispatch(name, args, tools, risky, approve) with an allow-list and an approval gate.
Explain it without notes
Describe the sequence of messages in one tool-calling exchange.
Why does an agent loop need a step limit and argument validation?
How would you make a tool that deletes data safe to expose to a model?
Your agent sometimes loops, calling the same tool again and again. How do you prevent it, detect it and debug it?
Expected output
step 1: get_weather({'town': 'Pune'}) -> error: missing 'city'; unknown argument 'town'
step 2: get_weather({'city': 'Pune'}) -> 31C, clear
step 3: final answer
It is 31C and clear in Pune.
step 1: get_weather({'city': 'Oslo'}) -> unknown city
step 2: get_weather({'city': 'Oslo'}) -> unknown city
step 3: get_weather({'city': 'Oslo'}) -> unknown city
stopped: step limit reached