Setting Up Your AI Model Configuration
Before writing any code, you need to create a configuration file that tells your Python program how to connect to the AI model. This approach keeps your code flexible and reusable.
Step 1: Create the .env File
Inside your project folder (AI agent development), create a new file named exactly .env. The dot at the beginning is important.
Step 2: Add the Three Required Properties
Your .env file needs three lines:
-
BASE_URL: The URL where your AI model is running. For local models using Ollama, this is:
BASE_URL=http://localhost:11434"localhost" means your own computer.
-
API_KEY: A security key for accessing the model. Since you're running locally with Ollama, no real key is needed. Use a generic placeholder:
API_KEY=OllamaNote: If you use a cloud AI service, replace with the actual API key from that provider.
-
MODEL: The specific model name you want to use. For example:
MODEL=llama3.2This tells Python which AI model to load. You can change this later to switch models without modifying your code. If you haven't installed Ollama yet, check out our guide to Install Ollama and Run Qwen3 4B Locally – AI Model Setup Guide.
Why Configuration Files Matter
By using a .env file instead of hardcoding values:
- Flexibility: Change models, API keys, or URLs in one place
- Security: Keep sensitive keys out of your code (important for cloud services)
- Reusability: Share your code without exposing credentials
- Portability: Switch between local and cloud AI models easily
Key Points to Remember
- Create the file inside your project folder, not in a subfolder
- Use exactly these property names:
BASE_URL,API_KEY,MODEL - For local Ollama, the base URL always points to
localhost:11434 - The API key is not used locally but must be present as a placeholder
- Model name can be any model you have installed locally
- Your Python program will read this file automatically to configure the AI connection
This configuration approach means you only need to change these three lines to switch AI models or switch between local and cloud-based AI services. For a complete development environment setup, see our guide on Free AI Development Setup: Install Python, VS Code & Ollama Tools. If you're new to how these systems interact, our overview on How AI Models Work: Cloud vs. Local (Free Ollama Setup) provides helpful context. Once your configuration is ready, you can build on this foundation by learning Understanding APIs and Building Your First AI Assistant in Python.
Now, everything is set up. Now, we will write our first AI program. So, create your first
Python file. So, inside the folder, which is basically AI agent development, you
create a new file named hello_ai.py. py is the extension for the Python code. So, this is the name which you will give
it. Once you give this name, your Visual Studio Code immediately knows that you are trying to write a Python program.
So, if the extension is not installed, on the right-hand corner, you will see that it will pop up a message that do
you want to install Python extension? And you click yes, then it will be downloaded and installed.
So, right now, don't worry about creating a chatbot.
Our goal is much smaller. We will start a small, we will make big. We simply want to ask one question and
display one answer. So, we'll just ask one question and we will display one answer. We are not making the entire
chatbot. So, first, creating the configuration file.
Before writing any code, let's create one more file. Inside the folder, create this file,
.env. So, create this file exactly with the name .env. Don't change the name.
Add the following contents. The first one is the this base URL is equal to
this. Actually, this is the place where your AI model is
running. This is the URL. Local host means my own computer. Local host means my own computer.
So, since Ollama, that means your AI model is currently running on this, you write
this base_url is equal to this entire URL. So, this is the first line which you write in this
file. Second line is you will write API_key. This is the properties which you will
set the value as Ollama. So, we will set this the value of AI API_key
as Ollama. What is this API_key? When you use the AI model which is provided by the server on the cloud, and
since it is payment-based, they give you a key so that you can only use that key value
to access the AI model. That is a security key. That is called API key. So, you have to provide that. But, in
our case, our model is itself onto the local computer. Therefore, we do not require API key. But, since this
parameter is required, we will use a generic name just like Ollama. So, that is clear.
So, since it is required, we will use a generic one like Ollama. But, if you want to use the
AI model on the cloud, [clears throat] then the API key provided by that service provider has to be used here.
And the third thing is the name of the model. You may have many models on your
computer or on the cloud. So, you have to provide the name of the model which you want to
use. So, this is the name of the model. Model is the name of the properties, and this is the value of that properties.
This tells the Python which AI model to use. Today, we are using this. Tomorrow, if you want to use another model, you
only need to change this one line. No code changes. You have to just change this one line, and nothing need to be
changed. So, just to summarize, inside the folder, create this file .env, and write
these three lines which you have I have already explained to you. This is only to set up the AI model so
that your Python program is able to communicate with the AI model. AI model is on your computer, or it may be on the
cloud server. Your Python program has to communicate with the AI model to get the responses, etc. Therefore, we are
setting up these three properties. One is the URL from where the AI model has to be accessed,
and the API key value. Since our case there is no API key value required, we will just put the Ollama, and then the
name of the model. So, these three value has to be configured. Remember what I told you earlier
that our AI assistant or our AI agent need not to be changed
even if you are using any different AI model. Only this information need to be changed. Only these three lines needs to
be changed. Rest of your program will remain same. That is the advantage of configuring this information rather than
hard coding it into your program. I hope it is clear to you. Thank you.
Using a .env file enhances flexibility, security, and portability. You can change models or API keys in one place without editing code, keep sensitive credentials out of your source files (important for cloud services), and easily switch between local setups (like Ollama) and cloud-based AI services.
You need to define BASE_URL (the endpoint where your AI model runs, e.g., http://localhost:11434 for local Ollama), API_KEY (a security token; use 'Ollama' as a placeholder for local use), and MODEL (the specific model name, such as 'llama3.2'). These three lines allow your Python program to connect and interact with the AI model.
For local models using Ollama, set BASE_URL=http://localhost:11434. 'localhost' directs the program to your own computer. If you later switch to a cloud service, replace this URL with the provider's endpoint.
For local Ollama, use a generic placeholder like API_KEY=Ollama since no real authentication is required. If you use a cloud AI service, replace this with the actual API key provided by that service to ensure secure access.
Yes, by changing only the MODEL property in your .env file (e.g., from 'llama3.2' to another installed model), you can switch models instantly. Your Python code reads this file automatically, making it reusable across different AI models.
Ensure the file is named exactly '.env' with a leading dot and placed in your main project folder, not a subfolder. Use the exact property names (BASE_URL, API_KEY, MODEL) without spaces or quotes around values. Forgetting these details can cause your program to fail to read the configuration.
By storing all connection details in the .env file, you can switch from local Ollama (using localhost:11434) to a cloud service by simply updating the BASE_URL and API_KEY lines. Your Python code remains unchanged, allowing seamless portability across different environments.
Keep this summary
Save it to LunaNotes and it becomes a real note in your library — editable, searchable, and ready to turn into flashcards or a diagram. Free to start.
Save to LunaNotesOr summarise for another video.
32K+ people have turned 65K+ videos into notes
“Very useful tool. I'm using it for the last couple weeks and I'm loving it so far. Makes learning on YouTube a great experience.”
This summary and transcript were automatically generated using AI with the Free YouTube transcript summary tool by LunaNotes.
Related summaries
How to Write Your First AI Program in Python with Ollama
Learn to write a simple AI-powered Python program that communicates with a local AI model (Ollama). This step-by-step tutorial covers importing libraries, loading environment variables, connecting to the AI, sending a question, and displaying the response.
Install Python, VS Code, and Ollama for AI Agent Development
This video provides a step-by-step guide to setting up your development environment for building AI agents. Learn how to install Python, download and configure Visual Studio Code, and set up Ollama with large language models.
Free AI Development Setup: Install Python, VS Code & Ollama Tools
Learn how to set up a complete local AI development environment using three free tools. This guide covers Python installation, Visual Studio Code setup, and Ollama for running AI models on your own computer — no GPU or cloud costs required.
Install Ollama and Run Qwen3 4B Locally – AI Model Setup Guide
Learn how to install the Ollama AI framework on your computer, download the lightweight Qwen3 4B model, and run it locally via terminal. This step-by-step tutorial lets you chat with an open-source LLM right on your own laptop—no cloud or GPU required.
How AI Models Work: Cloud vs. Local (Free Ollama Setup)
Discover how an AI assistant processes questions like 'What is machine learning?' in four simple steps. Learn the difference between cloud-based AI models and free local alternatives like Ollama, perfect for hands-on learning without API costs.
Most viewed summaries
A Comprehensive Guide to Using Stable Diffusion Forge UI
Explore the Stable Diffusion Forge UI, customizable settings, models, and more to enhance your image generation experience.
Kolonyalismo at Imperyalismo: Ang Kasaysayan ng Pagsakop sa Pilipinas
Tuklasin ang kasaysayan ng kolonyalismo at imperyalismo sa Pilipinas sa pamamagitan ni Ferdinand Magellan.
Mastering Inpainting with Stable Diffusion: Fix Mistakes and Enhance Your Images
Learn to fix mistakes and enhance images with Stable Diffusion's inpainting features effectively.
Pamamaraan at Patakarang Kolonyal ng mga Espanyol sa Pilipinas
Tuklasin ang mga pamamaraan at patakaran ng mga Espanyol sa Pilipinas, at ang epekto nito sa mga Pilipino.
How to Install and Configure Forge: A New Stable Diffusion Web UI
Learn to install and configure the new Forge web UI for Stable Diffusion, with tips on models and settings.
Found this summary useful?
Take it with you. One click puts it in your own LunaNotes library.
Save to LunaNotes