Function/Tool Calling
Gen AI · AI Agents & Tool Use
Theory
Tool calling (also called function calling) lets a model ask your application to run a predefined function, such as querying a database, calling a weather API or doing exact arithmetic, and then use the real result in its answer. You describe each tool with a name, a description and a schema for its arguments. The model never executes code itself: it replies with structured data, usually JSON, naming the tool and the arguments it wants.
Your code checks those arguments, runs the real function and sends the result back, and the model writes the final answer, or asks for another tool. This is how assistants report live order status, read your calendar or compute exact totals instead of guessing. Clear tool descriptions and strict argument validation matter as much as the prompt.
Why we use it
Models cannot see live data or reliably do exact calculations from memory. Tool calling hands those jobs to real code, so answers use current, correct values and the app can safely take actions.
When to use it
Use it when answers need live or private data, exact computation or an action like creating a ticket. If the needed information is static text, RAG may be simpler; if no external data or action is needed, a plain prompt is enough.
How it works
- App sends the question plus tool definitions
- Model decides whether a tool is needed
- Does the model request a tool? Yes: App validates args and runs it. No: Model answers directly.
- App sends the tool result back to the model
- Model writes the answer or requests another tool
- Final answer uses real data
Example
Function schema you define:
{"name": "get_stock_price", "parameters": {"ticker": "string"}}
Model output (not code — just structured intent):
{"function": "get_stock_price", "arguments": {"ticker": "AAPL"}}
Your app runs the real function, gets "$187.42", and sends it back to the model, which replies: "AAPL is currently trading at $187.42."Key points
- The model outputs a tool name and arguments matching a schema you define
- Your code executes the real function; the model never runs code itself
- Validate arguments before running anything, especially writes
- Clear tool names and descriptions help the model choose correctly
- Enables live data, exact calculations and actions the model cannot do alone