What actually happens when the model "calls a tool"

L1 · FundamentalsConcept~6 minTool use

A junior engineer says "the model ran our get_weather function and it failed". Explain to them what a tool call really is, end to end: who runs what, and where it can go wrong.

Answer it out loud or on paper first. Then open the short answer.

Short answer

The model never runs anything. You send it tool definitions (name, description, JSON Schema for arguments). It replies with a structured request: "call get_weather with {"city": "Paris"}", plus a call id. Your code looks the name up, validates the arguments, runs the function, and sends the result back as a tool message tied to that id. Then the model continues. Failures can happen at each hop: the model picks the wrong tool or invents arguments, your dispatcher can't find or validate it, the function raises, or the result is too big or unclear for the model to use.

Full answer

The round trip

  1. Define. Each tool is a name, a description the model reads, and a JSON Schema for its parameters.
  2. Request. You send messages plus tool definitions. The provider renders them into the prompt.
  3. Decide. The model generates either text or a tool call: name, arguments (as JSON) and an id. Some models return several calls at once.
  4. Dispatch. Your code maps the name to a function, parses and validates arguments, and runs it.
  5. Observe. You append a tool-result message with the same id and the output (or error) as text.
  6. Continue. You call the model again with the extended history. Repeat until it answers without a tool call.

Where it breaks

HopFailureFix
DecideWrong tool, hallucinated tool name, made-up argument valuesClear names and descriptions, fewer tools, validate
DispatchUnknown name, invalid JSON, wrong typesReturn an error observation, don't crash
RunException, timeout, side effect half-doneCatch, return the error as text, idempotency
ObserveHuge or cryptic resultTrim, structure, explain errors

Why it matters that the model doesn't run code

Security and correctness live in your dispatcher and tools. The model can only ask. That's the place to enforce permissions, limits and validation.

Follow-ups

  1. L1. What's the call id for?

    Expected answer

    Matching results to requests, especially when the model asks for several tools in one turn.

  2. L2. How do providers differ here?

    Expected answer

    Field names and shapes differ (tool_calls vs tool_use blocks, arguments as a string vs an object), but the loop is the same; an adapter layer normalises them.

  3. L2. Can you do tool calling with a model that has no native support?

    Expected answer

    Yes: describe tools in the prompt and parse a JSON action from the text, with more validation and retries. Native support is more reliable.

Wrong answers interviewers hear
  • "The model executes the function inside the provider." It only emits a request; your code executes.
  • "If the arguments match the schema they're safe to use." Schema-valid isn't the same as authorised or correct.
  • "When a tool fails the run should stop." Return the error as an observation so the model can recover.

Taught in

Improve this answer