Skip to content

Grokem · https://www.grokem.com/ai-and-machine-learning/ai-agents/tool-calling/tool-calling-explained-how-an-ai-model-picks-which-function-to-run

Tool CallingExplainerBeginner level

Tool calling explained: how an AI model picks which function to run

Tool calling lets an AI model ask your code to run a function and then use the result in its answer. This beginner explainer shows how the model chooses a tool, who actually runs it, and why the loop works, with a small runnable example.

Krishna Sharma

Technical Writer

Published
  • 6 min read
  • 0 sec

Your progress
  1. Tool calling explained: how an AI model picks which function to run6 min(finished)
    0%

What you will learn

5
  • Explain what tool calling (also called function calling) is in plain words.
  • Describe who decides, who runs the function and who writes the final answer.
  • Follow the request, run, return loop step by step.
  • Tell tool calling apart from a full AI agent.
  • Write clearer tool descriptions so a model can choose well.

An AI model on its own can only produce text. Tool use, also called function calling, is what turns it from a system that can only talk into a system that can act.

This guide explains the idea from first principles. You will see what the model does, what your code does, and how the two take turns. No installation is needed: the one code example is a toy that runs in plain Python and does not call any vendor API.

What is tool calling?

Tool calling is a way to let a model use regular functions from your codebase. The model asks for a function to be run. Your code runs it, gets a response, and feeds that response back to the model.

Function calling means giving the AI the ability to invoke external functions or APIs to perform specific tasks or retrieve information. "Tool calling", "tool use" and "function calling" are used for the same idea in most explanations, so you can treat them as one topic when you search.

The key point: the model does not run anything itself. It writes a request. Your program acts on it.

Why does a model need tools at all?

A model answers from the text it has seen and the text you give it. It cannot look up today's weather, read your database or send an email by itself.

A function can do those things. So you offer the model a small menu of functions, and it can ask for one when the question needs it.

This is the difference between a system that talks and a system that acts.

What does the model actually receive?

Along with the user's message, you send the model a description of each tool you allow. A description normally includes:

  • the tool's name,
  • a plain-language explanation of what it does,
  • the inputs it expects.

The official function-calling guides of OpenAI, Anthropic and Gemini cover schema design, parallel tool calls and strict JSON output. The exact field names differ between providers, so check your provider's own page before you write real code. This article does not list them.

How does the model decide which function to use?

The model compares the user's request with the tool descriptions you supplied. It then picks one of three outcomes:

  1. Answer directly, because no tool is needed.
  2. Ask for one tool, with the inputs filled in from the user's message.
  3. Ask for more than one tool (some providers support parallel tool calls).

So the decision rests on two things you control: the user's wording and your tool descriptions. A tool with a vague name and no explanation is hard to choose. A tool whose description says exactly when to use it is easy to choose.

The internal maths of that choice is not something you configure. Your job is to give the model clear options.

Who runs the function?

You do. Your application receives the model's request, calls the matching function, and keeps the result.

This matters for safety and control. Because your code sits between the model and the function, you can check the inputs, refuse a request, or log what happened before anything runs.

What is the tool call loop?

With tool calling, the model requests one action at a time and waits for the result. The cycle looks like this:

  1. You send the user's message and the tool descriptions.
  2. The model replies with a request to use a tool.
  3. Your code runs the function.
  4. You send the result back to the model.
  5. The model writes the final answer, or asks for another tool.
Your appsends user message+ tool descriptionsAI modelreplies witha tool requestYour functionruns andreturns a result1 message, tools2 tool request5 resultfinal answer3 run function4 result
The model asks, your code runs the function, and the model answers with the result.

If the model asks for another tool at step 5, the loop repeats until it can answer.

What does the loop look like in code?

The example below is a toy. The function toy_model is a stand-in that mimics a model with a simple keyword check, so you can see the shape of the loop. It is not a real model and not any vendor's API.

Read the run function: it sends the question, checks whether a tool was requested, runs the function, then returns the result for the final answer.

python
import json

def get_weather(city):
    return {"city": city, "forecast": "sunny"}

TOOLS = {
    "get_weather": {
        "description": "Look up the weather forecast for a city.",
        "function": get_weather,
    }
}

def toy_model(messages, tools):
    """A stand-in for a real model: it only mimics the decision."""
    last = messages[-1]
    if last["role"] == "tool":
        result = last["content"]
        return {"answer": "The forecast for " + result["city"] + " is " + result["forecast"] + "."}
    text = last["content"]
    if "weather" in text.lower() and "get_weather" in tools:
        city = text.rstrip("?").split(" in ")[-1]
        return {"tool": "get_weather", "arguments": {"city": city}}
    return {"answer": "No tool needed for this question."}

