Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 16.10 — Tool Calling and an Agent Loop from Scratch
Python · Example 3 of 3

Allow-list and human approval

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.

01

Write validate_args(parameters, args) that returns a list of problems for missing, unknown and wrongly typed arguments.

02

Write run_agent(model, tools, max_steps) with a step limit that returns the final answer or "gave up".

03

Write safe_dispatch(name, args, tools, risky, approve) with an allow-list and an approval gate.

Explain it without notes

01

Describe the sequence of messages in one tool-calling exchange.

02

Why does an agent loop need a step limit and argument validation?

03

How would you make a tool that deletes data safe to expose to a model?

04

Your agent sometimes loops, calling the same tool again and again. How do you prevent it, detect it and debug it?

Allow-list and human approval
Sign in to run this example in your browser.

Expected output

<tool_result>get_weather ran with {'city': 'Pune'}</tool_result>
refused: tool not on the allow-list
  approval asked for send_email{'to': 'a@example.com'}: no
refused: a human did not approve
  approval asked for send_email{'to': 'a@example.com'}: yes
<tool_result>send_email ran with {'to': 'a@example.com'}</tool_result>