Loading video player…

Run the Tool-Calling Loop

In this video, you followed a passenger’s question about Saturday’s sailings through the tool-calling loop. The model asked for two lookups in one reply, your code ran both and sent the results back by their IDs, and the model’s next reply was the answer.

Key Points

  • The history carries everything. The model keeps nothing between calls, so your code keeps the conversation as a history and sends all of it, with the instructions and the tools, on every request.
  • One reply can hold several tool calls. The passenger asked about Gull Island or Heron Island, and in this run, the reply held two calls to findSailings, each with its own ID, the tool’s name, and its arguments. Whether the calls come together varies from run to run, so your run may ask for one per round.
  • Every call needs a result. Answer only the first of two calls, and the next request fails.
  • The reply joins the history first. Your code adds it as an assistant entry that holds the tool calls, so the model sees its own requests on the next call.
  • Run each call, and answer it by its ID. For now, each call’s arguments go into your own function unchecked, and checking them comes later in the course. Its result joins the history as a tool entry whose callId is the call’s ID, so the model can match the result to its request. Its isError flag is false when the function worked.
  • Call again with the whole history. The second request carried 393 input tokens, about twice the first request’s 193, because the calls and their results are part of the input now.
  • A finished reply without calls is the answer. A reply that was cut off or refused has no calls either, so your model module will check for those first, as askForJson() does. Otherwise, your code adds the answer to the history, and the loop stops. Your own run will word the answer differently.
  • Cap the rounds. A model can keep asking for calls, and every round costs tokens, so the loop stops after a fixed number of rounds, such as five, answer or not. This run needed two. Another run can need more.

The model only asks. Your code runs every call, answers each one by its ID, and calls again until a finished reply has no calls or the cap is reached.

Behind the Scenes: The Raw History

The video’s history used a shape of your own: { role: "assistant", toolCalls } for the model’s requests and { role: "tool", callId, result, isError } for each result. Each provider expects its own shape for the same conversation, and in the next lesson, your model module translates between them. Open the version for your provider to see the history that the second request sent:

This is the input array of the video’s second request, from the real run against OpenAI’s Responses API. The two calls are function_call items, each with a call_id, and each result is a function_call_output item with the same call_id and the function’s result as a string (here, the rows as JSON).

The long strings are cut to ... here. Each call’s arguments also held "date": "2026-12-05", and each result held the full rows, with every sailing’s seatsLeft, vehicleSpacesLeft, and status:

Language: JSON
[
  {
    "role": "user",
    "content": "Is there room for my car on Saturday morning's sailing ..."
  },
  {
    "arguments": "{\"from\": \"Linden Harbor\", \"to\": \"Gull Island\", ...}",
    "call_id": "call_e67c99dc664f4b93bc21d5cf18ba5c7e",
    "id": "fc_05bb74b7b91e6eb0006b0f9bfdad1087d2ba65ba873317cb2f",
    "name": "findSailings",
    "status": "completed",
    "type": "function_call"
  },
  {
    "arguments": "{\"from\": \"Linden Harbor\", \"to\": \"Heron Island\", ...}",
    "call_id": "call_146abc2b8fdb47aea4bbee17b59ec21b",
    "id": "fc_05bb74b7b91e6eb0006b0f9bfe6fb487d2bbc64b6b9cfc9091",
    "name": "findSailings",
    "status": "completed",
    "type": "function_call"
  },
  {
    "type": "function_call_output",
    "call_id": "call_e67c99dc664f4b93bc21d5cf18ba5c7e",
    "output": "[{\"id\":\"LF101\",\"departs\":\"08:00\",...},{\"id\":\"LF102\",...}]"
  },
  {
    "type": "function_call_output",
    "call_id": "call_146abc2b8fdb47aea4bbee17b59ec21b",
    "output": "[{\"id\":\"LF201\",\"departs\":\"09:15\",...}]"
  }
]

The calls and the results are separate items in one flat array, matched only by call_id. In another run, a request that answered only the first of the two calls came back with a 400 error: No tool output found for function call call_....

OpenAI has no error flag. A failed call still gets its function_call_output, with the error message in output. It has no tool stop reason either. A reply is asking for tools when its output holds function_call items.

The runs for this video went to OpenAI only, because there was no Anthropic key for them. This is the shape that Anthropic’s documentation on handling tool calls describes, with the same sailings in it. The IDs are placeholders, since Anthropic generates its own, starting with toolu_. The long strings are cut to ..., as in the OpenAI version:

