Monday, June 22, 2026
banner
Top Selling Multipurpose WP Theme

In my newest put up, I defined learn how to use JSON mode, operate calls, and structured output to get structured, machine-readable output as a response from LLM. In that put up, I briefly touched on the thought of ​​operate calls and approached them as a approach to get hold of structured responses. However, operate calls are basically way more than retrieving structured information from a mannequin. It’s because operate calls are basically the spine. Agent AI workflow. So, in at present’s put up, we are going to take a more in-depth have a look at this very matter.

In all of the examples mentioned to date, the LLM is solely used as a passive responder. That’s, it solely receives questions and generates solutions. However don’t simply give one thing again to LLM. do one thing?Extra exactly, what if you wish to set off an motion based mostly on the mannequin’s response? This motion could possibly be something from looking out reside information, sending a message, querying a database, or calling an exterior API.

that is, instrument name. Device calls remodel LLM from an excellent sensible textual content generator to one thing that may truly set off actions and work together with the world round you.

So let’s have a look!


What’s a instrument name?

tool call (additionally referred to as) function call) is a mechanism by which LLM can request the execution of exterior capabilities or APIs as a part of response technology. Which means fairly than merely returning textual content, your mannequin can execute a selected operate with particular arguments in response to a consumer’s request.

The essential factor to know right here is that The mannequin itself doesn’t run the instrument. That is all determine which instrument to name with which arguments. The precise execution of your chosen instrument occurs inside your individual code, which comprises the requests to your AI mannequin. The instrument’s outcomes are then fed again to the AI ​​mannequin, which makes use of them to generate the ultimate response to the consumer.

This can be a instrument name loop and contains the next steps:

  • Consumer sends message
  • AI fashions take messages as enter and produce output. This mainly determines which instrument and which arguments to make use of.
  • The mannequin response is returned to your code, together with the instrument choice and every argument used. This code runs the chosen instrument utilizing the chosen arguments with out involving the AI ​​mannequin. This execution produces some outcome (comparable to a calculation or data retrieved from an API) that’s handed again to the AI ​​mannequin.
  • The AI ​​mannequin takes the outcomes of the instrument as enter and generates the ultimate response to the consumer based mostly on it.

Once more, the mannequin generates instrument calls, not instrument executions. The 2 are utterly completely different, and mixing them up is likely one of the commonest sources of confusion.

However what precisely is a instrument name? In apply, because of this the mannequin makes use of operate calls to return a structured, machine-readable response, as we noticed in a earlier put up. The content material of this response is None;There aren’t any pure language solutions, simply structured directions that inform you which instrument to name with which arguments. The mannequin generates an precise textual content response to the consumer solely after working the instrument and returning the outcomes.

However let’s examine this in motion!


We’ll begin with a easy instance utilizing only one instrument and one name, after which regularly construct up extra fascinating situations.

1. Single instrument: Climate API

I believe the commonest instance of an AI-powered instrument that involves thoughts is the Climate API (customized reside information foundation). So lets say we’re constructing a climate assistant. Particularly, I need to create a mechanism for customers to ask concerning the climate. Along with letting the AI ​​mannequin make issues (which the mannequin will fortunately do 🙃), we wish the AI ​​mannequin to have the ability to name actual climate capabilities and get actual information concerning the climate from someplace else outdoors of LLM. To get climate information, use open meteora free, open supply climate API that fortunately doesn’t require an API key.

To make use of a instrument, you could first declare it. instruments.

from openai import OpenAI
import json

shopper = OpenAI(api_key="your_api_key")

# Step 1: outline the instrument
instruments = [
    {
        "type": "function",
        "function": {
            "name": "get_current_weather",
            "description": "Get the current weather for a given city",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {
                        "type": "string",
                        "description": "The name of the city, e.g. Athens"
                    },
                    "unit": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"],
                        "description": "The temperature unit to make use of"
                    }
                },
                "required": ["city"]
            }
        }
    }
]

Discover that up thus far, the precise instrument used (climate API) just isn’t talked about anyplace. As a substitute, the mannequin decides which instrument to name based mostly on three issues: the operate description (“Get the present climate for a selected metropolis”), parameter description (“Title of a metropolis, e.g. Athens”), and the enforced schema. Primarily based purely on this data, the mannequin determines whether or not that is the suitable instrument to invoke a selected consumer message, and with what arguments. Subsequently, writing a transparent and exact description when defining a instrument is vital for the mannequin to correctly establish and invoke the suitable instrument based mostly on the consumer’s enter.

So, after defining your instrument variables, you may make requests to your AI mannequin.

# Step 2: ship the consumer message together with the instrument definition
messages = [
    {"role": "user", "content": "What's the weather like in Athens right now?"}
]

response = shopper.chat.completions.create(
    mannequin="gpt-4o-mini",
    instruments=instruments,
    messages=messages
)

print(response.selections[0].message)

