adding-tools-to-ai-agent

Adding Tools to an AI Agent

In the previous article, Creating a Simple AI Agent, we created our first working AI Agent using Python and the OpenAI Agents SDK.

Our Agent could receive a request, follow its instructions, communicate with an AI model, and return a response.

However, our Agent was still limited.

It could generate an answer, but it could not directly interact with external systems or perform actions.

In this article, we will give our Agent an important new capability:

Tools.

A tool allows an AI Agent to interact with the outside world and perform a specific action.

What Is a Tool?

An AI Agent tool is a function or capability that the Agent can use to perform a task.

For example, an Agent could have access to tools such as:

  • Calculator
  • Weather service
  • Web search
  • File system
  • Database
  • Calendar
  • Email
  • Git
  • External APIs

The Agent does not necessarily use every tool for every request.

Instead, the Agent can determine when a particular tool is useful.

A simple architecture looks like this:

User
  |
  v
AI Agent
  |
  v
Understand Request
  |
  v
Choose Tool
  |
  v
Execute Tool
  |
  v
Receive Result
  |
  v
Generate Answer

This ability to use tools is one of the important characteristics of an AI Agent.

Why Do Agents Need Tools?

An AI model can understand language and generate responses, but it does not automatically have access to every external system.

For example, suppose we ask:

What is 25 × 18?

The model can probably calculate the answer itself.

But imagine that we ask:

What is the current temperature in Stockholm?

The Agent needs access to a source of current information.

Or:

Read the sales database and tell me how many orders were placed yesterday.

The Agent needs access to a database.

Or:

Check my calendar and tell me when I am free tomorrow.

The Agent needs access to a calendar system.

Tools give the Agent these capabilities.

Our First Tool

For our practical example, we will start with something very simple:

A Calculator Tool.

The purpose is not to create a sophisticated calculator.

The purpose is to understand the relationship between:

Agent → Tool → Result

Our Agent will receive a request such as:

Calculate the total price of 15 products at €23.50 each.

The Agent can determine that a calculation is required and use the calculator tool.

The Agent Without a Tool

Before adding the tool, our architecture looks approximately like this:

User
  |
  v
AI Agent
  |
  v
LLM
  |
  v
Answer

The Agent receives the request and generates a response.

The Agent With a Tool

After adding the calculator, the architecture becomes:

                         +----------------+
                         |  Calculator    |
                         |     Tool       |
                         +-------^--------+
                                 |
                                 |
User → AI Agent → LLM → Tool Decision
                                 |
                                 v
                              Result
                                 |
                                 v
                              Answer

Now the Agent has an additional capability.

Step 1 – Open Our Existing Project (AI-Agent)

We will continue using the project from the previous article (Creating a simple AI Agent).

Our project should look similar to:

AI-Agent/
│
├── .venv/
│
├── .env
│
└── agent.py

Open the project in Visual Studio Code.

We will modify the existing agent.py file.

Step 2 – Create the Calculator Function

First, we create a normal Python function.

def calculate_total(price: float, quantity: int) -> float:
    return price * quantity

The function receives two values:

price
quantity

and returns:

price × quantity

For example:

price = 23.50
quantity = 15

The result is:

352.50

At this point, this is just a normal Python function.

The Agent does not know about it yet.

Step 3 – Give the Function to the Agent

The next step is to make the function available as an Agent tool.

The OpenAI Agents SDK provides mechanisms for exposing Python functions as tools that an Agent can use.

The exact SDK interface can change between versions, so we should always use the current Agents SDK documentation when implementing the tool.

The important concept is:

Python Function
       ↓
     Tool
       ↓
     Agent

Once the function is registered as a tool, the Agent can decide when it should be used.

Step 4 – Tell the Agent About the Tool

Our Agent instructions can explain its role.

For example:

You are a helpful software development assistant.

When a calculation is required, use the calculator tool.

Explain the result clearly to the user.

Notice that we are not telling the Agent exactly when to call the function for every possible question.

We are giving the Agent the capability and instructions.

The Agent can then determine whether the tool is appropriate.

Step 5 – Ask the Agent to Calculate Something

Now we can test our Agent.

For example:

Calculate the total cost of 15 products at €23.50 each.

This shall be done in the agent.py as follow:

async def main():
    result = await Runner.run(
        agent,
        "Calculate the total cost of 15 products at €23.50 each.",
        
    )

The Agent receives the request.

It recognizes that a calculation is required.

It can then use the calculator tool.

The process becomes:

User Request
     |
     v
"Calculate 15 × €23.50"
     |
     v
AI Agent
     |
     v
Determine that calculation is needed
     |
     v
Calculator Tool
     |
     v
352.50
     |
     v
AI Agent
     |
     v
Final Answer

The final response could be:

The total cost is €352.50.

Step 6 – Run the Agent

