API keys, costs and the free ways to start.
An LLM API lets your own code send prompts to a model and receive text, tool calls or structured output, authenticated with a key and billed per token. OpenAI's API is the reference most tutorials use. Gemini and Groq offer free tiers for development, and open models through Ollama cost nothing. Never commit a key to git.
- Auth
- A secret key in an environment variable
- Billing
- Per input and output token; prepaid credits
- Free routes
- Gemini and Groq free tiers; Ollama locally
- Rule
- Keys never in code, never in git, never in the browser
- 5production AI apps
- 14engineers led
- 200+students mentored
Questions students ask
The API questions searched most, answered from running several providers in production.
What is OpenAI API?
The OpenAI API is a web service through which programs call OpenAI's models directly, without the ChatGPT interface. You send an HTTP request with your messages, the model name and options such as tools or an output schema, and receive the model's reply as JSON, optionally streamed. It also covers embeddings, image generation, speech, transcription and fine-tuning. Developers use it through official libraries in Python and JavaScript. It is separate from a ChatGPT subscription: the API has its own account, billing and keys, and using ChatGPT does not include API access.
How to use OpenAI API
- Create an account on the developer platform, add a payment method or prepaid credits, and generate a secret key.
- Store the key in an environment variable or a local env file that is listed in .gitignore; never paste it into code.
- Install the official library in a virtual environment.
- Send a first request: a system message, a user message, a model name; print the reply.
- Add what your app needs: streaming for chat, tool definitions for actions, a JSON schema for structured output, temperature settings.
- Handle errors and rate limits with retries and backoff; log token usage per request so you can see cost.
Where to find ChatGPT API key
Sign in to the developer platform (not the ChatGPT app), open the API keys section, and create a new secret key, giving it a name and, ideally, a project and restricted permissions. The full key is shown once; copy it into your environment configuration immediately, because it cannot be viewed again. Set a monthly spending limit in the billing settings before you write any code, so a bug in a loop cannot run up a bill. Rotate the key if it is ever exposed, and create separate keys per project so one can be revoked without breaking the others.
What is OpenAI API key?
The key is a long secret string that identifies your account to the API and authorises charges to it. Anyone who has it can spend your credits and read nothing else, but spending alone is damage enough. It belongs in server-side code only: a backend reads it from the environment and makes the model call; the browser or mobile app talks to your backend, never to the provider directly. Keys committed to public repositories are found by scanners within minutes and used. If that happens, revoke the key in the dashboard at once and check the usage page.
Why is my OpenAI API key not working?
- No credit: new accounts need prepaid credits or a payment method before requests succeed; check the billing page.
- Wrong account or project: the key belongs to a different organisation or project than the one with credits.
- Not loaded: the environment variable is unset in the shell or process running your code; print its length (not its value) to check.
- Hidden whitespace or quotes copied with the key.
- Revoked or rotated key; create a new one.
- Rate limits or quota errors: too many requests per minute for your tier; add backoff.
- Model name wrong or not available to your account.
How much OpenAI API cost
Pricing is per million tokens, different for input and output, and different for each model; reasoning models and larger models cost more, small models cost a fraction, and cached input tokens are discounted. The exact numbers change often, so take them from the pricing page when you estimate. The method: count a typical request's input and output tokens, multiply by the rates, multiply by expected requests per day. A short chat exchange on a small model costs a tiny fraction of a cent; a long document through a top model can cost several cents. Set a spending cap, log usage, and use prompt caching and smaller models where quality allows.
How to use OpenAI API for free
There is no permanent free tier for the OpenAI API; it runs on prepaid credits, and promotional credits appear only occasionally. For free development there are three honest routes. Use a provider with a free tier, such as Google's Gemini API through AI Studio or Groq's free tier, both of which have rate limits suited to learning. Run open models locally with Ollama and point the OpenAI-compatible client library at the local server, which costs nothing and works offline. Or buy a small amount of credit and keep prompts short; a few dollars of credit goes a long way on small models for a student project.
What is OpenAI API usage credit?
Usage credit is the prepaid balance on your API account that requests draw down from. You buy credits in advance; each request's token cost is deducted; when the balance reaches zero, requests fail until you top up. Promotional or granted credits work the same way with an expiry date. The usage page shows spending by day, model and project, which is the first place to look when a bill surprises you. Set alerts and a hard monthly limit; for a team, create a project per application so each one's spend is visible on its own.
How to get Gemini API key
Go to Google AI Studio, sign in with a Google account, and create an API key; it can be tied to a new or existing Google Cloud project. The free tier works immediately with rate limits per minute and per day that are adequate for development; enabling billing on the project raises limits and moves you to paid rates. The Gemini client libraries and an OpenAI-compatible endpoint both accept the key. The same rules apply: environment variable only, never in a browser, restrict the key where the console allows, and rotate it if exposed.
How to get Groq API key
Create an account on Groq's developer console, open the API keys page and generate a key. Groq serves open models (Llama, Qwen, gpt-oss and others) on its own inference hardware with very low latency, and its free tier has per-minute and per-day limits that are generous for prototypes. The endpoint is OpenAI-compatible, so the OpenAI client library works by changing the base URL and key. DocuLens AI, my document extraction system, runs on Groq-hosted Llama for exactly this combination of speed and cost. Note that Groq, the inference company, is unrelated to Grok, the assistant from xAI.
First API call, safely
Ten minutes, zero leaked keys.
- 01
Get a key
Gemini or Groq free tier for a free start; OpenAI with a small credit and a spending cap.
- 02
Store it right
An env file in .gitignore; load it at startup; print only its length to confirm.
- 03
One request
System and user messages, a small model, print the reply and the token usage.
- 04
Swap providers freely
Use an OpenAI-compatible client so switching to Groq, Gemini or Ollama is a base URL change.
In the room
Where these systems get explained to students.


Read next
Providers, costs and deployment.
API bills you can predict?
I set up providers, caching, routing and spend controls so costs stay boring.

