Tuesday, November 4, 2025

Easy methods to Construct An AI Agent with Perform Calling and GPT-5


and Massive Language Fashions (LLMs)

Massive language fashions (LLMs) are superior AI methods constructed on deep neural community similar to transformers and skilled on huge quantities of textual content to generate human-like language. LLMs like ChatGPT, Claude, Gemini and Grok can sort out many difficult duties and are used throughout fields similar to science, healthcare, training, and finance.

An AI agent extends the capabilites of LLMs to resolve duties which are past their pre-trained information. An LLM can write a Python tutorial from what it realized throughout coaching. If you happen to ask it to e-book a flight, the duty requires entry to your calendar, net search and the flexibility to take actions, these fall past the LLM’s pre-trained information. Among the frequent actions embody:

  • Climate forecast: The LLM connects to an internet search instrument to fetch the newest climate forecast.
  • Reserving agent: An AI agent that may verify a person’s calendar, search the online to go to a reserving web site like Expedia to search out out there choices for flights and lodges, current them to the person for affirmation, and full the reserving on behalf of the person.

How an AI Agent Works

AI brokers kind a system that makes use of a Massive Language Mannequin to plan, motive, and take steps to work together with its surroundings utilizing instruments urged from the mannequin’s reasoning to resolve a specific process.

Fundamental Construction of an AI Agent

Picture Generated By Gemini
  • A Massive Language Mannequin (LLM): the LLM is the mind of an AI agent. It takes a person’s immediate, plans and causes by the request and breaks the issue into steps that decide which instruments it ought to use to finish the duty.
  • A instrument is the framework that the agent makes use of to carry out an motion based mostly on the plan and reasoning from the Massive Language Mannequin. If you happen to ask an LLM to e-book a desk for you at a restaurant, attainable instruments that shall be used embody calendar to verify your availability and an internet search instrument to entry the restaurant web site and make a reservation for you.

Ilustrated Resolution Making of a Reserving AI Agent

Picture Generated By ChatGPT

AI brokers can entry totally different instruments relying on the duty. A instrument is perhaps an information retailer, similar to a database. For instance, a customer-support agent may entry a buyer’s account particulars and buy historical past and determine when to retrieve that data to assist resolve a difficulty.

AI brokers are used to resolve a variety of duties, and there are a lot of highly effective brokers out there. Coding brokers, significantly agentic IDEs similar to Cursor, Windsurf, and GitHub Copilot assist engineers write and debug code sooner and construct tasks rapidly. CLI Coding brokers like Claude Code and Codex CLI can work together with a person’s desktop and terminal to hold out coding duties. ChatGPT helps brokers that may carry out actions similar to reserving reservations on a person’s behalf. Brokers are additionally built-in into buyer help workflows to speak with clients and resolve their points.

Perform Calling

Perform calling is a way for connecting a big language mannequin (LLM) to exterior instruments similar to APIs or databases. It’s utilized in creating AI brokers to attach LLMs to instruments. In operate calling, every instrument is outlined as a code operate (for instance, a climate API to fetch the newest forecast) together with a JSON Schema that specifies the operate’s parameters and instructs the LLM on when and methods to name the operate for a given process.

The kind of operate outlined will depend on the duty the agent is designed to carry out. For instance, for a buyer help agent we will outline a operate that may extract data from unstructured information, similar to PDFs containing particulars a few enterprise’s merchandise.

On this publish I’ll exhibit methods to use operate calling to construct a easy net search agent utilizing GPT-5 as the massive language mannequin.

Fundamental Construction of a Internet Search Agent

Picture Generated By Gemini

The principle logic behind the online search agent:

  • Outline a code operate to deal with the online search.
  • Outline customized directions that information the massive language mannequin in figuring out when to name the online search operate based mostly on the question. For instance, if the question asks concerning the present climate, the online search agent will acknowledge the necessity to search the web to get the newest climate experiences. Nonetheless, if the question asks it to write down a tutorial a few programming language like Python, one thing it may possibly reply from its pre-trained information it won’t name the online search operate and can reply instantly as an alternative.

