Code Beginner easy · 3 min

OpenAI API key not set error

What you will learn
Set your OpenAI API key so llamaindex can authenticate with GPT models.

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.

Skip if: You don't need to set an OpenAI key if you're using a different LLM backend (Anthropic, Ollama, local models) or if you're only working with embeddings from a non-OpenAI provider.

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

python
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)
Output
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 provided
OPENAI_API_KEY environment variable is not set or not loaded. Add it to a .env file in your project root (OPENAI_API_KEY=sk-...) and call load_dotenv() before initializing OpenAI, OR pass api_key directly to OpenAI(api_key='sk-...').
AuthenticationError: Invalid authentication credentials
Your API key is set but is invalid, expired, or has been revoked. Go to https://platform.openai.com/account/api-keys and generate a new key, then update it in your .env file or environment.
RateLimitError
Your API key is valid but you've hit rate limits. This is not a missing key error: it means the key works but you're making too many requests. Back off requests or upgrade your plan on the OpenAI dashboard.

Experienced 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).

VERSION In llama-index < 0.10.0, the pattern was ServiceContext with LLMPredictor. In llama-index-core >= 0.10.0, use Settings.llm = OpenAI(): the old pattern is removed.
NEXT

Now that authentication works, learn how to use VectorStoreIndex to create your first retrieval-augmented generation (RAG) pipeline.

Community Notes

No notes yetBe the first to share a version-specific fix or tip.