Function calling (or tool use) lets a language model ask your application to run a specific tool, such as a search, a database query or an API, with structured arguments. The model never runs code itself: your app validates the request, runs the tool, and returns the result for the model to use. Repeat this in a loop and you have an AI agent.
How does tool use work, step by step?
- Describe tools: you send the model a list of tools with names, descriptions and a JSON schema for their arguments.
- Model decides: for a user request, the model either answers directly or returns a tool call, for example
get_train_statuswith{"train_no": "12301"}. - Your code validates and runs it: check the arguments and permissions, then call the real function.
- Return the result: send the tool's output back to the model.
- Model continues: it answers the user or calls another tool.
How do you define good tools?
| Practice | Why |
|---|---|
| Clear names and descriptions | The model chooses tools from their descriptions |
| Strict argument schemas | Fewer malformed calls; easier validation |
| Small, single-purpose tools | Easier for the model to pick correctly and for you to test |
| Helpful error messages | The model can recover by retrying with fixed arguments |
| Read-only by default | Write or payment actions should need confirmation |
How do you keep tool use safe?
- Validate every argument on your side; never trust the model's output blindly.
- Give the narrowest permissions possible (allow-lists, scoped API keys).
- Ask a human before irreversible actions such as payments, deletions or emails.
- Treat tool results from the web as untrusted input that could contain prompt injection.
- Log calls and cap the number of steps per task.
From tool use to agents
Once a model can call tools reliably, wrapping the process in a loop with memory and limits gives you an agent. Read how to build AI agents and multi-agent systems next. In Program Zero, APIs and backend design come in Phase 4, agents and tool use in Phase 8, and AI security in Phase 11.