Prerequisite

Create an OpenAI account and generate an API key
1: Create an OpenAI Account in the event you don’t have one
2: Generate an API Key

Arrange and Activate Atmosphere

python3 -m venv env
supply env/bin/activate

Export OpenAI API Key

export OPENAI_API_KEY="Your Openai API Key"

Setup Tavily for Internet Search
Tavily is a specialised web-search instrument for AI brokers. Create an account on Tavily.com, and as soon as your profile is about up, an API key shall be generated you could copy into your surroundings. New accounta obtain 1000 free credit that can be utilized for as much as 1000 net searches.

Export TAVILY API Key

export TAVILY_API_KEY="Your Tavily API Key"

Set up Packages

pip3 set up openai
pip3 set up tavily-python

Constructing a Internet Search Agent with Perform Calling Step by Step

Step 1: Create Internet Search Perform with Tavily

An internet search operate is applied utilizing Tavily, serving because the instrument for operate calling within the net search agent.

from tavily import TavilyClient
import os

tavily = TavilyClient(api_key=os.getenv("TAVILY_API_KEY"))

def web_search(question: str, num_results: int = 10):
    strive:
        outcome = tavily.search(
            question=question,
            search_depth="fundamental",
            max_results=num_results,
            include_answer=False,       
            include_raw_content=False,
            include_images=False
        )

        outcomes = outcome.get("outcomes", [])

        return {
            "question": question,
            "outcomes": outcomes, 
            "sources": [
                {"title": r.get("title", ""), "url": r.get("url", "")}
                for r in results
            ]
        }

    besides Exception as e:
        return {
            "error": f"Search error: {e}",
            "question": question,
            "outcomes": [],
            "sources": [],
        }

Internet operate code breakdown

Tavily is initialized with its API key. Within the web_search operate, the next steps are carried out:

  • Tavily search operate is known as to look the web and retrieve the highest 10 outcomes.
  • The search outcomes and their corresponding sources are returned.

This returned output will function related context for the online search agent: which we are going to outline later on this article, to fetch up-to-date data for queries (prompts) that require real-time information similar to climate forecasts.

Step 2: Create Software Schema

The instrument schema defines customized directions for an AI mannequin on when it ought to name a instrument, on this case the instrument that shall be utilized in an internet search operate. It additionally specifies the situations and actions to be taken when the mannequin calls a instrument. A json instrument schema is outlined under based mostly on the OpenAI instrument schema construction.

tool_schema = [
    {
        "type": "function",
        "name": "web_search",

        "description": """Execute a web search to fetch up to date information. Synthesize a concise, 
        self-contained answer from the content of the results of the visited pages.
        Fetch pages, extract text, and provide the best available result while citing 1-3 sources (title + URL). 
        If sources conflict, surface the uncertainty and prefer the most recent evidence.
        """,

        "strict": True,
        "parameters": {
            "type": "object",
            "properties": {
                "query": {
                    "type": "string",
                    "description": "Query to be searched on the web.",
                },
            },
            "required": ["query"],
            "additionalProperties": False
        },
    },
]

Software schema’s Properties

  • sort: Specifies that the kind of instrument is a operate.
  • title: the title of the operate that shall be used for instrument name, which is web_search.
  • description: Describes what the AI mannequin ought to do when calling the online search instrument. It instructs the mannequin to look the web utilizing the web_search operate to fetch up-to-date data and extract related particulars to generate one of the best response.
  • strict: It’s set to true, this property instructs the LLM to strictly comply with the instrument schema’s directions.
  • parameters: Defines the parameters that shall be handed into the web_search operate. On this case, there is just one parameter: question which represents the search time period to search for on the web.
  • required: Instructs the LLM that question is a compulsory parameter for the web_search operate.
  • additionalProperties: it’s set to false, that means that the instrument’s arguments object can not embody any parameters aside from these outlined beneath parameters.properties.

Step 3: Create the Internet Search Agent Utilizing GPT-5 and Perform Calling

