Introduction to AI Agents
of the decade. You hear it everywhere on job descriptions, tech companies’ profiles, freelancers’ projects, etc. As overwhelming as it may sound, building an AI Agent is not that difficult. On the contrary, you can easily build a simple AI Agent in a couple of minutes. This is what we will achieve in this article.
In this article, we will go through the step-by-step process of building an AI Agent. You don’t need any preliminary knowledge, as we will explain each part of the project in simple, beginner-friendly terms. We will also provide a step-by-step guide to installing Python and the relevant IDE where we will build this project. This will serve as a dedicated AI agent tutorial for the very beginners in the field of programming, coding, and AI.
What are AI Agents?
But first, what exactly are AI Agents? AI Agents are software programs that are able to not only answer specific questions like simple chatbots, but they go a step further. They are able to answer questions and make autonomous decisions, as well as create things and get tasks done! They can observe, think, decide, and act to complete tasks with minimal human input. Suppose we want to buy a new laptop for heavy programming. We can ask the same question to both a chatbot and an AI Agent. The chatbot approach will be to suggest laptops for heavy programming and then respond to specific questions one by one. It waits for user input, has limited memory, and works mostly as a text generator. An AI Agent, on the other hand, takes goals and performs tasks automatically without the need to explicitly ask/direct to a specific feature. It researches, compares, plans, and analyzes requirements to make research-backed decisions. For our heavy programming laptop question, the chatbot will just answer in a single line, but the AI Agent will give us a comparison table, mention different products, their pricing, and pros and cons, and aid us in making the decision.
How does an AI Agent work?
The AI Agent is a smart program that is coded to fulfill a goal. Once we give it a task, the AI Agent first receives the request, breaks it down into smaller problems to address, and takes further input from the user if required through questions to properly understand and meet all requirements. It then uses appropriate tools like web searching, calculators, and its own memory to collect additional information, and analyzes this information carefully. It compares different options and curates the answer to the user’s needs.

Now that we know what AI Agents are and how they work, let us start coding our own personalized AI Agent.
Building an AI Educational Agent in Python
In this article, we will build an AI Educational Agent that will act as your personal education assistant.
Before we begin the coding and explanation, let us make sure that we have our platform requirements fulfilled:
Installing Python
If you are a complete beginner, chances are that you have never installed Python on your system. This is a project based on Python, so we need to install it on our system. Click on this link, and follow the steps.
During installation, check the box: “Add Python to PATH”, then click “Install Now”.
Installing and Setting up PyCharm
Whenever we are coding, we need a suitable platform or workspace that allows us to write code, run the code, install relevant libraries and packages, and debug our code for errors. This is where IDE, which stands for Integrated Development Environment, comes into play. An IDE is an application that provides a platform or workspace for writing, testing, and debugging code. For Python coding, we can use a number of IDEs like Spyder, Jupyter Notebooks, and Visual Studio, to name a few. The choice of using a specific IDE should be dependent on your proficiency in coding, your comfort zone, and, most importantly, your domain and what you want to achieve through your coding. In this tutorial, we will use PyCharm as our coding environment, as it facilitates an in-built terminal and easy library installation, perfect for beginner projects.
You can install the IDE from the following link: https://www.jetbrains.com/pycharm/download
Simply choose “Community Edition” and select the download option particular to your operating system.

Once PyCharm is installed, let us move forward to creating our project file.
Setting up the Project and Creating the Python File
Next, we will create our project file in PyCharm. A project in PyCharm is like a folder that can have within it different files: Python code files, libraries, an environment file, etc. The way we will go forward is first launch PyCharm, create a new Project, choose the location of your project, and create the Project. Next, we will create a Python file, main.py which will contain the main code. Once the file is created, you can test your installation by writing a generic code and running it.

print("Welcome to my new project on AI Agents")

