Function Calling & Tool Use: How AI Runs Commands
By Darus S Alfatah · 2026-11-03 · Tutorial · 9 menit
Author note: Wrong tool calls are almost always caused by the description, not the model. Clarifying descriptions fixed most of our issues.
From Chatting to Acting
Language models fundamentally only produce text. To "do something" — check stock, send email, calculate tax — we need a bridge: function calling or tool use. The model is given a list of available functions with their schemas, then chooses when to call them and with what arguments. Your application executes the function and returns the result to the model.
How the Flow Works
- The app sends the user message plus a list of tools (name, description, parameter schema).
- The model replies not with text but with a request to call a specific tool and its arguments.
- The app runs the tool and sends the result back to the model.
- The model composes a final answer based on the tool result.
{
"model": "auto",
"messages": [{"role":"user","content":"Weather in Bandung?"}],
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a city",
"parameters": {
"type": "object",
"properties": { "city": {"type":"string"} },
"required": ["city"]
}
}
}]
}
Why This Matters for AI Agents
Function calling is the backbone of autonomous agents. With tools, a model can search the web, read a database, run code, or change data. The "loop" pattern (call → execute → observe → repeat) lets an agent finish multi-step tasks without a human at every step.
Safe Practices
- Narrow tool definitions — avoid a "do anything" function; limit operations.
- Validate arguments — check input against the schema before executing.
- Permissions & confirmation — destructive operations require human approval.
- Iteration limits — cap loops so the agent does not spin forever.
- Logging — record every tool call for audit.
Common Mistakes
Ambiguous tool descriptions make the model choose wrong. Loose parameter schemas trigger unexpected arguments. No timeout leaves requests hanging. Never execute raw commands from the model without sanitisation — this is the most serious security gap in tool-based systems. Treat model output as untrusted input.
Choosing a Model for Tool Use
Not all models are equally skilled at tool calling. Reasoning and large models are generally more reliable at multi-step planning, while small models are faster and cheaper for single tools. Test with your real scenario, because function-calling ability often differs from general benchmark scores.
Use Cases in Indonesia
- E-commerce — check stock and shipping via internal APIs.
- Fintech — fetch balance and transaction history (read-only) for an assistant.
- Public services — check application status by ticket number.
Designing Effective Tool Descriptions
The description is the model's "documentation". State clearly what the function does, when to use it, and what each parameter means. Include example values. A poor description is the number-one cause of wrong tool calls — not model weakness.
Handling Multiple Tools at Once
A model can request several tools in parallel. Execute independent ones in parallel, then collect results before continuing. Limit how many tools are exposed at once; too many choices actually reduce selection accuracy.
Parallel vs Sequential
Interdependent tools must be sequential; independent ones can be parallel to save time. Determine dependencies from the schema, not from guesswork.
Security: Treat the Model as Untrusted
- Never
eval()model output. - Limit each tool's permissions (least privilege).
- Sanitise all arguments before touching real systems.
- Separate read and write tools; write tools require confirmation.
A Simple Agent Loop
while iterations < 5:
resp = model(messages, tools)
if resp.tool_call:
result = run(resp.tool_call)
messages += [resp, result]
else:
return resp.text
The 5-iteration cap prevents infinite loops and caps cost.
Testing Agents Before Production
Build a set of test tasks with known answers. Run the agent and check whether it picks the right tool, uses valid arguments, and stops in reasonable time. Record the success rate per task, not just the average. Failing cases usually reveal a tool with an unclear description or an overly loose schema.
When Not to Use an Agent
If a task is deterministic and can be solved with ordinary code, do not use an agent. Agents add cost, latency, and uncertainty. Use function calling only when flexibility in choosing actions based on context is genuinely needed.
Conclusion
Function calling turns a model from a mere answerer into a task performer. Design tools narrowly, validate arguments, and cap iterations. Every model on BankAI supports the OpenAI-compatible tool format, so your agent code stays portable.
Example: a single order-lookup tool: Suppose a chatbot needs to answer "where is my order?". Define a getOrder(status) tool with a clear JSON schema. The model calls the tool only when needed, your app runs the query, then the result is sent back to the model. Never let the model guess delivery status.
Frequently Asked Questions
Can beginners follow this tutorial?
Yes. The steps are explained in order with code examples, suitable for beginners and experienced developers alike.
Is BankAI’s endpoint compatible with the library I already use?
Yes, BankAI is 100% OpenAI-compatible, so libraries such as the OpenAI SDK and LangChain work without code changes beyond the Base URL and API key.
Where do I get an API key to follow this tutorial?
You get the API key (and Secret Token) after registering and completing payment in the BankAI dashboard.