def run(question):
    print("Question:", question)
    messages = [{"role": "user", "content": question}]
    reply = toy_model(messages, TOOLS)
    if "tool" in reply:
        print("Model requests:", json.dumps(reply))
        result = TOOLS[reply["tool"]]["function"](**reply["arguments"])
        messages.append({"role": "tool", "content": result})
        reply = toy_model(messages, TOOLS)
    print("Final answer:", reply["answer"])

run("What is the weather in Paris?")
run("Tell me a joke")

Is tool calling the same as an AI agent?

No. Tool calling is one building block. An agent extends simple function calling by adding memory, reasoning and autonomous tool usage.

Think of it this way:

  • Tool calling: the model asks for one action and waits for the result.
  • Agent: a larger system that uses tool calling repeatedly, remembers earlier steps and plans what to do next.

Learn the single loop first. Agents are built from it.

How do you help the model choose well?

You cannot change how the model reasons, but you can improve what it sees:

  • Give each tool a clear, specific name.
  • Describe in one sentence when the tool should be used.
  • Offer only the tools the task needs. A short menu is easier to choose from.
  • Validate the inputs in your own code before you run the function.
  • Return small, clear results, so the model can use them in its answer.

Pick the provider you will use and read its official function-calling page. That page gives the exact request format, the names of fields and the options for parallel calls and strict output. Those details are not repeated here because they differ between providers and can change.


Glossary

Tool calling
A way for a model to request that a function in your code is run, then use the result. Also called function calling or tool use.
Function tool call
A request from the model to run one of your regular functions, with inputs filled in.
Tool description
The name, explanation and expected inputs of a tool, sent to the model so it can decide when to use it.
Tool call loop
The cycle of request, run, return result, and answer that repeats until the model can reply.
Parallel tool calls
A provider feature where the model asks for several tools at once.
Agent
A system that builds on tool calling by adding memory, reasoning and autonomous tool usage.

Frequently asked questions

Is tool calling the same as function calling?

Yes, in most explanations. Tool use and function calling both mean letting a model invoke functions or APIs to perform tasks or retrieve information.

Does the AI model run the function itself?

No. The model returns a request. Your code runs the function, gets the response and feeds it back to the model.

How does the model know which tools exist?

You send it descriptions of the tools along with the user's message. It chooses based on those descriptions and the request.

Can a model ask for more than one tool at a time?

Some providers support parallel tool calls. Plain tool calling is usually described as one action at a time, with a wait for the result. Check your provider's page.

What is the difference between tool calling and an agent?

Tool calling is the single request and result loop. An agent adds memory, reasoning and autonomous tool usage on top of it.

Where do I find the exact request format?

In the official function-calling documentation of your provider, such as OpenAI, Anthropic or Gemini. The format differs between them.

Next steps

  • Read the official function-calling page of the provider you plan to use for exact field names.
  • Run the toy example and add a second tool to see how the choice changes.
  • Learn how strict JSON output and schema design make tool inputs reliable.
  • Move on to agents once the single loop feels familiar.

From Grokem: https://www.grokem.com/ai-and-machine-learning/ai-agents/tool-calling/tool-calling-explained-how-an-ai-model-picks-which-function-to-run

Finished this article?

How did this strike you?

Be the first to react – it is anonymous.

Community

Questions about this?

Ask in Questions and help and get help from other readers.

Ask a questionBrowse Questions and help

Comments

No comments yet. Share what worked for you, or ask a question.

Keep learning

Picked for this article

Related reading, discussions and learning paths on the same subject.

All in Tool Calling

Learning paths

Learning pathIntermediate

Building and calling APIs

Design an HTTP API people can rely on, read its status codes, choose between REST, GraphQL and gRPC, receive webhooks safely and call a paginated API with retries.

5 lessonsabout 50 minStart

Learning pathIntermediate

SQL for data work

From removing duplicates and window functions to modelling for reporting, cohort analysis, indexes and query plans: the SQL a working analyst or engineer uses every week, each lesson with queries that were run.

8 lessonsabout 90 minStart

Choose what we may use. You can change this at any time with “Privacy choices” at the foot of every page. Details are in the Cookie policy and the Privacy policy.

Strictly necessary

Remembers your theme, the lessons you marked as done and this choice. It stays on your device, tracks nothing and cannot be switched off.

Always on
What exactly is collected, and why

Google Analytics

Collects
The pages you view, your device and browser type, your language, how you reached the site and your approximate location (country and city, worked out from your IP address).
Why
To understand which pages and lessons are used, so we can improve them.
Stored
Two cookies, _ga and _ga_<id>, for up to 2 years. Google acts for us; the data is processed by Google, including outside your country.

Grokem’s own counts

Collects
The pages you open and how long you stay, how far you scroll, where you came from, your device type, browser, screen size, language and time zone. Your IP address is turned into a code that changes every day; it is never stored as it is.
Why
To see how long lessons are read and where readers leave. Not shared with anyone.
Stored
A random visitor ID, a visit ID and the time of your last page view in your browser’s storage (gr_vid, gr_sid, gr_last). No name or email is attached.