Here is what occurs whenever you make this request: The mannequin reads the consumer’s messages and “What is the climate like in Athens now?”we perceive that there are instruments out there. get_current_weather will make it easier to reply this question utilizing actual reside information. Subsequently, as an alternative of producing the textual content response instantly, we determine to name the instrument first. Extra particularly, the mannequin’s response at this level is:

ChatCompletionMessage(
    content material=None,
    position='assistant',
    tool_calls=[
        ChatCompletionMessageToolCall(
            id='call_abc123',
            type='function',
            function=Function(
                name='get_current_weather',
                arguments='{"city": "Athens", "unit": "celsius"}'
            )
        )
    ]
)

Discover how the content material appears NoneFor the reason that mannequin just isn’t returning a textual content response, However instrument name. Now our job is to truly run the instrument and the chosen mannequin and return the outcomes to the instrument. In our case, we make an API request to the Climate API utilizing the arguments supplied within the AI ​​mannequin’s response (i.e. metropolis and unit of measurement).

# Step 3: execute the instrument utilizing the Open-Meteo API
import requests

def get_current_weather(metropolis: str, unit: str = "celsius"):
    # geocode town identify to coordinates
    geo = requests.get(
        "https://geocoding-api.open-meteo.com/v1/search",
        params={"identify": metropolis, "depend": 1}
    ).json()
    lat = geo["results"][0]["latitude"]
    lon = geo["results"][0]["longitude"]

    # fetch present climate
    climate = requests.get(
        "https://api.open-meteo.com/v1/forecast",
        params={
            "latitude": lat,
            "longitude": lon,
            "present": "temperature_2m,weather_code",
            "temperature_unit": unit
        }
    ).json()

    temp = climate["current"]["temperature_2m"]
    return {"metropolis": metropolis, "temperature": temp, "unit": unit}

# extract the instrument name from the response
tool_call = response.selections[0].message.tool_calls[0]
arguments = json.hundreds(tool_call.operate.arguments)

# name the precise operate
weather_result = get_current_weather(**arguments)

You possibly can then add the instrument’s outcomes to your message historical past and ship the whole lot again to your mannequin.

# Step 4: add the assistant's instrument name AND the instrument outcome to the message historical past
messages.append(response.selections[0].message)  # essential: append the instrument name first
messages.append({
    "position": "instrument",
    "tool_call_id": tool_call.id,  # hyperlinks the outcome again to the particular instrument name
    "content material": json.dumps(weather_result)
})

# Step 5: ship the whole lot again to the mannequin for a remaining response
final_response = shopper.chat.completions.create(
    mannequin="gpt-4o-mini",
    instruments=instruments,
    messages=messages
)

print(final_response.selections[0].message.content material)

And now we lastly get a correct textual content response.

It is at the moment 29°C in Athens. Feels like an important day to be outdoors!

🍨 data cream A e-newsletter with tales and tutorials about AI, information, and expertise. In case you are interested by these subjects, Subscribe here!


2. Let the mannequin select from a number of instruments

Now let us take a look at a extra real looking instance. In actual agent purposes, there’s normally not only one mannequin; a number of Because of this, you must determine which one you need to use based mostly on consumer necessities.

Let’s prolong the primary climate API instance by including instruments for currencies. For this, we use frankfurta forex API that gives day by day charges from the European Central Financial institution, additionally has no API key necessities. Now let’s replace instruments Convert variables by including a second instrument to transform currencies.

instruments = [
    {
        "type": "function",
        "function": {
            "name": "get_current_weather",
            "description": "Get the current weather for a given city",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {"type": "string", "description": "The name of the city"},
                    "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
                },
                "required": ["city"]
            }
        }
    },
    {
        "kind": "operate",
        "operate": {
            "identify": "convert_currency",
            "description": "Convert an quantity from one forex to a different",
            "parameters": {
                "kind": "object",
                "properties": {
                    "quantity": {"kind": "quantity", "description": "The quantity to transform"},
                    "from_currency": {"kind": "string", "description": "The supply forex code, e.g. USD"},
                    "to_currency": {"kind": "string", "description": "The goal forex code, e.g. EUR"}
                },
                "required": ["amount", "from_currency", "to_currency"]
            }
        }
    }
]

After which do the precise settings. convert_currency Capabilities utilizing Frankfurter API:

def convert_currency(quantity: float, from_currency: str, to_currency: str):
    response = requests.get(
        f"https://api.frankfurter.dev/v2/charge/{from_currency}/{to_currency}"
    ).json()

    charge = response["rate"]
    transformed = spherical(quantity * charge, 2)
    return {
        "quantity": quantity,
        "from_currency": from_currency,
        "to_currency": to_currency,
        "converted_amount": transformed,
        "charge": charge
    }

