Getting Started with AI Agents
AI agents have become one of the most talked-about topics of this decade. You’ll find them mentioned in job postings, company profiles, freelance gigs, and beyond. Despite how buzzy it all sounds, building an AI agent is surprisingly straightforward. In fact, you can put together a basic one in just a few minutes. That’s exactly what we’ll walk through in this article.
Here, we’ll take you through the entire process of building an AI agent from scratch. No prior experience is needed — every step is explained in plain, beginner-friendly language. We’ll also walk you through installing Python and setting up the IDE where we’ll do our coding. Think of this as a hands-on AI agent tutorial designed for anyone brand new to programming, coding, or artificial intelligence.
So, What Are AI Agents?
Before diving in, let’s clarify what AI agents actually are. Unlike basic chatbots that simply respond to specific questions, AI agents go much further. They don’t just answer — they make independent decisions, create content, and carry out tasks on their own. They can observe their environment, reason through problems, make choices, and take action to accomplish goals with very little human guidance.
Let’s say you need a new laptop for heavy programming work. If you ask a regular chatbot, it might suggest a few laptops and then wait for you to ask follow-up questions one at a time. It has limited memory and essentially functions as a text generator. An AI agent, by contrast, takes your goal and runs with it — no need to point it toward each specific detail. It researches options, compares products, plans out the analysis, and evaluates requirements to arrive at well-informed recommendations. For that same laptop question, a chatbot might give you a one-line answer, while an AI agent would deliver a full comparison table with different models, prices, pros and cons — everything you need to make a confident decision.
How Does an AI Agent Actually Work?
At its core, an AI agent is a smart program built to achieve a specific goal. When you give it a task, it starts by receiving the request and breaking it down into smaller, manageable pieces. If it needs more information, it’ll ask you clarifying questions to fully understand what you’re looking for. Then it taps into tools like web searches, calculators, and its own memory to gather extra data, carefully analyzes everything, weighs different options, and puts together a tailored response.
Now that you understand what AI agents are and how they function, let’s roll up our sleeves and build one ourselves.
Creating an AI Educational Agent with Python
In this guide, we’ll build an AI Educational Agent that serves as your own personal study assistant.
Before we jump into the code, let’s make sure everything is set up on your end:
Setting Up Python
If you’re completely new to this, you probably haven’t installed Python yet. Since this project is built in Python, that’s our first step. Click the link below and follow the installation instructions.
During setup, make sure to check the box labeled “Add Python to PATH,” then hit “Install Now.”
Getting PyCharm Up and Running
When writing code, you need a proper workspace — somewhere to write, run, install libraries, and debug errors. That’s what an IDE (Integrated Development Environment) is for. It’s an application that gives you everything you need to write, test, and troubleshoot code all in one place. For Python, there are several great options like Spyder, Jupyter Notebooks, and Visual Studio. The best choice depends on your skill level, personal preference, and what you’re trying to accomplish. For this tutorial, we’re going with PyCharm — it has a built-in terminal and makes installing libraries a breeze, which is ideal for beginner projects.
Grab the IDE from this link:
Just pick the “Community Edition” and download the version that matches your operating system.

With PyCharm installed, let’s move on to setting up our project.
Creating the Project and Python File
Now we’ll set up our project in PyCharm. Think of a project as a folder that holds everything — your Python code files, libraries, environment settings, and more. Here’s the process: open PyCharm, create a new project, pick a location for it, and you’re set. Then we’ll create a Python file called main.py that will hold all our main code. Once it’s ready, you can verify everything works by writing a quick test line and running it.

print("Welcome to my new project on AI Agents")
The screenshot above shows the project name, its location on your system, the test code we used, the run button for executing it, and the output at the bottom. If you’ve made it this far, everything is working perfectly!
Setting Up the Environment File
Next, we’ll create an environment file. These files are used to securely store sensitive project information and are typically named .env. They’re perfect for keeping API keys, passwords, and configuration settings safe, which makes your project more secure and professional. In this project, we’ll create an environment file to store our API key (we’ll cover APIs in more detail shortly).

As shown, we’ve created a new file called environment. This file will be used to securely store the API key for our project in the variable API_KEY (the API key has already been added and hidden for security). Later, we will install and import the dotenv Python library, which will enable our program to read sensitive data from a .env file, such as our API key.
Generating the API Key
Our next step is to create an API key to use in our code. First, let’s understand what an API key actually is!
API stands for Application Programming Interface. It’s essentially a collection of rules and protocols that enables two separate software systems to interact and share information. Think of an API like a waiter at a restaurant who acts as a go-between for customers and the kitchen. Customers place orders through the waiter, who delivers them to the kitchen. Similarly, in programming, one software application sends requests to another through an API. Weather applications use APIs to pull real-time weather data from dedicated servers. For our AI Agent project in Python, we’ll use APIs to connect to pre-built AI models and leverage their capabilities within our program.

To connect our program to an AI model, we need an API key, which grants permission for this interaction to occur. There are several platforms online where you can obtain API keys to access AI models, some free and others paid. For this project, we’ll use OpenRouter, a unified platform for LLMs and AI models. After creating an account, we can easily generate and use an API key for free. We’re choosing OpenRouter over alternatives like Google Gemini or OpenAI because it’s free and gives us the flexibility to select any AI model we want with a single API key. It also provides beginner-friendly models that don’t require heavy computing power.
To create your API key on OpenRouter, visit their official website and set up your account. Once you’re logged in, navigate to the OpenRouter dashboard and click on “Get API Key”.


