What is tool calling, and who actually executes the tool?
Advertisement
🧩 Scenario
Concept Walkthrough
A tool call is a request, not an action
Think of the model as a teammate who can only send you messages. You hand it a list of functions it is allowed to ask for, with a name, a description and an input schema. When it decides it needs one, it does not run anything. It replies with something like { toolName: 'getOrderStatus', input: { orderId: 'ORD-1042' } }. That reply is just JSON. It is closer to a Redux action than a function call: it describes what should happen, and your code decides what actually happens.
Provider APIs show this clearly. Anthropic, for example, returns a stop_reason of "tool_use" with the tool name and input, and your app sends back a tool_result that references the call id.
The round trip
- Your app sends the conversation plus the tool definitions to the model.
- The model replies with one or more tool calls instead of (or as well as) text.
- Your app validates the arguments, runs the matching function, and captures the result or the error.
- Your app sends a second request with the tool result added to the messages.
- The model reads the result and either answers in text or asks for another tool.
So "who executes the tool?" has a clear answer: your code does, on your infrastructure, with your permissions.
Three places a tool can run
- Your server. The common case. In the AI SDK, a tool with an
executefunction runs on the server insidegenerateTextorstreamText. - The browser. A tool without
executestops the server loop and is handed to the client. In React you handle it inuseChat'sonToolCalland return the result withaddToolOutput. Good for things like reading the user's location. - The provider. Some providers offer hosted tools (web search, code execution) that run on their infrastructure. The AI SDK marks these with
providerExecuted. You get the results, but you did not run the code.
Why this matters in interviews
Once you understand that the model only asks, everything else follows: you validate arguments like form input, you check authorisation inside execute, and you can add an approval step before risky calls.
Key Code Explained
import { generateText, tool, isStepCount } from 'ai';
import { z } from 'zod';
const getOrderStatus = tool({
// The description is what the model reads to decide when to use the tool
description: 'Get the shipping status of one order by its order id.',
inputSchema: z.object({
orderId: z.string().regex(/^ORD-\d+$/).describe('Order id like ORD-1042'),
}),
// execute runs in YOUR process, never inside the model
execute: async ({ orderId }, { abortSignal }) => {
const res = await fetch(`${process.env.ORDERS_API}/orders/${orderId}`, {
signal: abortSignal,
});
if (!res.ok) return { found: false };
const order = await res.json();
return { found: true, status: order.status, eta: order.eta };
},
});
export async function answer(question: string) {
const result = await generateText({
model: 'anthropic/claude-sonnet-5.5', // example id
tools: { getOrderStatus },
// Allow up to 3 steps: tool call, tool result, final answer
stopWhen: isStepCount(3),
prompt: question,
});
// Every step records what the model asked for and what your code returned
for (const step of result.steps) {
console.log(step.toolCalls, step.toolResults);
}
return result.text;
}
The SDK validates input against inputSchema before execute runs, so orderId is already typed and checked. Without stopWhen, generateText runs a single step: you would get the tool call and its result, but no final text answer, because the default stop condition is one step.
Tradeoffs
| Where the tool runs | Good for | Watch out for |
|---|---|---|
Server (execute) | Databases, internal APIs, anything with secrets | Authorisation checks, timeouts, cost of extra steps |
Browser (no execute, onToolCall) | Location, clipboard, UI-only actions | Never put secrets here, the user controls the browser |
| Provider-executed | Hosted web search or code execution | Less control over what runs and what it costs |
| No tool, plain prompt | Pure text tasks | Model guesses data it does not have |
What Interviewers Actually Check
- Whether you say clearly that the model produces a request and your code runs it
- Whether you can describe the multi-request round trip, not just "the model calls the API"
- Whether you treat tool arguments as untrusted input
- Whether you know tools can run on the server, the client or the provider
- Whether you know the description and schema steer which tool gets picked
Follow-Up Questions
- What happens when the model asks for two tools in the same step?
- How would you stop the model from calling a tool when you want a plain text answer?
- Why does one tool call usually cost more tokens than a normal reply?
- How would you return a tool error so the model can recover instead of crashing the request?
- When would you move a tool from the server to the browser?
Common Candidate Mistakes
- Saying "the LLM calls our API". It cannot. Your code does.
- Skipping validation because "the model follows the schema". Usually it does, sometimes it does not.
- Giving tools vague names like
doActionwith no description, which makes tool choice unreliable. - Forgetting
stopWhenand wondering why there is no final answer after the tool runs. - Exposing a client-side tool that needs an API key, which leaks the key to the browser.
Interview Readiness Checklist
Before you leave this question, make sure you can answer:
- Can you explain tool calling in one sentence without saying the model runs code?
- Can you write a tool with
description,inputSchemaandexecute? - Can you walk through the requests involved in one tool call?
- Can you explain server, client and provider-executed tools?
- Can you explain why
executemust check permissions itself?
Summary
Tool calling is a contract between your app and the model. You describe functions with a name, a description and a schema. The model answers with a structured tool call: a name and JSON arguments. Your code validates those arguments, runs the function with your own permissions, and sends the result back so the model can continue. The model never executes anything itself, except for provider-hosted tools that the provider runs for you. In the AI SDK, a tool with execute runs on the server, a tool without it is handed to the client, and stopWhen controls how many request rounds happen before the final answer.
Does the model call my API directly when it uses a tool?
No. The model only produces a tool call: a tool name and arguments as data. Your server (or browser, for client-side tools) decides whether to run it, runs it, and sends the result back in the next request. Hosted tools such as a provider web search are the exception, because the provider runs them for you.
Is tool calling the same as function calling?
Yes, they are two names for the same idea. OpenAI popularised the term function calling, and most SDKs, including the AI SDK, now call it tool calling.
What happens if the model sends arguments that do not match my schema?
The AI SDK validates the arguments against inputSchema before calling execute. Invalid input surfaces as an InvalidToolInputError, and you can use the repairToolCall option to try to fix it. Never assume the arguments are valid just because you gave the model a schema.
Advertisement
Stay Updated
Get the latest frontend challenges, interview questions and tutorials delivered to your inbox.
Advertisement