Language: JSON
[
  {
    "role": "user",
    "content": "Is there room for my car on Saturday morning's sailing ..."
  },
  {
    "role": "assistant",
    "content": [
      {
        "type": "tool_use",
        "id": "toolu_FIRST",
        "name": "findSailings",
        "input": { "from": "Linden Harbor", "to": "Gull Island", "date": "2026-12-05" }
      },
      {
        "type": "tool_use",
        "id": "toolu_SECOND",
        "name": "findSailings",
        "input": { "from": "Linden Harbor", "to": "Heron Island", "date": "2026-12-05" }
      }
    ]
  },
  {
    "role": "user",
    "content": [
      {
        "type": "tool_result",
        "tool_use_id": "toolu_FIRST",
        "content": "[{\"id\":\"LF101\",\"departs\":\"08:00\",...},{\"id\":\"LF102\",...}]"
      },
      {
        "type": "tool_result",
        "tool_use_id": "toolu_SECOND",
        "content": "[{\"id\":\"LF201\",\"departs\":\"09:15\",...}]"
      }
    ]
  }
]

The model’s reply is an assistant message whose content holds one tool_use block per call, sometimes after a short text block, and its stop_reason is "tool_use". Your code answers in the next user message, with one tool_result block per call, all of them together and before any text, each with the tool_use_id of the call it answers.

A failed call still gets its tool_result, with is_error: true. According to the same documentation, a request where a tool_use has no matching tool_result in the next user message is rejected with a 400 error.

With either provider, every call in a reply gets exactly one result, matched by its ID, and the next request carries the whole conversation so far.

00:00 In the last lesson, the model sent back a request to call findSailings, and nothing ran. In this video, you’ll run what it asks for, send the results back, and keep going until the passenger gets an answer.

00:14 Here’s a new question for the booking assistant. A passenger wants room for their car on Saturday morning, from Linden Harbor to Gull Island or Heron Island, and either is fine.

00:24 To answer it, the model needs two sets of sailings.

00:29 The model keeps nothing between calls, so your code keeps the conversation as a history and sends all of it every time. Right now, it holds one entry, the passenger’s question.

00:40 Your code sends the instructions, the history, and the tools, and a reply comes back. This reply holds two tool calls. This video draws them in a shape of your own: each call’s ID, the tool’s name, and its arguments as an object.

00:55 That ID is the one your code sends back, whatever your provider names it. The first call asks for Gull Island and the second for Heron Island. Watch out: a reply can hold several calls, and every one of them needs a result.

01:10 Answer only the first, and the next request fails.

01:15 So your code starts by adding the reply to the history, as an assistant entry that holds both tool calls. The model needs to see its own requests next time.

01:25 Next, your code runs each call. The first call’s arguments go into findSailings. That’s your own function, a short filter over the company’s sailings file.

01:35 You don’t need to write it yet. Two Gull Island sailings come back. The 8 o’clock has no vehicle spaces left, and the 2:30 has six.

01:44 That result joins the history as a tool entry. Its callId is the first call’s ID, so the model can match the result to its request. Its isError flag is false, because the function worked.

01:56 Functions that fail come later in this section, so that’s all you need about this flag for now.

02:02 The second call runs the same way. The 9:15 to Heron Island has three vehicle spaces, and its entry carries the second call’s ID.

02:12 Now the history holds four entries, and your code sends all of them again, with the same instructions and tools. This request carried 393 input tokens, about twice the first request’s 193.

02:25 The calls and their results are part of the input now. This time, the reply has no tool calls. It’s a text answer. The 9:15 to Heron Island has room for the car, and the 8 o’clock to Gull Island has none.

02:40 Your code adds the answer to the history, and the loop stops. Your own run will word the answer differently.

02:48 Here’s the whole loop. Your code sends the instructions, the history, and the tools. If the reply has tool calls, it adds the reply to the history, runs every call, adds each result with its call’s ID, and goes round again.

03:03 If a finished reply has no calls, it’s the answer, and the loop ends. A reply that was cut off or refused has no calls either, so your module will check for those first, as askForJson() does.

03:16 However, a model can keep asking for calls, round after round, and every round costs tokens. Therefore, the loop has a cap, say five rounds, and stops there, answer or not.

03:28 This run needed two.

03:32 So the model only asks, and your code does the rest.

03:37 The text below this video shows the raw history for each provider. In the next lesson, Add askWithTools() to the Model Module, your module translates this history for your provider, and you write this loop against it.

Become a Member to join the conversation.