Lastly I’ll construct an agent that we will chat with, which may search the online when it wants up-to-date data. I’ll use GPT-5-mini, a quick and correct mannequin from OpenAI, together with operate calling to invoke the instrument schema and the net search operate already outlined.

from datetime import datetime, timezone
import json
from openai import OpenAI
import os 

consumer = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

# tracker for the final mannequin's response id to keep up dialog's state 
prev_response_id = None

# an inventory for storing instrument's outcomes from the operate name 
tool_results = []

whereas True:
    # if the instrument outcomes is empty immediate message 
    if len(tool_results) == 0:
        user_message = enter("Person: ")

        """ instructions for exiting chat """
        if isinstance(user_message, str) and user_message.strip().decrease() in {"exit", "q"}:
            print("Exiting chat. Goodbye!")
            break

    else:
        user_message = tool_results.copy()
    
        # clear the instrument outcomes for the following name 
        tool_results = []

    # get hold of present's date to be handed into the mannequin as an instruction to help in resolution making
    today_date = datetime.now(timezone.utc).date().isoformat()     

    response = consumer.responses.create(
        mannequin = "gpt-5-mini",
        enter = user_message,
        directions=f"Present date is {today_date}.",
        instruments = tool_schema,
        previous_response_id=prev_response_id,
        textual content = {"verbosity": "low"},
        reasoning={
            "effort": "low",
        },
        retailer=True,
        )
    
    prev_response_id = response.id

    # Handles mannequin response's output 
    for output in response.output:
        
        if output.sort == "reasoning":
            print("Assistant: ","Reasoning ....")

            for reasoning_summary in output.abstract:
                print("Assistant: ",reasoning_summary)

        elif output.sort == "message":
            for merchandise in output.content material:
                print("Assistant: ",merchandise.textual content)

        elif output.sort == "function_call":
            # get hold of operate title 
            function_name = globals().get(output.title)
            # hundreds operate arguments 
            args = json.hundreds(output.arguments)
            function_response = function_name(**args)
            tool_results.append(
                {
                    "sort": "function_call_output",
                    "call_id": output.call_id,
                    "output": json.dumps(function_response)
                }
            )

Step by Step Code Breakdown

from openai import OpenAI
import os 

consumer = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
prev_response_id = None
tool_results = []
  • Initialized the OpenAI mannequin API with an API key.
  • Initialized two variables prev_response_id and tool_resultsprev_response_id retains monitor of the mannequin’s response to keep up dialog state, and tool_results is an inventory that shops outputs returned from the web_search operate name.

The chat runs contained in the loop. A person enters a message and the mannequin referred to as with instrument schema accepts the message, causes over it, decides whether or not to name the online search instrument, after which the instrument’s output is handed again to the mannequin. The mannequin generates a context-aware response. This continues till the person exits the chat.

Code Walkthrough of the Loop

if len(tool_results) == 0:
    user_message = enter("Person: ")
    if isinstance(user_message, str) and user_message.strip().decrease() in {"exit", "q"}:
        print("Exiting chat. Goodbye!")
        break

else:
    user_message = tool_results.copy()
    tool_results = []

today_date = datetime.now(timezone.utc).date().isoformat()     

response = consumer.responses.create(
    mannequin = "gpt-5-mini",
    enter = user_message,
    directions=f"Present date is {today_date}.",
    instruments = tool_schema,
    previous_response_id=prev_response_id,
    textual content = {"verbosity": "low"},
    reasoning={
        "effort": "low",
    },
    retailer=True,
    )

prev_response_id = response.id
  • Checks if the tool_results is empty. Whether it is, the person shall be prompted to sort in a message, with an choice to stop utilizing exit or q.
  • If the tool_results is just not empty, user_message shall be set to the collected instrument outputs to be despatched to the mannequin. tool_results is cleared to keep away from resending the identical instrument outputs on the following loop iteration.
  • The present date (today_date) is obtained for use by the mannequin to make time-aware choices.
  • Calls consumer.responses.create to generate the mannequin’s response and it accepts the next parameters:
    • mannequin: set to gpt-5-mini.
    • enter: accepts the person’s message.
    • directions: set to present’s date (today_date).
    • instruments: set to the instrument schema that was outlined earlier.
    • previous_response_id: set to the earlier response’s id so the mannequin can preserve dialog state.
    • textual content: verbosity is about to low to maintain mannequin’s response concise.
    • reasoning: GPT-5-mini is a reasoning mannequin, set the reasoning’s effort to low for sooner’s response. For extra advanced duties we will set it to excessive.
    • retailer: tells the mannequin to retailer the present’s response so it may be retrieved later and helps with dialog continuity.
  • prev_response_id is about to present’s response id so the following operate name can thread onto the identical dialog.
