Search IOCombats

Search challenges, guides, questions and articles

What is tool calling, and who actually executes the tool?

Beginner8 min interview
Skills tested:
Explaining that a tool call is data the model produces, not code it runsDefining a tool with description, inputSchema and executeDescribing the request, tool call, tool result, follow-up request cycleKnowing the difference between app-executed, client-side and provider-executed toolsUnderstanding why tool arguments must be validated like any user input

Advertisement

🧩 Scenario

You are building an order-tracking assistant for an e-commerce site. A junior teammate believes the model "hits our orders API" when a user asks where their parcel is. The interviewer asks you to explain what tool calling actually is and where the code runs.

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

  1. Your app sends the conversation plus the tool definitions to the model.
  2. The model replies with one or more tool calls instead of (or as well as) text.
  3. Your app validates the arguments, runs the matching function, and captures the result or the error.
  4. Your app sends a second request with the tool result added to the messages.
  5. 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 execute function runs on the server inside generateText or streamText.
  • The browser. A tool without execute stops the server loop and is handed to the client. In React you handle it in useChat's onToolCall and return the result with addToolOutput. 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 runsGood forWatch out for
Server (execute)Databases, internal APIs, anything with secretsAuthorisation checks, timeouts, cost of extra steps
Browser (no execute, onToolCall)Location, clipboard, UI-only actionsNever put secrets here, the user controls the browser
Provider-executedHosted web search or code executionLess control over what runs and what it costs
No tool, plain promptPure text tasksModel 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

  1. What happens when the model asks for two tools in the same step?
  2. How would you stop the model from calling a tool when you want a plain text answer?
  3. Why does one tool call usually cost more tokens than a normal reply?
  4. How would you return a tool error so the model can recover instead of crashing the request?
  5. 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 doAction with no description, which makes tool choice unreliable.
  • Forgetting stopWhen and 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, inputSchema and execute?
  • Can you walk through the requests involved in one tool call?
  • Can you explain server, client and provider-executed tools?
  • Can you explain why execute must 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.

Frequently Asked Questions

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.

Namaste Dev
Sponsored

Cohort-style courses to go from tutorials to job-ready.

Explore Courses

Advertisement