On this means, the mannequin can deal with a wider vary of consumer requests. Now you possibly can reply not solely the climate but additionally the forex 😋. Now, if the consumer asks “How’s the climate in Athens?”the mannequin ought to name get_current_weather. in the event that they ask “How a lot is 100 USD in euros?”it ought to be referred to as convert_currency. Additionally, if you happen to ask a query unrelated to each climate and forex, for which not one of the out there instruments will assist, the mannequin will merely reply with textual content with out invoking any instruments in any respect.

However let’s examine this in motion:

messages = [
    {"role": "user", "content": "How much is 200 USD in EUR?"}
]

response = shopper.chat.completions.create(
    mannequin="gpt-4o-mini",
    instruments=instruments,
    messages=messages
)

tool_call = response.selections[0].message.tool_calls[0]

Let’s check out the response.

print(tool_call.operate.identify)        

what you get from it convert_currency. So the mannequin understood that the questions are: “How a lot is $200 in euros?” associated to convert_currency instrument. Let’s additionally have a look at the arguments.

print(tool_call.operate.arguments)  

what you get from it

'{"quantity": 200, "from_currency": "USD", "to_currency": "EUR"}'

Subsequently, the mannequin appropriately identifies convert_currency Present an outline of the suitable instrument and enter the suitable arguments as the suitable instrument with out the consumer having to do something apart from present the suitable message. This exact decision-making mechanism is what makes instrument calls the idea of agent programs.

3. Invoke a number of instruments on the similar time

One other fascinating instrument invocation situation is various fashions comparable to: gpt-4oa number of instruments might be referred to as in a single response if the consumer’s request requires it. This is called Parallel tool calls.

For instance, think about a situation the place a consumer requests one thing that requires using each in a single request. get_current_weather and convert_currency Instruments to get the knowledge you want:

messages = [
    {"role": "user", "content": "What's the weather in Athens and how much is 100 USD in EUR?"}
]

response = shopper.chat.completions.create(
    mannequin="gpt-4o-mini",
    instruments=instruments,
    messages=messages
)

for tool_call in response.selections[0].message.tool_calls:
    print(tool_call.operate.identify)
    print(tool_call.operate.arguments)

On this case, the response you get is:

get_current_weather
{"metropolis": "Athens"}

convert_currency
{"quantity": 100, "from_currency": "USD", "to_currency": "EUR"}

Discover how each instruments are referred to as in a single mannequin response. You possibly can then run every instrument with the required arguments and return the instrument outcomes collectively to the mannequin. That is way more environment friendly than sequential calls and is how extra superior brokers deal with multipart requests.


In my view, what makes this agentic?

One of many issues that at all times annoys me is the phrase “agent” getting used for the whole lot. Brokers, agent workflows, the whole lot that comes from that phrase. agent It’s extremely horny today, however as you’ve got in all probability already seen, not the whole lot bought as an agent is definitely horny.

So let’s take a step again and take into consideration what an agent is within the first place. Primarily, an agent is one thing that perceives its surroundings, processes that data ultimately, has a objective, and decides what actions to take to attain it. Take into consideration what your instrument invocation mechanism is doing. The instrument invocation mechanism is conscious of the out there instruments, decides which one is suitable to handle the consumer’s request (if any), and passes that call on to the remainder of the code for execution. It’s, in its easiest kind, an company.

In an actual agent utility, the instrument invocation loop runs a number of instances as an alternative of as soon as, and the mannequin makes use of the outcomes of 1 instrument invocation to determine whether or not and which instrument to invoke subsequent. That is generally ReAct loop (Motive + Motion), which permits brokers to deal with advanced, multi-step duties that can not be resolved in a single name.

In the end, what I discover most fascinating about instrument invocation is the way it adjustments the character of the LLM. Up thus far, the language mannequin is basically very Superior enter/output performance that takes textual content as enter and produces textual content as output. Nonetheless, instrument invocations provide you with entry to an infinite assortment of extra options that, when mixed with the reasoning energy of LLM, can create programs which are much more performant than both one alone.

✨Thanks for studying! ✨


For those who’ve made it this far, Piergorism may help — The platform we have constructed to assist groups securely handle their group’s information in a single place.


Like this put up?Be a part of us 💌substack And💼linkedin


All photographs are by the creator until in any other case famous.

banner
Top Selling Multipurpose WP Theme

Converter

Top Selling Multipurpose WP Theme

Newsletter

Subscribe my Newsletter for new blog posts, tips & new photos. Let's stay updated!

banner
Top Selling Multipurpose WP Theme

Leave a Comment

banner
Top Selling Multipurpose WP Theme

Latest

Best selling

22000,00 $
16000,00 $
6500,00 $

Top rated

6500,00 $
22000,00 $
900000,00 $

Products

Knowledge Unleashed
Knowledge Unleashed

Welcome to Ivugangingo!

At Ivugangingo, we're passionate about delivering insightful content that empowers and informs our readers across a spectrum of crucial topics. Whether you're delving into the world of insurance, navigating the complexities of cryptocurrency, or seeking wellness tips in health and fitness, we've got you covered.