Beginner
⏱ 45 min read
© Gate of AI 2026-04-09
Learn to build a simple AI agent using Python that can automate tasks and make intelligent decisions. This foundational guide will get you started with agentic AI concepts and practices.
Prerequisites
- Python 3.9 or higher
- Basic understanding of Python programming
- Access to an IDE or code editor (e.g., VSCode, PyCharm)
What We’re Building
In this tutorial, you will create a simple AI agent using Python. This agent will be capable of performing automated tasks based on predefined rules and data inputs. The goal of this project is to introduce you to the core concepts of AI agents, including decision-making, automation, and the integration of basic AI models to enhance functionality.
By the end of this tutorial, your AI agent will be able to process inputs, make decisions based on those inputs, and execute tasks such as sending an email or retrieving data from a web source. This foundational experience will prepare you for more complex agentic projects in the future.
Setup and Installation
To build our AI agent, we need to set up a Python environment and install necessary libraries. We will use `requests` for handling HTTP requests and `smtplib` for sending emails. These tools will enable our agent to interact with external systems and perform tasks.
pip install requestsNext, we need to configure environment variables for sensitive data such as email credentials. Create a `.env` file in your project directory with the following content:
EMAIL_USER=your_email@gmail.com
EMAIL_PASS=your_password
Step 1: Creating the Basic Agent Structure
We will start by defining a basic structure for our AI agent. This involves creating a class that will handle tasks and make decisions based on input data.
class SimpleAgent:
def __init__(self, name):
self.name = name
self.tasks = [] def add_task(self, task):
self.tasks.append(task)
def list_tasks(self):
return self.tasks
agent = SimpleAgent("TaskMaster")
agent.add_task("Send email")
print(agent.list_tasks())
This code initializes an agent with a name and a list of tasks. The `add_task` method allows adding new tasks to the agent’s list, and `list_tasks` returns the current tasks.
Step 2: Implementing Decision-Making Logic
Our next step is to enable the agent to make decisions. We’ll implement a simple decision-making function that checks conditions and executes tasks accordingly.
import randomclass SimpleAgent:
def __init__(self, name):
self.name = name
self.tasks = []
def add_task(self, task):
self.tasks.append(task)
def list_tasks(self):
return self.tasks
def make_decision(self):
if "Send email" in self.tasks and random.choice([True, False]):
self.send_email()
def send_email(self):
print("Email sent!")
agent = SimpleAgent("TaskMaster")
agent.add_task("Send email")
agent.make_decision()
Here, `make_decision` checks if “Send email” is in the tasks list and randomly decides to send an email. This simulates decision-making based on conditions.
Step 3: Enhancing with External Integrations
To make our agent more functional, we’ll integrate it with an email service using Python’s `smtplib`. This allows the agent to send real emails as part of its tasks.
import smtplib
from dotenv import load_dotenv
import osload_dotenv()
class SimpleAgent:
def __init__(self, name):
self.name = name
self.tasks = []
def add_task(self, task):
self.tasks.append(task)
def list_tasks(self):
return self.tasks
def make_decision(self):
if "Send email" in self.tasks:
self.send_email()
def send_email(self):
user = os.getenv('EMAIL_USER')
password = os.getenv('EMAIL_PASS')
with smtplib.SMTP('smtp.gmail.com', 587) as server:
server.starttls()
server.login(user, password)
server.sendmail(user, "recipient@example.com", "Subject: HinnThis is a test email from AI Agent.")
print("Email sent!")
agent = SimpleAgent("TaskMaster")
agent.add_task("Send email")
agent.make_decision()
This integration allows the agent to send an email using Gmail’s SMTP server. Ensure your Gmail account allows less secure apps to access SMTP.
Testing Your Implementation
To verify that your AI agent works, run the script and check for the “Email sent!” message in the console. You should also receive an email in the specified recipient’s inbox.
python agent.pyWhat to Build Next
- Integrate a web scraping task to fetch data periodically and analyze it.
- Expand the decision-making logic to include more complex rules and data-driven insights.
- Build a user interface to interact with the agent and visualize its tasks and decisions.