Lesson 11 / 27

How Tool Use Works

Follow the loop of request, tool call and result.

The model never runs your code

A model cannot look up your orders or send an email by itself. With tool use (function calling) you describe functions (name, purpose and a JSON Schema for the arguments) in the request. If the model decides a tool would help, its reply is not final text but a structured request: "call get_order_status with order_id = 481516", and the stop reason says so (tool_use for Anthropic, tool_calls for OpenAI). Your code validates the arguments, runs the function, and sends the result back in the next request, together with the earlier messages. The model then continues, possibly asking for more tools, until it returns a normal answer. You own the loop, so you can enforce permissions, limits and logging.

The model asks, your code acts

You describe tools; the model requests a call; your code runs it and sends back the result.

Four steps: define, request, execute, return.
Figure 4.1 — Define, request, execute and return.

The tool loop in one picture

Two API calls and one local function call answer one user question.

user: "Where is order 481516?"
  -> API call 1 (with tool definitions)
  <- model: stop_reason = tool_use / tool_calls
         "call get_order_status(order_id='481516')"
  -> YOUR CODE validates args, runs get_order_status -> {"status": "shipped"}
  -> API call 2 (history + the tool result)
  <- model: "Your order 481516 has shipped."   (stop_reason = end_turn / stop)

Several tools in one reply

A single model reply can request several tool calls. Return a result for every one before the next request.

Quick check: Who actually executes a tool call?

  • The browser
  • The model
  • The provider's GPU
  • Your application code
Answer

Your application code — The model only requests the call; you decide whether and how to run it.