Generative AIby Dhruv Narang

What Is an API Key?

Definition

An API key is a secret password that tells a company's API that the request comes from you. Without it, the API does not answer.

One key for each company. You get it from the company's website, in your account.
Never share it. Anyone who has your key can use your account.
Keep it out of your code. Put it in a .env file, and keep that file out of Git.

The .env File

You keep each key in a file named .env, and your code reads it from there.

MISTRAL_API_KEY=your key
GROQ_API_KEY=your key
.env is a plain text file in your project folder. Write the name, then =, then your key. No spaces.
import os
from dotenv import load_dotenv

load_dotenv()

client = Mistral(api_key=os.getenv("MISTRAL_API_KEY"))
load_dotenv() reads the file.
os.getenv(...) gives the key that has that name.
python-dotenv is the package that gives load_dotenv: pip install python-dotenv

Default Key Names

LangChain finds your key by itself when you use its default name in the .env file.

CompanyName in the .env file
OpenAIOPENAI_API_KEY
AnthropicANTHROPIC_API_KEY
Google GeminiGOOGLE_API_KEY  or  GEMINI_API_KEY
GroqGROQ_API_KEY
Mistral AIMISTRAL_API_KEY
With LangChain, write the default name in .env and call load_dotenv() once. LangChain reads the key from that name. You do not pass the key in your code.
A different name? Then LangChain cannot find the key. Use the default name, or pass the key yourself with the api_key setting.

Free Plan Limits

Free plans have limits. When you pass one, the API answers with an error and you must wait.

CompanyModelRequests per minuteTokens per minutePer day
Groqopenai/gpt-oss-20b308,0001,000 requests and 2,00,000 tokens
Mistralopen-mistral-nemo240,000-
Free plan numbers only. They change, so check your own limits on the provider's site: Groq limits ↗ and Mistral limits ↗.
A limit belongs to the account. Everyone who shares one API key shares one limit. Give each student their own key.

Common Key Errors

Three errors you will meet. Each one tells you what to fix.

ErrorWhat it meansWhat to do
401 Invalid API KeyThe API did not accept the key.Check the name in .env, and copy the key again.
403This model is not on your plan. Mistral says: not available in your subscription tier.Pick a model that your plan allows.
429You passed a limit of the free plan.Wait a minute and try again.
To check that your key is found, run this in the project folder. It prints loaded: True and the length of the key, never the key itself.
python -c "import os; from dotenv import load_dotenv; load_dotenv(); k=os.getenv('MISTRAL_API_KEY'); print('loaded:', bool(k), '| length:', len(k or ''))"