for output in response.output:
    if output.sort == "reasoning":
        print("Assistant: ","Reasoning ....")

        for reasoning_summary in output.abstract:
            print("Assistant: ",reasoning_summary)

    elif output.sort == "message":
        for merchandise in output.content material:
            print("Assistant: ",merchandise.textual content)

    elif output.sort == "function_call":
        # get hold of operate title 
        function_name = globals().get(output.title)
        # hundreds operate arguments 
        args = json.hundreds(output.arguments)
        function_response = function_name(**args)
        # append instrument outcomes listing with the the operate name's id and performance's response 
        tool_results.append(
            {
                "sort": "function_call_output",
                "call_id": output.call_id,
                "output": json.dumps(function_response)
            }
        )

This processes the mannequin’s response output and does the next;

  • If the output sort is reasoning, print every merchandise within the reasoning abstract.
  • If the output sort is message, iterate by the content material and print every textual content merchandise.
  • If the output sort is a operate name, get hold of the operate’s title, parse its arguments, and cross them to the operate (web_search) to generate a response. On this case, the online search response accommodates up-to-date data related to the person’s message. Lastly appends the operate name’s response and performance name id to tool_results. This lets the following loop ship the instrument outcome again to the mannequin.

Full Code for the Internet Search Agent

from datetime import datetime, timezone
import json
from openai import OpenAI
import os 
from tavily import TavilyClient

tavily = TavilyClient(api_key=os.getenv("TAVILY_API_KEY"))

def web_search(question: str, num_results: int = 10):
    strive:
        outcome = tavily.search(
            question=question,
            search_depth="fundamental",
            max_results=num_results,
            include_answer=False,       
            include_raw_content=False,
            include_images=False
        )

        outcomes = outcome.get("outcomes", [])

        return {
            "question": question,
            "outcomes": outcomes, 
            "sources": [
                {"title": r.get("title", ""), "url": r.get("url", "")}
                for r in results
            ]
        }

    besides Exception as e:
        return {
            "error": f"Search error: {e}",
            "question": question,
            "outcomes": [],
            "sources": [],
        }


tool_schema = [
    {
        "type": "function",
        "name": "web_search",
        "description": """Execute a web search to fetch up to date information. Synthesize a concise, 
        self-contained answer from the content of the results of the visited pages.
        Fetch pages, extract text, and provide the best available result while citing 1-3 sources (title + URL). "
        If sources conflict, surface the uncertainty and prefer the most recent evidence.
        """,
        "strict": True,
        "parameters": {
            "type": "object",
            "properties": {
                "query": {
                    "type": "string",
                    "description": "Query to be searched on the web.",
                },
            },
            "required": ["query"],
            "additionalProperties": False
        },
    },
]

consumer = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

# tracker for the final mannequin's response id to keep up dialog's state 
prev_response_id = None

# an inventory for storing instrument's outcomes from the operate name 
tool_results = []

