StudyVisuallyStart studying
Part 03 · Tool calling · Card 7 of 10

Calling the model again

Tool calling takes two model calls: one where it asks for the tool, and another (with the result added) where it writes the answer.

Last updated

Analogy

A round trip like OAuth: redirect out, come back with the goods, then finish the request.

Code example

round-trip.tsTypeScript
// 1st call -> tool_calls; you run the tool and push the result
const followUp = await client.chat.completions.create({
model: "gpt-4o-mini",
messages, // now includes the tool message
tools,
});
console.log(followUp.choices[0].message.content);
// "Order A12 shipped yesterday and arrives Friday."

In an agent

Wrap this round trip in a loop and you have an agent.

Practice

Interview questions

  • How does function / tool calling work?
    • Describe each tool with a name, a description and a JSON schema for its parameters.
    • The model returns which tool to call and with what arguments. It never executes code itself.
    • Your code validates the arguments, runs the function, and sends the result back as a tool message.
    • The model then continues with the result in context.

Questions people ask

Why does tool calling need two model calls?

In the first call the model decides it needs a tool and asks for it. Your code runs the tool. In the second call, with the result added to the messages, the model writes the actual answer. If the second reply asks for another tool, you repeat; that repetition is the agent loop.

How do I turn the tool calling round trip into an agent?

Put it in a loop. Call the model with tools; if it asks for tools, run them, append the results and call again; if it answers in text, return that answer. Add a maximum step count so a confused model cannot loop forever. That small loop is the core of every agent.