Skip to solution
mediumAI Engineering

How does function calling (tool use) work under the hood?

127 views
01

Understand the problem

Schemas in, structured intent out: the request-response loop that lets models act.

function-callingtool-useagents
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

You send the model tool definitions (name, description, JSON-Schema parameters) with the conversation. When the model decides a tool helps, it does not execute anything — it returns a structured tool-call message (name + arguments). Your code executes the function, appends the result as a tool-result message, and calls

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

The canonical tool loop
let messages: Msg[] = [{ role: "user", content: task }];

for (let step = 0; step < MAX_STEPS; step++) {
  const res = await client.messages.create({ model, tools, messages, max_tokens: 1024 });
  if (res.stop_reason !== "tool_use") return textOf(res);       // done

  const calls = res.content.filter((b) => b.type === "tool_use");
  const results = await Promise.all(calls.map(async (c) => ({
    type: "tool_result", tool_use_id: c.id,
    content: await execValidated(c.name, c.input),               // schema-check first
  })));
  messages.push({ role: "assistant", content: res.content },
                 { role: "user", content: results });
}
throw new Error("step budget exhausted");
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 55 of 80 decoded in the AI Engineering track. One more won't hurt.

Back to track