Learn Hub
AI AgentsFoundations

Tools and function calling

A tool is an action your code offers a model. The model asks for it by name with inputs, your code runs it, and the result goes back.

basics 7 min read Verified 20 Oct 2026

In 3 lines

  • What it is: Function calling, also called tool calling, lets a language model ask your code to run an action, such as "check the weather."
  • Why it matters: A model on its own can only write text. Tools are how an agent reads live data and changes things in the world.
  • Example: You ask, "Is there a room free on Friday?" The model asks your code to run check_rooms with the date, and then answers from the real result.

Think of it like…

Real-world example

A small hotel adds a chat assistant to its website. Guests ask things like, "Do you have a double room free this Friday?"

A language model, the AI that reads and writes text, can't know which rooms are free on Friday. That data lives in the hotel's booking system. So the hotel's developer gives the model one tool, called check_rooms. The tool takes a date and a room type.

When a guest asks, the model writes a tool call: "run check_rooms with date 2026-11-06 and type double." The hotel's code checks the booking system and finds two free rooms. It sends that result back to the model. The model then replies, "Yes, we have two double rooms free on Friday." The model never touched the booking system itself.

How it works

A few words first. A provider is a company that offers a model, like OpenAI or Anthropic. JSON is a simple text format for data, made of names and values in curly braces. A schema is a description of what valid data must look like. JSON Schema is a standard way to write that description.

The flow has five steps:

  1. Describe the tools. Your code sends the model a list of tools, along with the user's message. Each tool has a name, a plain description, and a schema for its inputs.
  2. The model asks. If a tool would help, the model replies with a tool call. The call is the tool's name plus the inputs it chose.
  3. Your code runs it. For tools you define yourself, the model does not run anything. Your code reads the call and runs the real function. (Some providers also offer built-in tools, like web search, that run on their own servers.)
  4. Send the result back. Your code adds the result to the conversation and calls the model again.
  5. The model answers, or asks for another tool. Repeating steps 2 to 5 until the job is done is called the agent loop.

Here is a tool definition in Claude's style. OpenAI's version looks almost the same, but names the schema field parameters.

1. The tool definition your code sends
{
  "name": "check_rooms",
  "description": "Check which hotel rooms are free on one night. Use it when a guest asks about availability. It does not make a booking. Returns free room numbers.",
  "input_schema": {
    "type": "object",
    "properties": {
      "date": { "type": "string", "description": "Night of stay, as year-month-day, e.g. 2026-11-06" },
      "room_type": { "type": "string", "enum": ["single", "double", "suite"] }
    },
    "required": ["date"]
  }
}

In the schema, type says what kind of value is allowed. An object is a group of named values, and a string is text. properties lists the inputs, and required says which ones the model must always fill in. The enum list tells the model the only allowed room types. Your code should still check it.

The next two examples are simplified. Each provider wraps them in its own extra fields.

2. The tool call the model sends back (simplified)
{ "id": "call_1", "name": "check_rooms", "input": { "date": "2026-11-06", "room_type": "double" } }
3. The result your code returns (simplified)
{ "call_id": "call_1", "output": { "free_rooms": ["204", "311"] } }

Two big providers use different names for the same ideas:

IdeaOpenAIAnthropic (Claude)
Input schema fieldparametersinput_schema
Model asks for a toola function_call itema tool_use block
You return the resulta function_call_output itema tool_result block

Notice the ID in examples 2 and 3. A model can ask for several tools in one reply. OpenAI's guide shows each call with a call_id, and your result quotes it. That way the model knows which result answers which call.

Common mistakes

  • Short, vague descriptions — the model picks tools mostly by reading their names and descriptions. Anthropic's docs call the description "by far the most important factor" and suggest at least 3–4 sentences. Say what the tool does, when to use it, and what it returns.
  • Thinking the model runs the code — it only writes a request. Your code must run the tool, check the inputs, and handle errors.
  • Trusting the inputs blindly — the model can pass a wrong date or a made-up ID. Check every input before you act, just as you would with a web form.
  • Too many tools at once — a long menu makes the wrong pick more likely. OpenAI's guide gives a soft target of fewer than 20 tools per turn, meaning per request to the model.
  • Asking the model for data you already have — if your code knows the guest's ID, fill it in yourself. Don't make it a tool input the model could get wrong.

Remember this

Quiz

Question 1

Q1.In function calling, who actually runs the function?

Question 2

Q2.What does the 'required' list in a tool's input schema do?

Question 3

Q3.Your model keeps picking the wrong tool. What should you try first?

Question 4

Q4.Why does each tool call carry an ID?

Go deeper

Done reading?

Mark it learned and the site will remind you to review it before you forget. Review page

Sources

Facts last checked against these on 20 Oct 2026.

On this page