Lesson 18 of 36 · Track 3: Build with AI

What is tool calling (function calling) in AI?

Beginner9 min5 steps1 videoFree · no login
The short answer

Tool calling, also called function calling, lets an AI model ask your code to run a function, like get_weather or book_slot. The model never runs your code itself: it replies with the function’s name and inputs, your app runs it, and you send the result back so the model can finish its answer. This is how chatbots check live data and take actions.

  1. 1

    Why does a model need tools at all?

    A language model only knows what was in its training data, and it can only produce text. On its own it cannot check today’s weather in Chennai, your PNR status, or your calendar.

    Tool calling fixes this. You give the model a short list of tools it may use. When a question needs one, the model writes a neat, structured request: "please run get_weather with city = Chennai". Your code does the real work and hands back the result.

    Think of a manager on a phone call who cannot leave the desk. She tells her assistant, "check how many boxes are in godown 2". The assistant checks and reports back, and she uses that number in her reply.

    Key idea

    The model chooses the tool and fills in the inputs. Your code does the actual work.

  2. 2

    How do you describe a tool to the model?

    Each tool has three parts: a name, a plain-English description of when to use it, and a schema listing its inputs. The schema uses JSON Schema, a standard way to say "this input is text, this one is a number, this one is required".

    The model reads the name and description to decide when to call the tool, so write them as if for a new intern. OpenAI’s guide even suggests an "intern test": could a person use this function correctly with only what you gave the model?

    • Use clear names and detailed descriptions.
    • Keep the list short. OpenAI suggests fewer than 20 tools at the start of a turn.
    • Use fixed choices (enums) where you can, like "celsius" or "fahrenheit".
    • Don’t make the model fill in things your code already knows, like the user’s ID.
    A tool definition (Anthropic format)
    {
      "name": "get_weather",
      "description": "Get the current weather for a city.",
      "input_schema": {
        "type": "object",
        "properties": {
          "location": {
            "type": "string",
            "description": "City and country, e.g. Pune, India"
          }
        },
        "required": ["location"]
      }
    }
  3. 3

    What happens, step by step?

    OpenAI’s docs describe five steps, and Anthropic and Google work the same way. You send the question plus the tool list. The model replies with a tool call instead of an answer. Your code runs the function. You send the result back in a second request. The model then writes the final answer, or asks for another tool.

    The model can also decide that no tool is needed. Ask "what is 2 + 2?" and it will just answer.

    1. User asks a question
    2. Model picks a tool and inputs
    3. Your code runs the function
    4. Result goes back to the model
    5. Model writes the answer
  4. 4

    What does a tool call look like?

    With Claude, the model’s reply contains a "tool_use" block with an id, the tool name and the inputs. Your code runs the function and replies with a "tool_result" that carries the same id, so the model knows which call the result belongs to.

    OpenAI calls the same thing a "function_call" with a "call_id" and "arguments" (a JSON string your code must parse). Gemini calls it a function call too. Different field names, same idea.

    Model asks, your app answers
    // Model to your app
    {
      "type": "tool_use",
      "id": "toolu_01A09q90qw90lq917835lq9",
      "name": "get_weather",
      "input": { "location": "Pune, India" }
    }
    
    // Your app to model
    {
      "type": "tool_result",
      "tool_use_id": "toolu_01A09q90qw90lq917835lq9",
      "content": "31°C, clear sky"
    }
  5. 5

    What can go wrong, and how do you stay safe?

    Models sometimes pick the wrong tool or fill in a wrong value, like the wrong city or an amount with an extra zero. Your code is the last line of defence, so check every input before acting on it.

    Some tools only read (get_weather). Others change things in the world (send_email, make_payment, delete_file). Treat those with extra care.

    • Validate inputs in code: right type, sensible range, user allowed to do this.
    • Ask the user to confirm before anything costly or hard to undo.
    • Give each tool the least access it needs, such as read-only database access.
    • Log every tool call so you can see what happened later.
    Key idea

    Tool calling turns a chatbot into something that can act. Next you will see MCP, a standard way to share tools across apps, and agents, which call tools in a loop.

From the people who built it

Straight from the source

The original idea, then the same idea taken apart, then how you use it today.

Timo Schick, Jane Dwivedi-Yu and colleagues, mostly at Meta AI Research
Authors of Toolformer, one of the first papers to show a language model learning by itself when to call outside tools.
Toolformer: Language Models Can Teach Themselves to Use Tools (paper, 2023)
1 · Understand
The paper starts from a puzzle: big language models can learn new tasks from a few examples, yet they slip on simple things like arithmetic or looking up a fact, where much smaller tools do well. Toolformer trains a model to decide which API to call, when to call it and what inputs to pass, using only a handful of examples per tool.
2 · Break it down
  • The six tools were a calculator, a question-answering system, two search engines, a translation system and a calendar.
  • The model tried tool calls on ordinary text. A call was kept for training only if its result made the following words easier to predict.
  • With tools, it often did as well as much larger models on the tested tasks, without getting worse at ordinary writing.
