AI Tool Calling: How It Works and How to Run It Safely
AI tool calling explained for builders: the request-execute-return loop, schemas, MCP, local models, debugging failed calls, and approval gates for risky actions.

AI tool calling is the mechanism that lets a language model ask your software to do something: query a database, call an API, click through a website. The model never runs anything itself. It emits a structured request, your application validates and executes it, and the result goes back to the model. Everything that matters in production, from permissions to debugging, lives in that gap between request and execution. That gap is where we build Melaya: a controlled execution layer that works with any model you bring.
TL;DR
- Tool calling is a loop: you describe tools with schemas, the model returns a structured call, your code executes it, and the result returns as a message.
- The model only requests. The application validates, permits, executes and decides whether the model gets another turn, so the application is the security boundary.
- "Tool calling", "function calling" and "tool use" name the same feature. MCP standardizes how tools are described, so one tool server can serve many clients.
- Debug by tracing every step: the tools offered, the call requested, the validation result, the execution output and the final answer.
- In Melaya, you bring your own model (cloud or local), and every consequential action runs behind human approval.
What tool calling actually is
A language model produces text. On its own it cannot read your CRM, check an order, or open a browser tab. Tool calling closes that gap. You hand the model a list of tools, each with a name, a plain-language description and a parameter schema. When a request needs one of them, the model answers with a tool call instead of prose.
The vocabulary varies by vendor. OpenAI popularized "function calling", Anthropic documents it as tool use, and agent frameworks say "tool calls". The mechanics are the same. In an agent, tool calls are the actions: each step of a multi-step task is the model choosing a tool, receiving its output, and choosing again.
A useful way to hold it: your AI is the brain, and the tools are the hands. The brain decides what to reach for. Something else has to control the hands.
How the loop works, step by step
Arize AI describes the cycle in four moves. Here it is with what each step demands from you.
- Define tools. Each tool gets a name, a description and a JSON Schema for its parameters. The description is what the model reads to decide when to use the tool, so it carries more weight than most teams expect.
- The model emits a call. Instead of text, the response contains a structured entry with the tool name and an arguments object. In OpenAI-style APIs the finish reason signals a tool call. In Anthropic's API, Claude returns a stop reason of tool_use with one or more tool_use blocks.
- Your code executes. You look up the handler, validate the arguments against the schema, check permissions and run the operation. This step is yours, not the model's.
- Return the result. You append the output as a tool-result message tied to the call ID, then invoke the model again. It reads the result and either answers or requests another call.
That is the whole protocol. A minimal call looks like this:
{"name": "get_order_status", "arguments": {"order_id": "A-10442"}}
Your handler runs the lookup, returns {"status": "shipped"}, and the model writes the customer-facing sentence.
Where each step goes wrong
| Step | Typical failure | What prevents it |
|---|---|---|
| Define | Two tools with overlapping descriptions | One job per tool, distinct names, explicit "use when / do not use when" lines |
| Call | Wrong tool, missing or invented arguments | Strict schemas with enums and required fields |
| Execute | Timeouts, auth errors, unsafe side effects | Per-call timeouts, scoped credentials, approval gates |
| Return | Huge or ambiguous output the model misreads | Trim results, return structured errors |
| Repeat | Endless loops and runaway cost | Step caps and budgets per run |
The model requests, your application decides
The most important sentence in this topic is short: the model does not execute the function. Aalto University's course material states that the model only asks for a call, and the surrounding application stays responsible for deciding whether the call is valid, running the deterministic code, formatting the result and deciding whether the model gets another turn.

