OpenAI API key not set error
Why this matters
You cannot build any llamaindex application that uses OpenAI models without first configuring your API key: this is the first blocker developers hit.
Explanation
The openai.error.AuthenticationError: No API key provided error occurs when llamaindex tries to call an OpenAI model but cannot find your API credentials.
Mechanically, when you initialize an OpenAI model in llamaindex (or don't initialize one and it defaults to GPT-3.5), the SDK looks for your key in this order: (1) the OPENAI_API_KEY environment variable, (2) a .env file, or (3) directly passed to the OpenAI() constructor. If none exist, the API call fails with an authentication error.
The fix is to provide your key before any LLM call happens: either set the environment variable, load it from a .env file, or pass it directly to the OpenAI constructor in Settings.
Analogy
Your API key is like a door badge. Llamaindex is trying to enter the OpenAI building, but the security guard (OpenAI's servers) won't let it through because it can't see a valid badge.
Code
import os
from dotenv import load_dotenv
from llama_index.core import Settings
from llama_index.llms.openai import OpenAI
load_dotenv()
api_key = os.getenv('OPENAI_API_KEY')
if not api_key:
raise ValueError('OPENAI_API_KEY environment variable is not set')
Settings.llm = OpenAI(model='gpt-4.1', api_key=api_key)
response = Settings.llm.complete('Say hello')
print(response) Hello! How can I assist you today?
What just happened?
The code loaded the OPENAI_API_KEY from the environment (via .env file using python-dotenv), validated it exists, passed it explicitly to the OpenAI constructor, set it globally in Settings, then made a test LLM call that succeeded and printed the response.
Common gotcha
Many developers set the environment variable in their terminal session, but it doesn't persist to their IDE or new shell. They restart their IDE and suddenly get 'API key not set': the environment variable is gone. Solution: use a .env file instead, which gets loaded by python-dotenv regardless of shell session.
Error recovery
AuthenticationError: No API key providedAuthenticationError: Invalid authentication credentialsRateLimitErrorExperienced dev note
Never hardcode your API key in source code or commit it to git. Even if you immediately revoke it, it's in the repo history forever. Use environment variables + .env files (with .env in .gitignore). For production, inject keys via environment variables in your deployment platform (Docker ENV, Kubernetes secrets, Lambda environment variables): never check them in.
Check your understanding
If you set OPENAI_API_KEY in your terminal and it works, but your coworker clones your repo and gets 'API key not set' on the same code, what's the issue and why?
Show answer hint
The answer involves understanding that environment variables set in one shell session don't transfer to another user's shell, and the solution requires a .env file that loads the key for any environment. The coworker needs their own OPENAI_API_KEY in their environment or a shared .env (which should not be committed, so they need to create it locally).