3 · Use it
Give the model tools for the jobs it is weak at: exact maths, today’s date, live facts and your own data. Let the model write the text, and let your tools supply the facts.
Atty Eleti, Jeff Harris and Logan Kilpatrick (OpenAI)
Authors of the June 2023 post that launched function calling in the OpenAI API, the pattern most AI APIs now follow.
Function calling and other API updates (blog post, June 2023)
1 · Understand
Developers describe functions to the model, and the model can choose to reply with a JSON object holding the inputs for one of them. The post says this lets developers "more reliably get structured data back from the model."
2 · Break it down
  • Functions were described with JSON Schema, the same format you saw in this lesson’s tool definition.
  • Examples in the post: turn a request to email someone into a send_email call, a weather question into get_current_weather, or a question about orders into a database query.
  • It also warned that text coming back from a tool can try to steer the model. It advised using only trusted tools and asking the user to confirm actions like sending email or buying something.
3 · Use it
Use tool calling even when you just need clean structured data, like pulling names and dates out of a message. And copy the 2023 safety advice: confirm with the user before any tool that acts in the real world.
5 terms

Words to know

Tool calling (function calling)
Two names for the same thing: the model asks your app to run a named function with specific inputs.
Tool definition
The name, description and input schema you send so the model knows a tool exists and how to use it.
JSON Schema
A standard way to describe the shape of JSON data: which fields exist, their types, and which are required.
Tool result
The output of the function, which your app sends back to the model so it can continue.
Client tool vs server tool
A client tool runs in your app. A server tool, such as Anthropic’s web search, runs on the provider’s own servers.
1 video · plays here

Watch

Hand-picked from the people who explain this best. The video loads only when you press play.

What is Tool Calling? Connecting LLMs to Your Data
IBM Technology · 5 min

A whiteboard walk-through of the request, tool call and result loop. Focus on who runs the tool at each step.

Hands-on · 10 minutes or less

Try it

Play the part of the code in any chatbot

  1. 1Open ChatGPT, Claude or Gemini and start a new chat.
  2. 2Paste this: "You can use one tool: get_weather(city). When you need it, reply ONLY with JSON like {"tool": "get_weather", "city": "..."} and wait. I will reply with the result. Do not guess the weather."
  3. 3Ask: "Should I carry an umbrella in Mumbai today?" The model should reply with only the JSON tool call. That is a tool call.
  4. 4Now act as your code. Reply: "RESULT: 27°C, heavy rain expected after 4 pm". Watch the model use your result to answer.
  5. 5Ask "What is the capital of France?" and notice that it answers directly without calling the tool. Then read the official guide below to see the real version.
OpenAI function calling guide
3 questions · instant feedback

Check yourself

Pick an answer to see if you are right, and why.

Question 1

Question 1 of 3

A user asks for the weather and the model returns a get_weather tool call. Who actually runs get_weather?

Question 2

Question 2 of 3

Why should a tool have a clear, detailed description?

Question 3

Question 3 of 3

Your code has run the function and has a result. What happens next?

Lesson 18 of 36

Done? Keep going

Mark this lesson complete to keep your place. Progress is saved on this device, no login.

Up next · Lesson 19 · 10 minEmbeddings and RAGRAG (retrieval-augmented generation) means: first search your own documents for the passages that answer a question, then give those passages to the model so it answers from them. The search runs on embeddings, lists of numbers that capture meaning, so "refund" can match "money back". It turns a closed-book exam into an open-book one.← Previous: What is an API?
People also ask

Questions people ask

What is the difference between tool calling and function calling?

There is no real difference. OpenAI’s docs say function calling is "also known as tool calling", and Anthropic’s docs call it tool use. All of these mean the model asks your code to run a named function with specific inputs.

Does the LLM execute the function itself?

No. For tools you define, the model only returns the function name and inputs. Your application runs the code and sends the result back. Some providers also offer server tools, like web search, that they run on their own servers.

What is the difference between tool calling and MCP?

Tool calling is how a model asks for a function to be run. MCP (Model Context Protocol) is a standard way to package tools and data so any AI app that supports MCP can discover and use them. MCP tools still reach the model through tool calling.

6 sources · checked Nov 2026

Sources and further reading

Free · No signup to start

Going for an AI role? Practise the interview.

Talk to an AI interviewer about what you just learned and get feedback on every answer.

Start a free mock interview