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.

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."

🙋User asksweather inMumbai?🧠Model decidescall get_weather🛠️Your code runsitcalls weather API📦Result returns28°C, cloudy💬Final answer"28°C and cloudy"
The model asks for the tool; your code runs it; the model writes the answer.

The five steps

  1. User asks a question.
  2. Model decides a tool is needed and prepares the arguments.
  3. Your code runs the tool with those arguments.
  4. The result goes back to the model.
  5. 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 definitionsSafe tool execution
Clear, short names and descriptionsCheck and clean every argument before using it
Precise input types; use fixed lists (enums) where you canHandle errors and time-outs gracefully
Keep the input list as small as possibleReturn short, structured results (JSON)
Give an example in the description when it helpsLog 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 min

Write a tool for an AI

  1. Pick a simple tool, e.g. convert_currency or book_meeting_room.
  2. Write its name, a one-line description and its inputs (with types), like the JSON in this lesson.
  3. 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.

Build it for real · guided projectCode an AI research assistant that uses tools (a ReAct agent)About 90 min · Copy-paste code · step by step
Test what you learned

Take a quick quiz, or look up any word in the glossary.