Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 39 ยท Tools and Function Calling

What Are Tools? Function Calling Basics

Learn how tools give a language model hands: the function-calling flow, tool definitions, tool-use messages and results, and a runnable calling loop in Python.

A language model on its own can only produce text. A tool is a function your program exposes to the model so that it can ask for something to be done: look up an order, read a file, run a calculation, call a web API. The model never runs the function. It emits a structured tool call ("call get_weather with city = Lima"), your program runs the real function, and the result goes back to the model as a message. This protocol is called function calling or tool use, and it is the mechanism behind nearly every practical agent.

The function-calling flow

sequenceDiagram
  participant U as Your program
  participant M as Model
  participant F as Your function
  U->>M: messages + tool definitions
  M-->>U: tool call: get_weather(city="Lima")
  U->>F: run get_weather("Lima")
  F-->>U: "18C, cloudy"
  U->>M: messages + tool result
  M-->>U: final answer text

The model asks; your code acts; the result returns as a message.

The conversation now contains more than user and assistant text. Typically it holds:

  1. A user message with the question.
  2. An assistant message containing a tool call: a tool name, an ID and arguments.
  3. A tool result message tied to that ID, containing the output (or an error).
  4. A final assistant text message that uses the result.

The ID matters: a model can request several calls at once, and results must be matched to requests.

A tool is a definition plus a function

For each tool you provide the model three things: a name, a description (when to use it) and a parameter schema (what arguments it takes). Your program separately holds the actual Python function. The description is effectively a mini prompt, so write it carefully; the next lesson is entirely about schemas.

Python
# Read-only: a typical tool definition as sent to a provider (field names vary slightly)
get_weather_tool = {
    "name": "get_weather",
    "description": "Get the current temperature and sky conditions for a city.",
    "input_schema": {
        "type": "object",
        "properties": {"city": {"type": "string", "description": "City name, e.g. Lima"}},
        "required": ["city"],
    },
}

Building a tool-calling loop

The program below defines two tools and a stand-in fake_model that returns tool calls in the same shape that real APIs do: a list of blocks that are either text or tool uses. The loop is the same as in your first agent loop, but now actions are structured objects with IDs.

Python Stand-in: a structured tool-calling loop

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.

The structure is: no tool calls means final answer. That single test replaces the "type": "final" convention you used earlier, and it is how real SDK loops decide to stop.

Parallel tool calls

A model may request several independent calls in one reply, for example weather for three cities. Your loop executes all of them, returns one result per ID, and lets the model continue. Running them concurrently saves time when they do not depend on each other.

Python Handle several tool calls in one turn

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.

(In the browser Python runner threads may run one after another; the code shows the pattern, and the matching by ID is the part that matters.)

What makes a good tool?

  • Does one clear thing. search_orders(customer_id) beats do_everything(command).
  • Has a plain-language description that says when to use it and when not to.
  • Returns concise, readable text or small JSON. Not 10,000 rows.
  • Fails with helpful messages. "No customer with id 99; ids look like C-1234" lets the model fix itself.
  • Is safe by default. Read-only tools are easy to allow; tools that change things need extra care (see executing tool calls safely).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Who actually executes a tool in the function-calling protocol?

    The model only emits a structured request. Your code runs the real function and returns the result.
  2. 2. Why does each tool call have an ID?

    The tool result message carries the same ID so the model knows which call it answers.
  3. 3. How does the loop usually decide the model has finished?

    When the model returns only text, that text is the final answer.
  4. 4. Which tool design is best?

    Narrow tools with clear names and compact results are easier for the model to choose and use.

Summary

  • A tool is a function your program exposes; the model requests calls, your code runs them.
  • A call has a name, an ID and arguments; the result message references the ID.
  • No tool calls in a reply means the final answer.
  • Independent calls can run in parallel; match results by ID.
  • Good tools are narrow, well described, concise and safe by default.

Next: Tool Schemas with JSON Schema