Building Your First Agent
Building your first agent using LLMs and tools.
June 10, 2025 · Kenneth Ezekiel
Introduction
In the first part of this series, we established the foundational concepts of Agentic AI. We defined an agent as an autonomous system that perceives its environment, reasons about its state, and takes actions using tools to achieve a specific goal, differentiating it from a standard Large Language Model (LLM).
In this article, we will transition from theory to practice. This tutorial will guide you through the process of building a simple but functional AI agent. The objective is to construct a system that can leverage both real-time web search and mathematical calculations to solve a multi-step problem that the underlying LLM could not solve on its own.
Defining the Agent's Task
To effectively demonstrate an agent's capabilities, it must be assigned a clear objective that necessitates planning and tool use. A simple, single-step query would be insufficient.
Therefore, the mission for our agent will be to answer the following question:
"Who was the president of the United States when the lead actor of the movie 'The Matrix' was born, and what is that president's birth year raised to the power of 0.3?"
This task is designed to compel the agent to execute a logical sequence of actions:
- Utilize a search tool to identify the lead actor of 'The Matrix'.
- Use the search tool again to determine the actor's birthdate.
- Use the search tool a third time to find the serving U.S. president during that time.
- Use the search tool once more to find that president's birth year.
- Finally, utilize a calculation tool to perform the required mathematical operation.
Prerequisites and Environment Setup
Before development, it is essential to prepare the environment correctly. The following steps outline the required setup.
Requirements:
- Python (version 3.8 or newer)
- A code editor (e.g., Visual Studio Code)
- An API key from OpenAI
- An API key from Tavily Search
Step 1: Project Environment First, create a dedicated folder for this project. In your terminal, navigate to this folder and create a Python virtual environment.
Step 2: Library Installation With the virtual environment activated, install the necessary Python libraries.
Step 3: API Key Management
API keys should not be hardcoded into source code. A secure practice is to use a .env
file. Create a file with this name in your project's root directory and add your keys as follows:
Create your primary Python file, main.py
, and use the dotenv
library to load these keys as environment variables.
Building the Agent: A Code Walkthrough
This section details the step-by-step construction of the agent's components.
Step 4.1: Initializing the Language Model
The core of the agent is the LLM, which provides the reasoning capabilities. We will instantiate OpenAI's gpt-4o
model.
Step 4.2: Defining the Agent's Tools The agent requires tools to interact with external systems. We will provide a search tool and a calculator.
This tools
list defines the set of external functions the agent is permitted to invoke.
Step 4.3: Designing the Prompt Template The prompt template structures the input to the LLM, guiding its reasoning process.
The agent_scratchpad
is a critical variable. It is a placeholder where the history of previous actions and their corresponding observations is injected. This allows the agent to maintain context and plan its subsequent steps based on what has already transpired.
Step 4.4: Creating the Agent Logic This step binds the LLM, the tools, and the prompt together to form the agent's core logic.
Step 4.5: Creating the Agent Executor
The AgentExecutor
is the runtime component that invokes the agent and manages the execution loop until a final answer is produced.
Setting verbose=True
is essential for observing the agent's step-by-step reasoning process during execution.
Executing the Agent and Analyzing its Output
With all components assembled, we can now invoke the agent with our defined task.
Upon execution, the verbose
output will display the agent's internal monologue and actions, which will resemble the following logical sequence:
Full Code
Conclusion
This tutorial has demonstrated the practical construction of a functional AI agent. By assembling an LLM, tools, a prompt, and an executor, we created a system capable of decomposing a complex problem and leveraging external information to arrive at a solution.
The agent we built is effective but limited to the pre-existing tools we provided. A significant area for expanding agent capability is teaching them to use new, custom-built tools that can interact with proprietary APIs or private data sources.
The next article in this series, Part 3, will address this limitation. We will explore the process of creating custom tools and investigate more advanced prompting techniques to further enhance agent performance and utility.
Read More
Tags: agentic ai, artificial intelligence, machine learning