How to Add Tools to an AI Agent with Python1.
1.What Is a Tool in an AI Agent?
A tool is a function, API, database, or external service that an AI Agent can use to perform a specific task.
For example, an AI agent may know how to check the weather, but it needs a weather API tool to get the current weather information.

2.How a Tool Works
The basic flow looks like this:
User → AI Agent → Select Tool → Execute Tool → Tool Result → AI Agent → Response
For example:
User:
What is the weather in Mumbai today?
AI Agent:
The agent understands that it needs current weather information.
Agent → Weather Tool:get_weather("Mumbai")
Weather Tool:
Returns current weather data.
What Is the Role of Tools in an AI Agent?
Tools give an AI Agent the ability to interact with the outside world. without tools, an agent mainly works with the information and capabilities available to its model. With tools, it can perform actions such as:
- Search for information
- Get real-time weather
- Perform calculations
- Read or update databases
- Send emails
- Call external APIs
- Read or create files
- Schedule appointments
AI Agent With a Tool
With a stock-price API tool, the agent can:
- Understand the user’s question.
- Identify that real-time data is required.
- Select the stock-price tool.
- Send
RELIANCEto the tool. - Receive the latest available data.
- Explain the result to the user.
So, the tool acts as a bridge between the AI Agent and external systems.
user_name = os.getenv("RAK_USER")
user_token = os.getenv("USER_TOKEN")
url = "https://rkdigitalschool.com/1/messages.json"
if user_name:
if user_name.startswith("u"):
print("user found and looks good")
else:
print("user found but doesn't start with u")
else:
print("User not found")
if user_token:
if user_token.startswith("a"):
print("token found and looks good")
else:
print("token found but doesn't start with a")
else:
print("token not found")
# Remember this?
def push(message):
print(f"Push: {message}")
payload = {"user": user_name, "token": user_token, "message": message}
requests.post(url, data=payload)
push("HEY!!")
push
# Now this:
@function_tool
def push_tool(message: str) -> str:
""" Send the given message to the user as a push notification """
payload = {"user": user_name, "token": user_token, "message": message}
result = requests.post(url, data=payload).status_code
return f"Push sent with API status code {result}"
push_tool
push_tool.description
notifier = Agent(name="Notifier", model="gpt-5.4-mini", instructions="You notify the user upon request", tools=[push_tool])
with trace("Pizza has arrived"):
result = await Runner.run(notifier, "Notify the user that the pizza is here")
print(result.final_output)
Explanation:
user_name = os.getenv("RAK_USER")
user_token = os.getenv("USER_TOKEN")
os.getenv() reads a value from your environment variables.
Why use environment variables?
You generally should not hard-code sensitive information such as tokens directly in Python code. this keeps the token outside your source code.
Defining the API URL
url = "https://rkdigitalschool.com/1/messages.json"
This is the API endpoint where your notification request will be sent.
Checking whether the username exists
if user_name:
if user_name.startswith("u"):
print("user found and looks good")
else:
print("user found but doesn't start with u")
else:
print("User not found")
Checking the token
if user_token:
if user_token.startswith("a"):
print("token found and looks good")
else:
print("token found but doesn't start with a")
else:
print("token not found")
It checks two things:
- Does a token exist?
- Does it start with
"a"?
Creating the normal Python function
def push(message):
print(f"Push: {message}")
payload = {
"user": user_name,
"token": user_token,
"message": message
}
requests.post(url, data=payload)
This is a normal Python function.
Converting the function into an AI Agent tool
@function_tool
def push_tool(message: str) -> str:
""" Send the given message to the user as a push notification """
payload = {"user": user_name, "token": user_token, "message": message}
result = requests.post(url, data=payload).status_code
return f"Push sent with API status code {result}"
Converts your Python function into a tool that an AI Agent can use.this is a major concept in agentic AI.
Building the payload
payload = {
"user": user_name,
"token": user_token,
"message": message
}
Sending the request
result = requests.post(url, data=payload).status_code
This sends the POST request. the important difference from the previous function is that you’re now capturing the HTTP status code.
Returning the result to the Agent
return f"Push sent with API status code {result}"
Creating the Agent with tool
notifier = Agent(
name="Notifier",
model="gpt-5.4-mini",
instructions="You notify the user upon request",
tools=[push_tool]
)
Running the Agent
with trace("Pizza has arrived"):
result = await Runner.run(notifier, "Notify the user that the pizza is here")
print(result.final_output)
This creates a trace around the Agent execution. It can help you inspect what happened during the Agent run, such as the model execution and tool calls.
Runner.run()
result = await Runner.run(
notifier,
"Notify the user that the pizza is here"
)
This starts the Agent.
The user request is:
Notify the user that the pizza is here
The Agent receives this request.
Getting the final answer
print(result.final_output)
prints the Agent’s final response.
For example, it might produce:
The user has been notified that the pizza is here.
The exact wording depends on the model.
Complete architecture