You can see in the above screenshot the project name displayed, the location of the project, the generic code used for testing, the run button to execute the code, and lastly the output of the code. If you can get here, you have everything running fine!
Creating the Environment File
Now, we will create a new file, which will be the environment file. Environment files store secret information safely for the project and are usually named as .env. It is used to save keys, passwords, and configuration settings for our project, making our project more secure and professional. In this project, we will create an environment file and store our API key in it (more about APIs later).

As can be seen, we have created a new file named environment. It is in this file that we will safely store the API Key for this project in the variable API_KEY (I have added the API key already and hidden it). We will later install and import the dotenv Python library that helps our program read secret information from a .env file, in our case, the API key.
Creating the API Key
Now the next task is to create an API Key to use in our code. But first, let us understand what an API Key is!
API stands for Application Programming Interface. It is a set of rules or protocols that allow two distinct software systems to communicate with each other. We can share information from one program to another by using an API that connects them both. You can understand this as a waiter in a restaurant that acts as an intermediary between the customers and the kitchen. The customers send an order to the kitchen for a particular dish, and this is accomplished through the designated waiter. In the programming world, one software application sends a request to another software application through the API. Weather apps use APIs to get live weather data from relevant weather servers. In our project of building an AI Agent in Python, we use APIs to connect with already built AI models and use their features in our program.

In order for our program to connect with an AI model, we need an API key. The API key gives permission for this communication to happen. Now there are a number of ways to get API keys online and access AI models. Some of these ways are free, others are not. In this project, we will be using OpenRouter which is a unified interface for LLMs and AI Models. We can easily create an API key and use it in our projects for free once we have created the account. The reason why we are using OpenRouter instead of other AI model platforms like Google Gemini, OpenAI, etc, is that not only is it free, but it also allows us to choose any AI model of our choice using that API key. It also facilitates beginners with models that do not require high computing.
Now, to create the API key in OpenRouter, go to their official website, open up your account. Once the account is created, go to the OpenRouter dashboard and click on the “Get API Key”.


Click on the “+ New Key” icon to create your API key. Specify the project. Once you have accessed the key, copy it and paste it into your env file API_KEY variable that we created before. This key should not be shared publicly anywhere!
Installing the Relevant Dependencies
Now that our API key is created and safely secured in the .env file, let us go back to our main.py file and start coding. The first thing is to install and import the relevant dependencies/packages. We are doing this project in Python, which is just a coding language with basic inbuilt functions and tools. But in order to expand our functionalities, we need some more powerful tools and functions that the Python standard library does not provide. It is for this reason that we make use of other Python packages and libraries, by first installing them in our Python system and then importing them in our code.
In this project, we need Python to communicate with already built AI models, send requests, and process requests. Since these functionalities are not available in the standard Python library, we will install the OpenAI Python library and then import it into our code. To install, go to the terminal icon on your PyCharm IDE and then type:
pip install openai

