What are AI agents and why everyone is talking about them
You have probably seen the term AI agent popping up in job listings, startup profiles, freelancer projects, and even casual tech conversations. It is the hot topic right now, and for good reason. But maybe you have been thinking: this must be way too complicated for me right now.
The good news is that it is not.
Building an AI agent from scratch is more accessible than it sounds, and in this guide you are going to see it in action, step by step, without needing any prior experience in programming or artificial intelligence. In just a few minutes, you can have a working agent running on your computer. That is exactly what we are going to accomplish here.
Throughout this content, you will understand what sets an AI agent apart from a regular chatbot, how this type of system thinks and makes decisions autonomously, and how to set up your own development environment with Python and a beginner-friendly IDE. We will also walk through creating your API Key on OpenRouter, installing the necessary dependencies, and writing the main project code, which will be an educational AI assistant.
Everything explained in a simple, straightforward, no-fluff kind of way. 🚀
AI agent vs. chatbot: what is the real difference
Before opening any IDE or writing a single line of Python code, it is worth understanding what we are actually building here. An AI agent is not just a system that answers questions. It is a program capable of observing, thinking, deciding, and acting to complete tasks with minimal human intervention. That is what separates it from a traditional chatbot, which simply receives an input and returns an output generated by a language model, with no intermediate reasoning or planning logic whatsoever.
To make this difference more concrete, imagine the following scenario: you want to buy a new laptop for heavy-duty programming. If you ask a conventional chatbot, it will suggest a few models and then wait for you to ask specific questions, one by one. The chatbot works like a text generator that reacts to what you ask, has limited memory, and depends entirely on your guidance.
An AI agent, on the other hand, takes a completely different approach. It receives the goal, researches on its own, compares options, analyzes requirements like performance, price, and portability, and delivers a more comprehensive result. It can build a comparison table, list the pros and cons of each model, mention updated prices, and help you make an informed decision. All of this without you having to manually guide every single step.
In practice, an AI agent operates within a continuous cycle called a reasoning and action loop. It receives a task, analyzes what needs to be done, breaks the problem into smaller parts, chooses available tools or strategies like web search, calculators, or its own memory, executes an action, observes the result, and decides the next step. This process can repeat multiple times until the goal is achieved.
This autonomous behavior is what is making AI agents so relevant in the market. Companies are using this type of system to automate customer service, data analysis, technical support, content creation, and much more. And the foundation of all of this can be built with Python, which is one of the most widely used languages for artificial intelligence development today precisely because of its simplicity and the massive ecosystem of available libraries.
Setting up the environment: installing Python and the IDE
The first real step of the project is getting your development environment ready. If you are a complete beginner, there is a good chance you do not have Python installed on your machine yet. Since this project is entirely based on Python, this installation is required.
Installing Python
Head to the official website at python.org/downloads and download the latest stable version. During installation on Windows, do not forget to check the Add Python to PATH option, because this will make your life a lot easier when running commands in the terminal. After that, just click Install Now and follow the standard flow.
Choosing and installing the IDE
With Python installed, the next step is choosing an IDE, which stands for Integrated Development Environment. It is basically an application that provides a complete space for writing, testing, running, and debugging code. There are several popular options like Spyder, Jupyter Notebooks, and Visual Studio Code, and the ideal choice depends on your experience level, your comfort zone, and the type of project you want to develop.
In the original article for this tutorial, the recommended IDE is PyCharm Community Edition, which is free and offers an integrated terminal and easy library installation, making it a great option for beginner projects. You can download it at jetbrains.com/pycharm/download, selecting the Community version and the download that matches your operating system.
Another very popular and equally solid option for beginners is Visual Studio Code, which is free, lightweight, and has an official Python extension that adds features like autocomplete, debugging, and direct script execution. Regardless of your choice, the important thing is having an environment where you can write and run Python code without complications.
Creating the project and the main file
With the IDE installed, it is time to create your project. In PyCharm, for example, just open the IDE, click on create new project, choose where the files will be saved, and confirm. A project works like a folder that groups different files together: Python code, libraries, configuration files, and more.
Inside the project, create a file called main.py, which will be the heart of your agent. To test if everything is working, write the following code and run it:
print(“Welcome to my AI Agent project”)
If the message shows up in the output terminal, congrats. Your environment is set up and ready for development. 🎉
Creating the environment file and protecting your API Key
Now we are going to create a very important file for project security: the environment file. Environment files, usually named .env, are used to store sensitive information like API keys, passwords, and configurations that should not be directly exposed in the main code. This makes the project more secure and professional.
In your IDE, create a new file in the root of the project and name it .env. Inside it, you will add a line like API_KEY=your_key_here, where the actual value will be filled in during the next step. This file keeps your key out of the main code, which is a best practice because it prevents the key from being exposed if you share the project or push the code to GitHub.
Understanding APIs and creating your key on OpenRouter
For your AI agent to communicate with a language model, it needs an API Key. But first, it is worth understanding the concept behind it.
What is an API
API stands for Application Programming Interface. It is basically a set of rules and protocols that allows two different software systems to communicate with each other. Think of an API as a waiter at a restaurant: the customer places an order, the waiter takes it to the kitchen, and brings the finished dish back. In the programming world, one piece of software sends a request to another piece of software through the API and receives a response back.
Weather apps, for example, use APIs to fetch real-time meteorological data from specialized servers. In our AI agent project, we are going to use APIs to connect with AI models that have already been built and leverage their capabilities within our program.
Creating the API Key on OpenRouter
For this communication to happen, we need an API Key, which works like an access permission. There are several ways to get API keys for online AI models, some free and some paid. In this project, we are going to use OpenRouter, which is a unified interface for various LLMs and AI models. The cool thing about OpenRouter is that, besides being free for many models, it lets you choose any available model using a single API key, and it offers options that do not require high processing power, perfect for beginners.
To create your API Key, go to openrouter.ai and create a free account. After logging in, head to the dashboard and look for the option to create API keys. Click the new key icon, give it a name to identify the project, and copy the generated key. This is the only time the full key will be displayed on screen, so save it somewhere safe before closing the window. This key should not be shared publicly under any circumstances.
With the API Key copied, go back to your IDE and paste the value into the .env file we created earlier, in the API_KEY variable.
Installing the project dependencies
With the API Key ready and safely stored in the .env file, let us go back to the main.py file and get into the part that really matters: the code. Python, on its own, is a language with basic built-in functions and tools. To expand what we can do, we need external packages and libraries that are installed separately and then imported into the code.
Installing the OpenAI library
The first library we need is openai, which provides the communication client for APIs compatible with the OpenAI standard. In your IDE’s integrated terminal, type:
pip install openai
After installation, import it in your main file:
from openai import OpenAI
Installing the dotenv library
Next, we need the python-dotenv library, which is designed to read information from .env files. Install it with the command:
pip install python-dotenv
Then import it in the code along with the os library, which helps Python communicate with the operating system to access files, folders, and environment variables:
from dotenv import load_dotenv
import os
Writing the AI agent code
Now let us get to the core part of the project: writing the logic for the educational AI agent. Each block of code has a specific function, and we are going to walk through all of them.
Loading the API Key in the code
The first step inside main.py is loading the .env file and retrieving the API key. We use the load_dotenv() function to tell Python to open and read the environment file, and os.getenv() to grab the value of the variable we need:
load_dotenv()
api_key = os.getenv(“API_KEY”)
Creating the communication client
Next, we create the client, which is the object responsible for allowing your code to communicate with the AI model servers. Think of it as a messenger that needs an authentication key to send and receive requests and responses. The configuration points to the OpenRouter base URL:
client = OpenAI(api_key=api_key, base_url=”https://openrouter.ai/api/v1″)
OpenRouter accepts the same request format as the OpenAI API, which makes the integration straightforward and hassle-free.
Creating the infinite conversation loop
For the agent to work continuously, like a real conversation, we use a while True loop in Python. This means the program will keep running and accepting new questions until it is manually stopped:
while True:
Receiving user input and displaying status
Inside the loop, we capture the user question with the input() function and display a processing message so they know the system is working on the response, since AI models can take some time to process:
question = input(“You: “)
print(“Thinking…\n”)
Sending the request and configuring the model
Now comes the heart of the agent. We use the chat.completions.create() method from the OpenAI library to generate responses. Here we define the model to be used and the conversation messages. The original article uses the baidu/cobuddy:free model from OpenRouter, which is free and accessible for beginners. You can swap it out for any other model available on the platform if you want to experiment.
The message structure uses a Python dictionary with two keys: role and content. The message with the role system defines the agent behavior, in this case as an educational tutor. The message with the role user contains the question asked by the user:
response = client.chat.completions.create(
model=”baidu/cobuddy:free”,
messages=[
{“role”: “system”, “content”: “You are a helpful educational tutor.”},
{“role”: “user”, “content”: question}
]
)
When this request is processed, the AI model receives both the system instruction and the user question, combines them, and generates a response that is stored in the response variable. This is the main step of the project, where the agent effectively talks to the AI model. 🎯
Extracting and displaying the response
The model response comes in a structured format with different options. We select the first option using the [0] index and access the message content, which is the actual answer:
answer = response.choices[0].message.content
print(“\nAI:”, answer)
print(“\n———————–\n”)
Running the agent and testing it in practice
With the code written, just run the file in the terminal and start interacting with the assistant. If everything is configured correctly, the agent will answer your questions and keep waiting for new inputs in a continuous cycle.
One important thing to note: since we are using a free model, it is very likely that responses will take a bit longer than expected. This happens because free models are shared with many other users and are sometimes hosted on slower servers. If the processing time is too long, it is worth trying a different model on OpenRouter. After a few tests, you will find an option that balances speed and quality.
Try asking chained questions, like asking what a variable is in Python and then requesting a practical example, and notice how the agent generates contextualized responses. That is the magic of working with language models inside an agent framework.
Next steps: how to level up your agent
The project we built here is intentionally simple, focused on the fundamentals. But from here, the possibilities for growth are pretty wide. You can add tools to the agent, like the ability to search the web, read files from your computer, run math calculations, or query databases. You can also maintain a full conversation history so the agent has context memory across messages.
Another interesting upgrade is creating a graphical interface using libraries like Gradio or Streamlit, which turn the script into a web application with a friendly visual layout and no frontend knowledge required. Every improvement you implement is a concrete step toward building more complex systems with real-world value.
The most important thing here is not to arrive at a perfect product right away, but to understand how the pieces fit together: Python as the base language, the IDE as the workspace, the API Key as the bridge to the language model, and the agent logic as the engine that makes everything work together. In this article, we successfully built an educational AI agent from scratch, using accessible dependencies and an approach that shows creating something with artificial intelligence does not have to be intimidating. It all comes down to having a basic understanding of the fundamentals and knowing how to leverage existing packages and modules to do the heavy lifting.
With that understanding, you already have what you need to explore, experiment, and build increasingly interesting things with artificial intelligence. 💡
