Function calling step by step: how an AI uses a tool
Function calling (also called tool use) lets an AI ask your software to do something it can't do alone: check the weather, search a database, book a meeting. The AI never runs the tool itself. It decides when a tool is needed, which one, and what inputs to send; your code runs it and hands back the result.
Watch the short video: How AI Uses Tools for Live Weather (opens on YouTube)
Everyday analogy
LLM = brain, tools = hands. A manager (the AI) fills in a request slip, "Weather check: city Mumbai, units Celsius", and passes it to an assistant (your code), who makes the call and brings the answer back.
Example
You ask, "Is it hot in Mumbai today?" The model replies with a tool call, not text: get_weather(location="Mumbai", unit="celsius"). Your app calls a weather service, which returns 28°C, cloudy. The model then writes: "It's 28°C and cloudy in Mumbai right now."
The five steps
- User asks a question.
- Model decides a tool is needed and prepares the arguments.
- Your code runs the tool with those arguments.
- The result goes back to the model.
- Model writes the final answer using the result.
1. You describe the tool (JSON schema)
{
"name": "get_weather",
"description": "Get current weather for a city",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"},
"unit": {"enum": ["celsius", "fahrenheit"]}
},
"required": ["location"]
}
}2. The model answers with a call
{
"name": "get_weather",
"arguments": {
"location": "Mumbai",
"unit": "celsius"
}
}Your code runs it and returns {"temp": 28, "condition": "Cloudy"}.
| Good tool definitions | Safe tool execution |
|---|---|
| Clear, short names and descriptions | Check and clean every argument before using it |
| Precise input types; use fixed lists (enums) where you can | Handle errors and time-outs gracefully |
| Keep the input list as small as possible | Return short, structured results (JSON) |
| Give an example in the description when it helps | Log every call; ask a human before risky actions |
Multi-step tool use is just this loop repeated: call a tool, look at the result, decide the next tool, until the task is done. A trip planner might call search_flights, then search_hotels, then get_weather, and combine all three. That loop is what turns a chatbot into an agent. All major model providers (Anthropic, OpenAI, Google, Meta, Mistral) support it.
Try it yourself
About 10 minWrite a tool for an AI
- Pick a simple tool, e.g. convert_currency or book_meeting_room.
- Write its name, a one-line description and its inputs (with types), like the JSON in this lesson.
- Ask an AI chatbot: Pretend you can call this tool. For the request 'convert 500 dirhams to rupees', show the tool call you would make.
What you should see: The AI produces a neat, structured request, which is exactly what your code would run.
Use any free AI chatbot (ChatGPT, Claude, Gemini or Copilot). Never paste private or confidential information.