Tool Use
Function calling
Escrito por Darjus Kazakevic · revisado el
The model asks. Your code decides whether to do it.
On its own, a model can only produce text. Tool use lets it do more. You describe your functions as typed schemas. The model picks one and fills in the arguments. Your code runs it and returns the result. Reading a file, querying a database and sending a message all work this way.
Two facts shape the design. First, the model only writes a request, so your code stays in control of what runs. Second, the tool description is a prompt. The schema fixes the shape of the arguments, but only the description tells the model which tool to pick.
En esencia — A tool call is a written request, not an action. The model never touches anything; your code runs the tool or refuses.
Cuándo recurrir a él
Recurre a él cuando
- The model needs current or private data it cannot know
- The task ends in an action: a write, a message, a deploy
- Part of the job needs exact code: maths, lookups, validation
Reconsidéralo cuando
- One fixed retrieval step would do — use RAG, with no tool choice
- The “tool” only returns text the prompt could simply contain
- The action cannot be undone and nothing checks it first
The parts of a tool
- The schema (Input schema)
- A JSON Schema for the arguments: names, types, required fields and allowed values. It fixes the shape of the request, and many providers can enforce it while generating (a “strict” mode).
- The description
- Text that tells the model what the tool does, when to use it and when not to. It is the only thing that separates two tools with similar arguments, so it does more work than the schema.
- The dispatch
- Your code finds the tool by name, validates the arguments as untrusted input, and runs it. This is the one place where model output becomes an action, so permission checks and approvals belong here.
- The result
- What goes back into the transcript for the model to read. It is prompt content, so make it short, labelled and capped, and phrase errors so the model can act on them.
Reglas de diseño
- Write the description as a prompt
- Say what the tool does, when to use it, when not to, and whether it changes anything. The model picks between two tools that both “get order info” almost at random. A tighter schema does not fix that.
- Prefer a few clear tools to many similar ones
- Every tool is another choice and more tokens in every request. One search_orders with a filter beats six near-identical finders. Name tools for the job (search_orders), not for your route (GET /v2/orders).
- Give each tool the narrowest power that works
- A tool that runs any SQL can drop a table. A tool that runs one named, parameterized report cannot. A limit on what the tool can do holds, whatever the model was tricked into asking for.
- Write results for a reader with no memory
- The model sees only what you return. Label columns, give units, return “no matching orders” instead of an empty array, and write “…37 more rows” when you cut a list.
Errores habituales
- Treating the schema as the safety check. It limits the shape of the arguments, not their meaning.
- Exposing your REST API as it is, so the model sees endpoints instead of clear tasks.
- A rule in the description (“only SELECT”) instead of a tool that cannot do anything else.
- Forgetting that tool results can carry hostile instructions too, for example text from a web page.