Make sure your virtual environment is activated.

In the VS Code terminal, you should see something similar to:

(.venv) PS C:\Utvecklingprogram\AI\AI-Agent>

Then run:

python agent.py

If everything is configured correctly, the Agent should execute the request and return the result.

Screenshot of result

(.venv) PS C:\Utvecklingprogram\AI\AI-Agent> python agent.py
The total cost is **€352.50**.
(.venv) PS C:\Utvecklingprogram\AI\AI-Agent> 

What Happened Behind the Scenes?

This is the most important part of the example.

The user did not directly call the calculator function.

Instead, the user gave the Agent a goal.

The Agent determined that a tool was appropriate.

The tool performed the calculation.

The result was returned to the Agent.

The Agent then generated the final response.

Conceptually:

1. User gives goal
       ↓
2. Agent understands request
       ↓
3. Agent decides a tool is needed
       ↓
4. Agent calls the tool
       ↓
5. Tool performs the operation
       ↓
6. Tool returns the result
       ↓
7. Agent uses the result
       ↓
8. Agent responds to the user

This is the basic tool-calling workflow.

Tool Calling

The process is often called tool calling or function calling.

The important idea is that the AI model does not necessarily execute the function itself.

Instead, it can request that a particular tool be called with particular arguments.

The Agent framework handles the interaction between the model and the tool.

A simplified example is:

LLM:
"I need to use the calculator."

Tool call:
calculate_total(
    price=23.50,
    quantity=15
)

Tool result:
352.50

LLM:
"The total cost is €352.50."

This separation is important when designing Agent systems.

The Agent Does Not Need to Use Every Tool

Suppose our Agent has a calculator tool.

The user asks:

What is an AI Agent?

There is no reason to use the calculator.

The Agent can simply answer the question.

User
  |
  v
AI Agent
  |
  +---- Calculator not needed
  |
  v
Answer

But if the user asks:

Calculate 125 × 48.

the calculator becomes useful.

User
  |
  v
AI Agent
  |
  v
Calculator Tool
  |
  v
6000
  |
  v
Answer

This ability to choose whether a tool is required is an important part of Agent behavior.

Adding More Tools

Once we understand the calculator example, we can imagine giving the Agent many other tools.

For example:

                    AI Agent
                       |
       +---------------+---------------+
       |               |               |
       v               v               v
   Calculator       Web Search      Database
       |               |               |
       v               v               v
   Calculation      Information       Data

The Agent can choose the appropriate tool depending on the user’s request.

For example:

User:

Calculate 45 × 27.

Agent:

Calculator Tool.

User:

What is the latest version of Python?

Agent:

Web Search Tool.

User:

How many customers registered yesterday?

Agent:

Database Tool.

Tools Can Perform Real Actions

Tools do not have to be limited to retrieving information.

They can also perform actions.

For example, an Agent could have tools that:

  • Create a file
  • Modify a document
  • Search a database
  • Create a calendar event
  • Send an email
  • Create a Git commit
  • Run a test
  • Call an external API

This is where Agents become particularly powerful.

For example:

User:
"Analyze my project and run the tests."

             ↓

AI Agent
             ↓
      File System Tool
             ↓
       Read project
             ↓
       Test Runner
             ↓
       Run tests
             ↓
      Analyze results
             ↓
        User

Security and Permissions

Giving an Agent tools also introduces security concerns.

A tool should only have the permissions it actually needs.

For example, a calculator is relatively low risk.

A database tool may have access to sensitive information.

A tool that deletes files or sends emails can have much greater consequences.

Therefore, when designing an Agent, we should ask:

  • What tools does the Agent need?
  • What information can each tool access?
  • What actions can each tool perform?
  • What permissions should the tool have?
  • Which actions require human approval?

A good principle is:

Give the Agent the minimum permissions required to perform its task.

What We Have Learned

In this article, we extended our simple AI Agent by introducing the concept of tools.

We learned that a tool is a capability that allows an Agent to interact with external functions, data, or systems.

We also learned the basic tool-calling workflow:

User
 ↓
Agent
 ↓
Understand
 ↓
Choose Tool
 ↓
Execute Tool
 ↓
Receive Result
 ↓
Generate Response
 ↓
User

Our calculator example is intentionally simple.

The important thing is not the calculator itself.

The important thing is understanding how an Agent can decide to use an external capability and then use the result to complete the user’s request.

Conclusion

Tools are one of the most important building blocks of AI Agents.

Without tools, an Agent is largely limited to the capabilities of its underlying AI model.

With tools, an Agent can interact with external systems and perform useful actions.

In our next article, we will give our Agent another important capability:

Memory.

We will explore how an Agent can remember information from previous interactions and how memory can be used to create more useful and personalized Agent systems.

Adding Memory to an AI Agent

← Back to AI Agents – Step-by-Step

← Back to Home Page