whereas True:
    # if the instrument outcomes is empty immediate message 
    if len(tool_results) == 0:
        user_message = enter("Person: ")

        """ instructions for exiting chat """
        if isinstance(user_message, str) and user_message.strip().decrease() in {"exit", "q"}:
            print("Exiting chat. Goodbye!")
            break

    else:
        # set the person's messages to the instrument outcomes to be despatched to the mannequin 
        user_message = tool_results.copy()
    
        # clear the instrument outcomes for the following name 
        tool_results = []

    # get hold of present's date to be handed into the mannequin as an instruction to help in resolution making
    today_date = datetime.now(timezone.utc).date().isoformat()     

    response = consumer.responses.create(
        mannequin = "gpt-5-mini",
        enter = user_message,
        directions=f"Present date is {today_date}.",
        instruments = tool_schema,
        previous_response_id=prev_response_id,
        textual content = {"verbosity": "low"},
        reasoning={
            "effort": "low",
        },
        retailer=True,
        )
    
    prev_response_id = response.id


    # Handles mannequin response's output 
    for output in response.output:
        
        if output.sort == "reasoning":
            print("Assistant: ","Reasoning ....")

            for reasoning_summary in output.abstract:
                print("Assistant: ",reasoning_summary)

        elif output.sort == "message":
            for merchandise in output.content material:
                print("Assistant: ",merchandise.textual content)

        # checks if the output sort is a operate name and append the operate name's outcomes to the instrument outcomes listing
        elif output.sort == "function_call":
            # get hold of operate title 
            function_name = globals().get(output.title)
            # hundreds operate arguments 
            args = json.hundreds(output.arguments)
            function_response = function_name(**args)
            # append instrument outcomes listing with the the operate name's id and performance's response 
            tool_results.append(
                {
                    "sort": "function_call_output",
                    "call_id": output.call_id,
                    "output": json.dumps(function_response)
                }
            )

While you run the code, you’ll be able to simply chat with the agent to ask questions that require the newest data, similar to the present climate or the newest product releases. The agent responds with up-to-date data together with the corresponding sources from the web. Under is a pattern output from the terminal.

Person: What's the climate like in London right now?
Assistant:  Reasoning ....
Assistant:  Reasoning ....
Assistant:  Proper now in London: overcast, about 18°C (64°F), humidity ~88%, mild SW wind ~16 km/h, no precipitation reported. Supply: WeatherAPI (present situations) — https://www.weatherapi.com/

Person: What's the newest iPhone mannequin?
Assistant:  Reasoning ....
Assistant:  Reasoning ....
Assistant:  The newest iPhone fashions are the iPhone 17 lineup (together with iPhone 17, iPhone 17 Professional, iPhone 17 Professional Max) and the brand new iPhone Air — introduced by Apple on Sept 9, 2025. Supply: Apple Newsroom — https://www.apple.com/newsroom/2025/09/apple-debuts-iphone-17/

Person: Multiply 500 by 12.           
Assistant:  Reasoning ....
Assistant:  6000
Person: exit   
Exiting chat. Goodbye!

You may see the outcomes with their corresponding net sources. While you ask it to carry out a process that doesn’t require up-to-date data, similar to maths calculations or writing code the agent responds instantly with none net search.

Word: The online search agent is an easy, single-tool agent. Superior agentic methods orchestrate a number of specialised instruments and use environment friendly reminiscence to keep up context, plan, and resolve extra advanced duties.

Conclusion

On this publish I defined how an AI agent works and the way it extends the capabilities of a big language mannequin to work together with its surroundings, carry out actions and resolve duties by using instruments. I additionally defined operate calling and the way it allows LLMs to name instruments. I demonstrated methods to create a instrument schema for operate calling that defines when and the way an LLM ought to name a instrument to carry out an motion. I outlined an internet search operate utilizing Tavily to fetch data from the online after which confirmed step-by-step methods to construct an internet search agent utilizing operate calling and GPT-5-mini because the LLM. Ultimately, we constructed an internet search agent able to retrieving up-to-date data from the web to reply person queries.

Try my GitHub repo, GenAI-Programs the place I’ve revealed extra programs on numerous Generative AI matters. It additionally features a information on constructing an Agentic RAG utilizing operate calling.

Attain out to me by way of:

Electronic mail: [email protected]

Linkedin: https://www.linkedin.com/in/ayoola-olafenwa-003b901a9/

References

https://platform.openai.com/docs/guides/function-calling?api-mode=responses

https://docs.tavily.com/documentation/api-reference/endpoint/search

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

PHP Code Snippets Powered By : XYZScripts.com