That split is a security design, not an implementation detail. Because the application sits between request and action, oversight patterns such as approvals and sandboxing stay enforceable. Arize makes the same point from the blame side: validation and responsibility for a bad call sit with the application, and overlapping tool descriptions cause wrong-tool failures that teams often misread as a weak model.
If you are building this layer from scratch, the work is bigger than the loop. You need argument validation, an allowlist of permitted tools, credential scoping, timeouts, retry limits and an audit trail. Training on AI agent safeguards for autonomous systems covers the same boundary-and-oversight thinking at a professional-education level. For production tool use, our guide to AI agent security best practices lists the controls we apply.
What tools a model can call
Tool calling is only as useful as the tools behind it. The common categories in business settings:
- Data retrieval: search over document stores and vector indexes, so answers come from your content.
- Structured data: SQL execution and database connectors, with access controls on what can be read or written.
- APIs and connectors: REST calls, webhooks, and CRM, ticketing and payment integrations.
- Code execution: sandboxed runtimes for calculations and transformations of data.
- Custom business logic: discount approval, claims routing, anything specific to your process.
- MCP server tools: capabilities exposed through the Model Context Protocol.
This taxonomy follows Teradata's enterprise guide to agent tools. Notice what is missing from most lists: tools for software that has no API. Many real workflows live in a web app, an Android app or a desktop program. That is where API-only tool calling stops, and where UI-level control starts.
MCP: one tool description, many clients
Without a standard, every model vendor and every client defines tools its own way, and you rewrite integrations per pair. The Model Context Protocol fixes the description side. Under the MCP specification, a server lists its tools, and each tool carries a name, a human-readable description and an input schema in JSON Schema, with optional output schema and annotations describing behavior.
The same specification recommends that clients show which tools are exposed to the model, signal visibly when a tool is invoked, and present confirmation prompts so a human stays in the loop. That recommendation matches how consequential actions should work everywhere.
The practical result: write or connect a tool server once, then use it from any MCP-compatible client. The Melaya MCP Server brings Melaya's capabilities into clients such as ChatGPT, Claude, Cursor and Codex, so the tool layer stays the same whichever client you work in.
Tool calling with any model, cloud or local
Tool calling quality differs between models, and it differs more between small local models and frontier cloud models. Three practical rules hold across them:
- Keep the tool list short. More tools means worse selection. Give each run only the tools it needs.
- Make schemas strict. Enums, required fields and clear types shrink the space of malformed calls, which matters most on smaller models.
- Feed errors back as structured messages. When a call fails validation, return the reason to the model so it can correct the arguments, rather than crashing the run.
Melaya is bring-your-own-model. It works with any cloud or local model: Claude, GPT, Gemini, DeepSeek, Mistral, Qwen, Ollama and LM Studio. We do not sell a model and agents do not require one of ours. Your tools, memory, schedules and workflows stay in place when you switch models or move to local infrastructure, so a model swap is a configuration change rather than a rebuild.
For open-weight users this is the missing piece: a local Qwen or Mistral running in Ollama can reason well enough for many tasks, but it needs a governed set of hands, with permissions, approvals and traces, that does not depend on one vendor's API.
Debugging AI agents: trace every step
An agent that fails rarely fails loudly. It picks a plausible but wrong tool, passes a slightly wrong argument, or loops. Logs of the final answer tell you nothing. You need a trace of the whole run.
Triage in the order the loop runs:
- Was a call expected? If the task needed a tool and the model answered from memory, the tool description or the tool choice setting is the suspect.
- Was the right tool requested? Wrong-tool errors usually trace to overlapping descriptions or too many tools in scope.
- Did the arguments pass validation? Log the validator result with a reason code, such as missing field or value outside the enum.
- Did execution succeed? Record status, latency and the raw output. Timeouts and auth failures show up here.
- Did the model use the result correctly? Oversized or ambiguous output is the common culprit.
- Did retry or fallback help? Cap retries and set a step budget so a failing loop stops instead of burning money.
Fix in that order too: instrumentation first, then validators, then tool descriptions, then workflow state and retry control. Prompt and model tuning come last, once you know which failure bucket dominates. Tuning a prompt before you have traces is guessing.
The minimum record per step: a run or session ID, model and prompt version, the candidate tool set, the requested tool and arguments, the validation outcome, execution status and latency, and the final result.
Replay beats reproduction
Agent failures are hard to reproduce because the model is probabilistic and the world changes between attempts. A stored trace removes the guesswork. Melaya runs are traceable and replayable: you see what the agent was given, which tool it called, with what arguments, and what came back, then step through it again. Our write-up on preventing agent hallucinations by design covers how grounding tool output reduces the invented-result failures that traces tend to expose.
Permissions and approvals for consequential actions
A read-only lookup and a payment are both "a tool call" to the model. They are not the same to you. Treat tools in tiers:
- Read: low risk, allowed by default within scope.
- Write to connected apps: change records, create tickets, update data.
- Send or publish: email, messages, posts, anything an outside party sees.
- Pay or commit: spending, orders, contracts.
In Melaya, every consequential action, meaning publishing, sending, paying or writing to connected apps, runs behind human approval. Agents propose, a person approves, and the approval is recorded in the execution history. Granular permissions define which tools an agent may call at all, so a marketing agent never holds a database-write tool it has no reason to need. Our guides on human-in-the-loop AI agents and AI agent governance set out how to define those tiers and who approves what.
Tool output is also an input channel. A web page or email the agent reads can contain instructions aimed at the model. Treat returned content as untrusted, keep sensitive tools behind approval, and read our note on prompt injection protection.
When the tool is a browser, a phone or a desktop
APIs cover part of the work. The rest sits behind interfaces built for humans. Melaya's Browser Control lets an approved model operate websites, Mobile Agents operate Android applications (Android only, not iOS), and Computer Control operates desktop software through the Melaya desktop app. Each of these is still tool calling: the model requests "click", "type" or "read screen", and the execution layer performs it under the same permissions, approvals and traces.
If you are weighing this against scripted test frameworks, see how AI browser automation compares with Selenium and Playwright. For the design behind agents that combine models, tools and memory, read our overview of AI agent architecture.
Where Melaya fits
Melaya gives your chosen model the tools to perform real work without you building the execution layer. The visual Agent Builder combines models, tools, memory, schedules, approvals, triggers and multi-agent pipelines, with every run traceable and replayable. Melaya Assistant works across connected business systems, and native integrations include Odoo.
Pricing is published per product, in US dollars:
| Plan | Price |
|---|---|
| Sandbox | $0 per month |
| Browser Agents | $9.99 per month |
| Mobile Agents | $9.99 per month |
| Melaya Assistant | $9.99 per month |
| Melaya Marketing | $14.99 per month |
| Outpost | $20 per month |
| Forge | $49 per month, or $469 per year |
| Bastion | $129 per month, or $1,239 per year |
| Citadel | from $490 per month |
Start in Sandbox to build a first agent with your own model, then move up when you need more capacity or controls. Full plan details are on the Melaya pricing page.
If your use case is enterprise-scale, with private deployment and provider independence, see the enterprise AI agent platform page.
Our view on tool calling
Most teams treat tool calling as a model feature. We treat it as an execution problem. The model is the part you can swap every quarter. The part that has to stay stable is everything around it: which tools exist, who may call them, what a call is allowed to touch, and what record is left behind.
That is why Melaya is bring-your-own-model. Claude, GPT, Gemini, DeepSeek, Mistral, Qwen, or a local model in Ollama or LM Studio all plug into the same tool layer. Your workflows do not depend on one vendor's pricing or roadmap.
It is also why we put approvals in the path of every consequential action. An agent that can publish, send or pay without a person in the loop is an incident waiting for a bad day. An agent that proposes the action, shows its reasoning and waits for a yes is one you can put on a schedule.
Build with traces from the first run. When something breaks, you want the sequence of calls in front of you, not a guess.
Melaya Team
FAQ
What is the difference between tool calling and function calling?
None in practice. Function calling was the first name, tool calling is the broader term, and some vendors say tool use. In each case you describe functions with schemas, the model returns a structured request, and your application executes it.
Does the model run the tool itself?
No. The model returns a structured request naming a tool and its arguments. Your application validates the arguments, checks permissions, runs the code and returns the result. That boundary is what lets you add approvals and limits.
Can local or open-weight models do tool calling?
Yes, many can, including Qwen and Mistral models run through Ollama or LM Studio. Reliability varies by model size, so use strict schemas and a short tool list. In Melaya you connect a local model to the same tools and approval controls as a cloud one.
How do I debug a failing AI agent?
Capture a full trace of each run: the tools offered, the call requested, the validation result, the execution output and the final answer. Then walk the loop in order and fix instrumentation and validation before touching prompts. Melaya runs are traceable and replayable.
What is MCP and how does it relate to tool calling?
The Model Context Protocol is a standard way for a server to describe tools to any compatible client. Tool calling is the model behavior, MCP is the shared interface that tells clients which tools exist and how to call them.
Sources
- Tool use with Claude, Claude Platform Docs
- What Is Tool Calling (Function Calling), Arize AI
- Function Calling and Tool Schemas, Aalto OpenCS
- AI Agent Tools: The Enterprise Guide, Teradata
- Tools, Model Context Protocol specification
Recommended
- AI That Uses Your Phone: Android Device Control
- AI That Uses Your Browser: Melaya Browser Control
- AI Browser Automation vs Selenium and Playwright
- Android Accessibility AI Agent Tutorial (No Root)
Frequently asked questions
What is the difference between tool calling and function calling?
None in practice. Function calling was the first name, tool calling is the broader term, and some vendors say tool use. In each case you describe functions with schemas, the model returns a structured request, and your application executes it.
Does the model run the tool itself?
No. The model returns a structured request naming a tool and its arguments. Your application validates the arguments, checks permissions, runs the code and returns the result. That boundary is what lets you add approvals and limits.
Can local or open-weight models do tool calling?
Yes, many can, including Qwen and Mistral models run through Ollama or LM Studio. Reliability varies by model size, so use strict schemas and a short tool list. In Melaya you connect a local model to the same tools and approval controls as a cloud one.
How do I debug a failing AI agent?
Capture a full trace of each run: the tools offered, the call requested, the validation result, the execution output and the final answer. Then walk the loop in order and fix instrumentation and validation before touching prompts. Melaya runs are traceable and replayable.
What is MCP and how does it relate to tool calling?
The Model Context Protocol is a standard way for a server to describe tools to any compatible client. Tool calling is the model behavior, MCP is the shared interface that tells clients which tools exist and how to call them.

