AI Foundations0% complete
0%

Chapter 14 of 2525 chapters remaining

Saved on this device. Mark a chapter complete after reading.

MODULE 3 · LESSON 14 · 11 MIN

Function Calling and Tool Use: Giving AI Hands

By Siten Sanghvi · Published · Updated

In function calling (also called tool use) with tools your own code runs, the model does not touch the outside world. It writes a structured request, your application decides whether and how to carry it out, and the result goes back to the model as more context. The "hands" in the title are your code's.

You describe the tools you offer, the model replies with a request naming a tool and its arguments (the values to pass in), your code runs it or refuses, and the model writes its answer from the result. OpenAI's function calling guide, Anthropic's tool use overview and Google's Gemini function calling docs describe this same division of labor. Details differ by provider and change; vendor facts are dated 2026-11-09.

Who does what in one tool call

Step The model Your application
1. Offer tools Receives the tool list with the request Writes each tool's name, description and parameter schema and sends them
2. Request Replies with text, one or more tool requests (a name plus arguments), or both Receives the request as data; nothing has run yet
3. Check and run Waits Validates the arguments, checks the user may do this, then runs the operation or refuses
4. Return Receives the result or an error as new context Sends it back, tied to the request's ID
5. Answer Writes a reply from what it was given, or requests another tool Shows the reply

In the simplest call the model generates text twice: a request, then an answer from the result. The OpenAI guide lists running the code on the application side as a step, and Gemini's docs say the model does not execute the function. Anthropic's docs add that some tools (web search, code execution) run on Anthropic's servers instead. Then the provider does step 3.

EXHIBIT 14 / TOOLS

Explore the mechanism

The model requests. Code decides.

A valid tool call can still be forbidden.

The model requests. Code decides.ModelYour applicationExternal service01Propose order 1234502Validate + authorize03Read order 1234504Return result #4205Read result as context
External-service callNot called
Request / result ID#42 / #42

Request #42 has not executed

Synthetic session C-17 owns 12345 and 12399; 55555 belongs to C-90. The trusted session supplies customer identity; schema-valid does not mean authorized. Tools your code runs; provider-run tools differ.

Inspect the example data and reasoning
The model requests. Code decides. — current illustrative state
ElementWhat to notice
Model laneProposes a request; never executes the external call
Application laneValidates, authorizes, runs or refuses, and returns a result
Service laneOnly the application can call it

Synthetic order data. The model never calls the external service directly. This is not a universal provider message format.

Read the mechanism without interacting
Request
The model proposes a tool name and arguments.
Boundary
The application validates the shape and authorizes using its trusted session identity.
Result
The application returns a result or error as context, associated with the request ID.

An own-order lookup may run. A schema-valid lookup for someone else must be refused by application authorization.

Try explaining it: What must your code check that a schema alone cannot establish?

A concrete example (illustrative)

A support chat user signed in as customer C-17 asks, "Where is order 12345?" The application offers one tool, get_order_status, described as "Look up shipping status for one order owned by the signed-in customer," with one order_id argument.

  • Request (step 2). The model replies with a request, simplified here: {"tool": "get_order_status", "arguments": {"order_id": "12345"}}. Real formats differ (OpenAI returns a function call item with a call ID, Anthropic a tool_use block).
  • Check and run (step 3). Your code checks the ID's format, looks the order up, confirms it belongs to C-17 (identity from your session, not the model), and only then returns the status.
  • Return (step 4). The result, for example {"status": "shipped", "expected_delivery": "2026-11-14"}, goes back tied to the request's ID.
  • Answer (step 5). The model writes: "Order 12345 has shipped and is expected on November 14."

If the order belonged to another customer, step 3 would refuse and return an error such as "no such order for this customer," and the model would say it could not find it. The model never saw the database, only what your code returned, and it can still misreport that.

What the model is actually doing

The model chooses whether to call a tool, which one, and which values to pass, using the conversation and your descriptions. Anthropic's guide to defining tools calls the description the most important factor in tool performance. The choice is a prediction, not a rule: Anthropic's overview notes a model may guess a value the user never gave.

The idea predates today's APIs: the Toolformer paper (2023) fine-tuned a 6.7-billion-parameter model to insert API calls into text. It is a research system, not any vendor's recipe.

Tool definitions and schemas

A tool definition has a name, a description and a parameter schema: here JSON Schema, a standard way to describe allowed JSON (field names, types, which are required). The field name differs: OpenAI and Gemini use parameters, Anthropic input_schema, MCP (below) inputSchema. Providers support different JSON Schema subsets, so schemas are not portable by default.

Definitions travel with the request, so they occupy the model's context (BT-L12); Anthropic bills them as input tokens, advises fewer, more capable tools because they reduce selection ambiguity, and Gemini's docs advise at most 10 to 20 active tools.

Valid is not the same as safe

A tool request can go wrong in three separate ways:

  • Wrong shape. A field is missing or a number arrives as text. Anthropic's strict tool use page says that without strict mode a model might return "2" instead of 2 or omit required fields. OpenAI documents a strict setting that makes calls adhere to the schema, and Anthropic one that constrains sampling to it (structured output in BT-L09). OpenAI notes unsupported schema features. A reply cut off at the token limit can leave a request incomplete (Anthropic stop reasons).
  • Right shape, wrong value. The model guessed, misread, or was misled. Shape and permission checks pass it; confirming the target with the user, or showing what was returned, can catch it.
  • Right shape, right value, not allowed. A well-formed order ID that belongs to someone else.

