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.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

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.