Tool and Function Calling
Let a model request actions through tools, while your code decides what is allowed, checks arguments, and runs the call.
Objective
By the end of this lesson, you can explain the tool-calling loop, write tool definitions with tight inputs, and build a dispatcher where code, not the model, enforces what may run.
How tool calling works
Tool calling (also called function calling) lets a model ask your application to do something: look up hours, search a database, check the weather. The loop has four steps:
- Your app sends the model a list of tools, each with a name, a description, and an input schema.
- The model replies with a proposed call: a tool name and arguments, as structured data.
- Your code checks the call and, if allowed, runs it.
- Your app sends the result back, and the model writes its answer.
The key fact: the model never runs anything. It writes a request. Your code is the only thing that acts. That makes your dispatcher the security boundary.
Designing tools well
- Small and specific.
lookup_hours(site)beatsrun_sql(query). A tool that can do anything will eventually be asked to. - Typed inputs. Use enums (fixed lists) where you can, such as site names.
- Read before write. Start with read-only tools. Add tools that change data only with approval gates (Module 5).
- Clear descriptions. The model picks tools by their descriptions. Vague descriptions cause wrong picks.
- Results as data. Tool results go back to the model as context. Treat them as untrusted, since they may contain text from outside (Module 8).
{
"name": "lookup_hours",
"description": "Get opening hours for one pantry site. Read-only.",
"input_schema": {
"type": "object",
"properties": {
"site": { "type": "string", "enum": ["eastside", "northgate"] }
},
"required": ["site"]
}
}
Build the dispatcher (Python)
The first run downloads Python into your browser (several MB).
The same idea in JavaScript
Many web apps run the dispatcher in JavaScript. Edit the allowlist and watch the console. Keyboard users: press Esc, then Tab, to leave the editor.
Allowlist dispatcher
Run it. Then add an 'args' type check like the Python version, so lookupHours refuses a number.
Notice the last call: without a type check, the JavaScript version quietly returns "unknown site." Quiet failures are hard to find in logs. Prefer loud refusals.
When not to use tool calling
If the steps are always the same, do not let a model choose them. Call the function directly in code. Tool calling earns its place when the right tool depends on what the person asked and you cannot know ahead of time.
Quiz: Tool calling check
Discussion
Ask. Answer. Build together.
Loading…