Lesson 5 / 12 · 11 min
Tools and function calling
How a model calls a function, how to design tools that are safe and reliable, and structured output.
Function calling is the mechanism behind every useful agent. You describe the tools available as structured definitions, the model decides when to use one and produces the arguments as data, and your code runs the actual function. Let us look at how that handshake works.
A tool is a typed contract
You give the model a name, a description, and a schema of the parameters (usually JSON Schema). The description matters as much as the code, it is how the model decides when this tool is the right one. Be specific about what the tool does and when to use it.
{
"name": "create_lead",
"description": "Save a new sales lead to the CRM. Use only after the
customer has shared a name and a contact number.",
"parameters": {
"type": "object",
"properties": {
"name": { "type": "string" },
"phone": { "type": "string", "description": "E.164, e.g. +62812..." },
"interest":{ "type": "string", "enum": ["website","ai","software"] },
"budget": { "type": "integer", "description": "IDR, optional" }
},
"required": ["name", "phone", "interest"]
}
}When the model decides to use it, it does not run anything, it returns a structured request like create_lead({ name: "Sari", phone: "+62812...", interest: "website" }). Your application validates those arguments, runs the database insert, and returns a result the model can read on the next turn.
Designing tools that do not break
- One job per tool. A tool that does three things confuses the model about when to call it.
- Validate everything the model sends. Treat its arguments like untrusted user input, because effectively they are.
- Make dangerous actions explicit and gated. Refunds, deletes, payments should require a confirmation step or a human.
- Return clear results, including errors. "out_of_stock" teaches the model more than a generic failure.
- Keep the list short. Five sharp tools beat twenty overlapping ones.
Structured output
Sometimes you do not want a conversation, you want data. Most models can be constrained to return strict JSON matching a schema. This is how you turn free text into something a system can use: extract the order details from a chat, classify a message as complaint or inquiry, pull fields from an invoice. Structured output is the bridge between language and your databases.
What you take away
- A tool is a name, a description, and a typed parameter schema
- The model returns a structured call; your code validates and executes it
- One job per tool, validate inputs, gate dangerous actions, return clear errors
- Structured output turns free text into schema-clean data for your systems
Curated sources
Hand-picked references for going deeper, reviewed by our team.