Lesson 9 — Dispatching a tool call
From JSON to a function call
When the model wants a tool, it replies with a tool call: JSON naming the tool and its arguments.
{"tool": "add", "args": {"a": 4, "b": 6}}
Dispatching means: find the function by name in the registry and call it with those arguments. The ** operator unpacks a dict into named arguments, so add(**{"a": 4, "b": 6}) is the same as add(a=4, b=6).
Why it matters: this is the "act" step of the loop. It is also a safety point — the model may ask for a tool that does not exist (or that you never gave it). Your dispatcher must say no instead of crashing.
Finish dispatch(): return "error: unknown tool <name>" for names not in TOOLS; otherwise call the tool from TOOLS with the args as keyword arguments and return the result.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.