Strict mode can help with the first, within those limits; the other two are yours.

Your application's job: the security boundary

Treat the model's request as untrusted input. OWASP's guidance on improper output handling says to treat the model like any other user, with zero trust, and validate what it sends to backend functions, because prompt input, including text planted in content the model reads, can steer it. In practice:

  • Validate. Confirm the tool name is one you offered and the arguments fit your rules; use parameterized queries, not commands built from model text.
  • Authorize as the user, not as the model. Run with the signed-in user's permissions and enforce access in the downstream system. OWASP's excessive agency guidance calls this complete mediation (every request checked, every time, in the system that owns the data) and warns against relying on the model to decide what is allowed.
  • Limit what exists. OWASP names three causes of excessive agency: too much functionality, permission or autonomy. Prefer narrow tools over open-ended ones such as any shell command, and add read-only credentials and scope limits such as one recipient, not a list. This lesson's own worst-case sort (not a standard): read-only lookups risk wrong or over-shared data, side-effect actions such as paying risk irreversible mistakes, open-ended execution (shell, code, browser) risks anything the environment allows. Start read-only, confirm side effects, isolate the rest.
  • Confirm high-impact actions. Require human approval before irreversible steps such as sending, deleting or paying.
  • Return errors as results. A failed or refused call should come back as an error result (Anthropic uses is_error, MCP isError) so the model can retry or tell the user; if a call times out, say its outcome may be unknown.
  • Log every request and outcome, minding what the logs hold (BT-L20).
  • Treat results as untrusted too. A web page or email a tool returns can carry instructions meant for the model. Anthropic's handle tool calls page warns of this indirect prompt injection; BT-L18 covers it.

Separating request from execution gives you a place for these controls; it protects you only if you use it.

Several calls in one turn

A response can hold zero, one or several tool requests. OpenAI's guide says to assume several, and Anthropic's parallel tool use page says Claude may call several by default. Each needs its own result tied to its ID, and how to run them is your decision: concurrent, sequential or mixed. Independent read-only lookups are usually safe to run together; tools with side effects or ordering needs may be better run in turn. Results that lead to more requests start a loop, the subject of the next lesson.

MCP: a shared way to connect tools

The Model Context Protocol (MCP) is an open protocol for connecting LLM applications to tools and data. The specification showed version 2026-07-28 on 2026-11-09. Messages use JSON-RPC 2.0, a standard JSON request format. A host (the LLM application) creates clients, each connected to one server; servers offer tools, resources (data) and prompts. Per the tools page, a client lists tools with tools/list and invokes one with tools/call; each tool has a name, description and inputSchema.

MCP changes the connection, not the boundary. The spec's message flow still has the model select a tool and the client call the server. The spec says tools represent arbitrary code execution, that descriptions of tool behavior (such as annotations) are untrusted unless the server is, that a human should be able to deny calls, that clients should show tool inputs before calling, and that servers must validate inputs; the full conversation stays with the host (architecture page). The protocol cannot enforce these itself. MCP sits beside provider tool-calling APIs; some of those can act as the MCP client for you (Anthropic's MCP connector, in beta on 2026-11-09), which can move step 3 to the provider.

Try it

A paper exercise. For a tool reset_password(user_id), write its description. Then write three requests: a valid one, one for a user the requester does not manage, and one with a user_id the model guessed and the requester never gave. For each, write what your code checks, whether it runs, and the result string it returns. Self-check: the second refuses with an error saying what to do next; the third passes every check if the guess is a user the requester manages, which is why the reset needs a confirmation naming the target.

The takeaway

In function calling the model proposes and your code decides: it validates the request, authorizes it, runs or refuses it, and returns a result or error as context. Strict schema settings can make arguments well-formed, not correct or allowed. Model output is untrusted input, so narrow tools, confirmations and logs make the boundary real. MCP standardizes how hosts find and call tools, not whether they are safe.

Up next

Agents and Multi-Step Workflows: what happens when this cycle repeats, with step limits, failure modes and human checkpoints.

VISUAL FIELDNOTES / 14

Keep the big picture.

A one-page visual companion. Save it, print it, or explain it to someone else.

A valid tool call can still be forbidden. Synthetic order data. The model never calls the external service directly. This is not a universal provider message format.View the full-size sheet ↗

The model requests. Code decides.

A valid tool call can still be forbidden.

One US Letter page · Vector artwork · Fits A4 when printed to fit

Read the key points as text
Request
The model proposes a tool name and arguments.
Boundary
The application validates the shape and authorizes using its trusted session identity.
Result
The application returns a result or error as context, associated with the request ID.

Remember: The model proposes a tool name and arguments.

Try it: An own-order lookup may run. A schema-valid lookup for someone else must be refused by application authorization.

Recall: What must your code check that a schema alone cannot establish?

OPTIONAL CHECK

Check your understanding.

The optional check requires JavaScript. You can continue reading the lesson.

YOUR LEARNING

Keep your place.

Your progress is saved on this device.