Click the “+ New Key” button to generate your API key. Assign a name for the project when prompted. Once you’ve accessed the key, copy it and paste it into the API_KEY variable in your .env file that we set up earlier. Remember, this key should never be shared publicly!
Setting Up Required Dependencies
Now that our API key is created and stored safely in the .env file, let’s return to our main.py file and begin writing code. The first step is to install and import the necessary dependencies and packages. We’re working with Python, which comes with basic built-in functions and tools. However, to extend its capabilities, we need additional powerful tools that aren’t included in the standard Python library. To achieve this, we use external Python packages and libraries by first installing them in our Python environment and then importing them into our code.
For this project, we need Python to communicate with pre-built AI models, send requests, and handle responses. Since these capabilities aren’t in the standard Python library, we’ll install the OpenAI Python package and import it into our code. To install it, go to the terminal in your PyCharm IDE and enter:
pip install openai
Once the OpenAI library is installed, we’ll import it into our main.py file:
from openai import OpenAINext, to access the API key stored in our .env file, we’ll install and import the dotenv Python library, which is built to read data from .env files.
In the terminal (not the Python file), enter the following command to install the dotenv library:
pip install python-dotenvAfter the library is installed, import it just as we did with the OpenAI library. We’ll also import Python’s built-in os library. This library enables Python to interact with the operating system for system-related tasks like accessing files, directories, environment variables, and managing paths. In our project, we’ll use the dotenv library to load the .env file and the os library to extract values from it.
from dotenv import load_dotenv
import osLoading the API Key into the Main Python File
Once the libraries are imported, we’ll read the .env file and retrieve the API key. For this, we’ll use two functions: load_dotenv(), which instructs Python to open and read the .env file, and getenv(), which extracts the specific information we need from that file.
load_dotenv()
api_key = os.getenv("API_KEY")Setting Up the Client
Next, we’ll proceed with setting up the client for our project. The client is essentially an instance of the OpenAI class (for those familiar with OOP) that enables your code to communicate with OpenAI’s servers. It handles authentication and offers a structured method for sending requests to AI models. You can think of it as a messenger that uses an API key for authentication and sends requests to and receives responses from the AI model.
Here is the syntax for initializing the client:
client = OpenAI(
api_key,
base_url="
)We have used a ready-made template from the OpenAI library to create an object called client, using an API key already loaded from the .env file. This key lets the client connect to AI models through the specified URL. In our project, we have selected OpenRouter AI models:
Setting Up the Endless Chat Loop
Next, we will set up an infinite loop that will keep running until we end it manually (or add extra logic to stop it). In Python, this can be done using a while loop, a loop that repeats until a condition is no longer true. In our program, the while loop will keep the chatbot active continuously. Once the AI Agent provides an answer, it will then wait for the next input from the user. In addition to the while keyword, we will include the keyword True, so the loop never ends on its own,
while True:
#Code inside this loop will keep running until manually stoppedGetting User Input & Showing a Processing Indicator
The next step is to capture input from the user. This means taking what the user types as a question for the AI Agent. We will set up a variable named question, where we will save whatever the user enters. To confirm the program is running and not stuck (AI models can be slow to respond), we will show “Thinking…” in the terminal. We will use Python’s print command for this, as seen in the code below. This way, users know their question has been received and is being processed.
question = input("You: ")
print("Thinking...n")Sending the Request to the AI, Picking a Model & Setting Up Prompts
Now that the user’s question is stored in the variable question, we need to connect our program with an AI model. We will use the chat.completions.create() function from the OpenAI Python package to get replies from the AI. The answer from the AI will be saved in the response variable. You can find a list of available models here. I selected baidu/cobuddy:free, as it tends to respond more quickly than other models I have tried. With the model name from OpenRouter set, we can now set up the conversation flow between the user and the AI.
We will save this conversation inside a variable called messages, which is actually a Python dictionary containing keys such as role and content. Python dictionaries store information in key-value pairs.
| Role | System | User |
| Content | You are a helpful educational tutor | question |
Within this dictionary, we define the details for each role, both system and user. For the system, the content for the role reads, "You are a helpful educational tutor", which aligns with our goal of making an AI Educational Agent. The user’s content is the question entered by the user. Here is the code for the previous explanation:
response = client.chat.completions.create(
model="baidu/cobuddy:free",
messages=[
{
"role": "system",
"content": "You are a helpful educational tutor."
},
{
"role": "user",
"content": question
}
]
)After processing, the AI models take into account both the system’s instructions and the user’s question to create a response. The output is stored in the variable response. This is the main part of our project where the AI Agent communicates directly with the model. You may change the model name on the second line if needed.
Extracting the AI’s Reply & Displaying It
Finally, we need to show the AI-generated message. To do this, we take the text of the answer saved in response. The response from the model may include several choices; we’ll select the first by using index [0]. Next, we retrieve message’s content, which is the actual reply. Here is how it’s coded:
answer = response.choices[0].message.content
print("nAI:", answer)
print("n-------------------n")Notice, we have accessed the message dictionary, then printed out the actual stored value for the key “content.”
Running the Code
Now let’s run the code!

You can see in the code working in the image above, and the AI responding to questions. But you may notice the responses are quite slow. This happens because we are using a free model which is shared by many users and might be hosted on slower servers. Still, if you find the response time too slow, try switching to a different AI model from OpenRouter. After some trial and error, you should be able to find a faster option!
Conclusion
In this article, we successfully built an Educational AI Agent that answers questions. We wrote our code from scratch, using several dependencies, and learned how beginners can create projects like this with Python. This was a simple tutorial focusing on the basics and showing that building an AI project really isn’t so difficult with some knowledge of programming fundamentals and the ability to make use of ready-made packages and modules.