Once the OpenAI library is installed, we will import it into our main.py file:
from openai import OpenAI
Next, in order to access the API in our .env file, we will install and import the dotenv Python library that is designed to read information from .env files.
In the terminal (not the Python file), write the following code for installation of the dotenv library.
pip install python-dotenv
Now that the library is installed, import it as we imported the OpenAI library. We will also import the Python os library. This library helps Python communicate with the operating system to manage system-related tasks, access files, folders, and environment variables, and create paths. In our project, we will use the dotenv library to load the .env file and os library to retrieve the values from it.
from dotenv import load_dotenv
import os
Loading the API Key in the Main Python File
Once importing libraries is completed, next we will read the .env file and retrieve the API key. For this purpose, we will use two functions: load_dotenv(), which tells Python to open and read the .env file, and getenv(), which retrieves the information we need from that file.
load_dotenv()
api_key = os.getenv("API_KEY")
Creating the Client
We will move forward with building the client for our project. The client is basically an object of the OpenAI Class (in case you know about OOP) that allows your code to communicate with OpenAI’s servers. It facilitates authentication and provides a structured way to send requests to AI models. We can consider it the messenger that requires an API key for authentication purposes and sends and receives requests and responses to and from the AI model.
Here is the syntax of the client initialization:
client = OpenAI(
api_key,
base_url="https://openrouter.ai/api/v1"
)
We have used a ready-made blueprint from the OpenAI library to create an object client that takes an API key that we have already retrieved from the .env file. This key will allow the client to communicate with the AI models through the URL that we have provided. In our case, we have chosen OpenRouter AI models: https://openrouter.ai/api/v1
Creating the Infinite Chat Loop
Next, we will create the infinite loop that will keep going on until we stop it manually (or we can add additional functionality). In Python, this infinite loop can be achieved with a while loop, which is basically a loop that repeats again and again until a condition becomes false. In our project, the while loop will be used to keep the chatbot running continuously. So once the AI Agent has answered a question, it will ask the user for the next prompt. Along with while keyword, we will add the keyword True so the loop will never stop automatically,
while True:
#Code inside this loop will keep on running until manually stopped
Taking Input from the User & Showing Processing Status
The next task is to take input from the user. This is basically what the user will ask the AI Agent. We will create a variable called question, inside of which we will store the input from the user. Then, in order to show the processing status, or that the program is actually running in the background (how slowly though), and is not frozen, because AI models do take processing time, we will display the line “Thinking…” in the output. We will use the Python print function for this purpose, as shown in the code block below. In this way, the user will know that their input question has been received and is now being processed.
question = input("You: ")
print("Thinking...\n")
Sending the AI Request, Selecting Model & Message System
Now that the user has asked the question, and it has been stored inside the variable, question the next task is to enable the communication of our program with an existing AI model. We will use the chat.completions.create() method in the OpenAI Python library to generate responses from the AI models. The answer to the user’s question after effective communication will be stored in the variable response. We will select a model from this link. I have used the model baidu/cobuddy:free because of it being faster than others I previously used. Once we have specified the model name from OpenRouter, we will then work on the conversation between the user and AI.
We will store this conversation in the variable messages, which is actually a Python dictionary having keys: role and content. The way Python dictionaries work is that we have keys, and values associated with these keys.
| Role | System | User |
| Content | You are a helpful educational tutor | question |
Inside our dictionary, we will define the content for both roles, system and user. For the system, the content of the role is "You are a helpful educational tutor" that achieves our goal of building an AI Educational Agent. The user’s content is the question which the user will ask. Let us code the above scenario:
response = client.chat.completions.create(
model="baidu/cobuddy:free",
messages=[
{
"role": "system",
"content": "You are a helpful educational tutor."
},
{
"role": "user",
"content": question
}
]
)
Whenever the above is processed, the AI models will take the user’s question and the system’s content together and generate answers combining both of the above. The generated answer is returned in the variable response. This is the main step of our project where our AI Agent is actually talking to the AI model. We can change the model name from the second line.
Extracting the AI Response and Printing it to the User
Next, we need to output/print the AI-generated text. To do this, we will take the complete generated answer that was stored in the response variable. The response from the AI model will have different choices we can choose from. We will choose the first response by giving it the index [0]. Next, we will access the message’s content, which is the actual answer from the AI. Coding this would look like this:
answer = response.choices[0].message.content
print("\nAI:", answer)
print("\n-------------------\n")
Notice that we have accessed the dictionary message, and then further printed out the value stored against the key “content“.
Running the Code
Now let us run the code!

You can see the code working in the image above, and the AI responding to questions. But you will very likely find that the answers generated are very slow. This is because we have used a free model in our project, and they are used by others as well, and sometimes it might be hosted on slow servers. Nevertheless, if the processing time is too long, consider changing the AI model from OpenRouter. You will be able to fund a good fast one after some hit and trial!
Conclusion
In this article, we have successfully created an Educational AI Agent that responds to our questions. We have coded the project from scratch, with the help of certain dependencies, and have seen how we can code such projects in Python as beginners. This was a very easy tutorial that employed the very basics and showed us that building an AI is not that hard after all. It comes down to having a very basic knowledge of the fundamentals and the ability to use already created packages and modules to get the work done for us.









Leave a Reply