Skip to content

How to Configure AI Model Settings for Your First Python Program

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=Ollama
    

    Note: 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.2
    

    This 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.

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 LunaNotes

Or 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.”
Bhai Chrome Web Store review

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

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

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

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

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)

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.

Found this summary useful?

Take it with you. One click puts it in your own LunaNotes library.

Save to LunaNotes

Start taking better notes today with LunaNotes