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 1 of 3

Describe tools with JSON Schema

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?

Describe tools with JSON Schema
Sign in to run this example in your browser.

Expected output

tools sent to the model: ['get_weather', 'add']
{
  "type": "function",
  "function": {
    "name": "add",
    "description": "Add two numbers.",
    "parameters": {
      "type": "object",
      "properties": {
        "a": {
          "type": "number"
        },
        "b": {
          "type": "number"
        }
      },
      "required": [
        "a",
        "b"
      ]
    }
  }
}