Python Setup for LLM Development: Virtual Environments, API Keys, Environment Variables and Your First LLM Call

LLM Development 55 min min read Updated: Nov 21, 2026 Beginner
Python Setup for LLM Development: Virtual Environments, API Keys, Environment Variables and Your First LLM Call
Beginner Topic 1 of 3

What You Need

ItemWhy
Python 3.10 or newerMost LLM libraries need a recent Python. Check each library's page
A code editorVS Code or similar
A terminalTo run commands
An API keyLets your code call a hosted model
Git (optional)To track changes

Step 1: Create a Virtual Environment

A virtual environment keeps each project's packages separate so projects do not break each other.

bash python -m venv .venv source .venv/bin/activate # Windows: .venv\Scripts\activate pip install --upgrade pip pip install python-dotenv requests

Step 2: Protect Your API Key

An API key is like a password that also costs money when used. Never paste it into code that you share or commit to Git.

  • Store it in an environment variable
  • For local work, put it in a .env file and add .env to .gitignore
  • Use a different key for each project so you can delete one safely
  • If a key leaks, revoke it in the provider's dashboard immediately
text # .env (never commit this file) LLM_API_KEY=your-key-here

Step 3: Read the Key in Python

python import os from dotenv import load_dotenv load_dotenv() # reads .env key = os.environ.get("LLM_API_KEY") if not key: raise SystemExit("Set LLM_API_KEY first")

Here is a safe way to show a key without leaking it. It prints only the first and last characters.

python def mask(key): if len(key) < 10: return "(too short to show)" return key[:3] + "..." + key[-3:] print(mask("sk-demo-1234567890abcdef")) print(mask("short"))
Result

sk-...def | (too short to show)

Step 4: Your First Call

Every provider has its own library, but the pattern is the same: create a client, send messages, read the reply. Replace the model name with one from your provider's documentation.

python # Example shape using the Anthropic Python SDK (pip install anthropic) import anthropic client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY reply = client.messages.create( model="YOUR_MODEL_NAME", max_tokens=200, messages=[{"role": "user", "content": "Explain an API in one sentence."}], ) print(reply.content[0].text)
Other providers OpenAI style libraries use client.chat.completions.create(model=..., messages=[...]). Many open model servers copy that style, so one code pattern works in many places.
Costs Set a spending limit or alert in your provider account before you test in loops.

Practice

  1. Create the virtual environment
  2. Add .env to .gitignore
  3. Make a first call and print the answer
  4. Try asking the same question twice and compare

Conclusion

A clean environment and a protected key are the base of every LLM project.

Quick Summary: Virtual env; env variables; .gitignore; masked printing; first call.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators