Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 38 ยท Structured Output and Tool Use

Function Calling and Tool Use

Learn how tool use works: describing functions to a model, how it requests calls, writing good tool descriptions and dispatching calls safely in Python.

A language model can only produce text. Function calling (also called tool use) is the mechanism that lets that text trigger real actions: checking the weather, querying a database, creating a ticket, doing exact math. The model never runs anything itself. It asks your program to run a function, your program runs it, and the result goes back to the model.

How it works

  1. You describe the available tools to the model: name, purpose, and parameters (usually as a JSON Schema).
  2. The user asks something. The model decides either to answer directly or to request a tool call: a structured message naming the tool and giving arguments.
  3. Your code validates and executes the call.
  4. You send the result back as a new message.
  5. The model uses the result to write the final answer (or to request another call).
sequenceDiagram
  participant U as User
  participant A as Your app
  participant M as Model
  participant T as Tool (your code)
  U->>A: "Is it raining in Oslo?"
  A->>M: message + tool descriptions
  M-->>A: tool call: get_weather(city="Oslo")
  A->>T: run get_weather("Oslo")
  T-->>A: {"condition": "rain", "temp_c": 6}
  A->>M: tool result
  M-->>A: "Yes, it is raining in Oslo, 6 degrees."
  A-->>U: final answer

The model asks, your code acts. The model never touches your systems directly.

Providers use different field names for these messages, but the shape is the same. Check your provider's documentation for the exact format.

Describing a tool

A tool description is a prompt. The model reads it to decide when to call the tool and how. Clarity here matters as much as anywhere else.

JSON
{
  "name": "get_weather",
  "description": "Get the current weather for a city. Use when the user asks about current conditions. Not for forecasts.",
  "parameters": {
    "type": "object",
    "properties": {
      "city": {"type": "string", "description": "City name, e.g. 'Oslo'"},
      "units": {"type": "string", "enum": ["celsius", "fahrenheit"]}
    },
    "required": ["city"]
  }
}

Tips for good tool descriptions:

  • Say when to use it and when not to. "Not for forecasts" prevents wrong calls.
  • Describe each parameter, with an example value and the format.
  • Use enums for fixed choices.
  • Keep names verb-like and specific (search_orders, not data).
  • Few, distinct tools beat many overlapping ones; overlapping tools confuse the choice.
  • Return clear errors the model can act on ("city not found; ask the user to check the spelling").

A tool registry in Python

The Python side is ordinary code: a table of functions and their descriptions, plus a dispatcher that validates arguments before running anything. The model's request below is hand-written to show the shape; no model is called.

Python Describe tools and dispatch a call

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Three things to notice. The dispatcher never trusts the arguments: it checks for unknown tools, missing fields, wrong types and unexpected keys. Errors come back as structured results the model can read and react to. And only functions in TOOLS can run; the model cannot invent a call to something you did not expose.

Why tools beat "just ask the model"

  • Exactness: arithmetic, dates, sorting and counting done in code are exact.
  • Fresh data: the model's knowledge has a cutoff; tools fetch current information.
  • Private data: tools can query your database under your access rules.
  • Actions: send an email, create a ticket, book a slot.

Safety rules for tool use

A model that can call tools can be steered, by a user or by text in a document it reads, into calling them badly. Treat tool calls like requests from an untrusted client.

  • Validate arguments (types, ranges, allowed values) before executing.
  • Authorize in code: the tool runs with the user's permissions, not blanket access.
  • Least privilege: expose read-only tools unless writes are needed.
  • Confirm risky actions with the human (deleting, paying, emailing).
  • Rate-limit and log every call.
  • Never pass raw model output to a shell or an SQL string. Use parameterized queries and allow-lists.
Python Gate risky tools behind confirmation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Parallel and forced calls

Many APIs let a model request several calls at once (independent lookups) and let you force or forbid tool use ("always call this tool", "never call tools"). Forcing a tool is a handy way to get guaranteed structured output: define a tool whose parameters are your schema, force it, and read the arguments as your JSON.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Who actually executes a tool call?

    The model only requests the call; your code validates and runs it.
  2. 2. What makes a good tool description?

    The description is a prompt the model uses to decide when and how to call the tool.
  3. 3. Why validate tool arguments before running them?

    Treat tool calls like requests from an untrusted client.
  4. 4. Which action should usually need human confirmation?

    Destructive or irreversible actions should be confirmed.

Summary

  • Tool use lets text-only models trigger real actions: the model requests a call, your code validates and runs it, and returns the result.
  • Tool descriptions are prompts: say when to use the tool, describe parameters, use enums and keep tools few and distinct.
  • Validate every argument, authorize in code, use least privilege, confirm risky actions and log everything.
  • Forcing a tool whose parameters are your schema is a way to get structured output.

Next: The Tool-Calling Loop