How to Get an LLM API Key (OpenAI and Gemini) and Understand the Cost
Where to create an OpenAI or Gemini API key, how token-based billing works, how to estimate cost and how to keep your key safe.
Short answer: to get an LLM API key, create a developer account with the model provider (for example OpenAI or Google's Gemini), open the API keys page in its dashboard, generate a key, copy it once and store it safely. You pay per token, with separate rates for input and output, so cost is roughly tokens used multiplied by the rate on the official pricing page.
What Is an LLM API Key?
An API key is a long secret string that identifies your account when your code calls a model. Think of it as a password for programs. Anyone who has your key can make calls that are billed to you, so it must never be shared or published. If you are new to the idea, read what an API is and how it works first.
How Do You Get an OpenAI API Key?
The dashboard may look different by the time you read this, but the flow is usually the same:
Sign up for the provider's developer platform. This is separate from the chat app.
Add a billing method or credits if the platform asks for one.
Open the API keys section and choose to create a new key. Give it a clear name, such as "learning-project".
Copy the key immediately. Most dashboards show it only once.
Set a monthly spending limit or budget alert if the dashboard offers one.
How Do You Get a Gemini API Key, and Is It Free?
For Gemini, you use Google's AI developer tools: sign in with a Google account, open the page for creating API keys and generate one. Whether a free usage tier exists, and what its limits are, changes over time and can differ by location. Check the official pricing and quota page instead of trusting a blog post, including this one.
How Is the Cost Calculated?
Providers bill by tokens, which are small pieces of text. A request has two parts, and each has its own rate:
Part | What it contains | How it is billed |
|---|---|---|
Input tokens | Your prompt, system instructions, chat history, attached documents | Input rate |
Output tokens | The text the model writes back | Output rate, usually higher than the input rate |
Rates are normally shown as a price per million tokens, and they differ by model. Larger models cost more per token than smaller ones. The formula is simple:
def estimate_cost(input_tokens, output_tokens, input_rate, output_rate):
# Rates are "price per 1 million tokens", copied from the official pricing page.
return (input_tokens * input_rate + output_tokens * output_rate) / 1_000_000
# The rates 2 and 8 below are made-up numbers, only to test the arithmetic.
print(estimate_cost(1500, 500, 2, 8)) # 0.007
Notice that the whole chat history is sent as input again on every turn, so long conversations get more expensive with each message. The API response includes a usage field that tells you the exact token counts, which is better than guessing.
How Do You Store the Key Safely?
Never paste a key into your source code or commit it to GitHub. Store it in an environment variable and read it from there:
import os
api_key = os.environ.get("MY_API_KEY")
if api_key is None:
raise SystemExit("Set the MY_API_KEY environment variable first")
Put the variable in a .env file and add that file to .gitignore. The chapter on environment variables and .env files shows the full setup, and Call an LLM API from Python shows your first real request.
Common Mistakes
Committing the key to a public repository. If it leaks, delete the key in the dashboard and create a new one at once.
Putting the key in front-end code. Anything the browser can see, users can see. Call the model from a backend.
No spending limit. A loop bug can send thousands of requests. Set a limit and test with short prompts first.
Forgetting the output. Long answers cost more. Cap the maximum output length in your request.
Key Takeaway
An API key is a secret that bills your account. Create it in the provider's dashboard, keep it in an environment variable, set a spending limit, and estimate cost from input and output tokens using the rates on the official pricing page.
Ready to start? You will write real Python code that calls an LLM API, handles the key safely and reads token usage. Begin with the free course Python for AI: From Zero to Your First AI App.
Related articles
More in AI Engineering
What Is Function Calling in LLMs?
Function calling lets an LLM ask your code to run a tool and use the result. Learn the step-by-step flow, a runnable example and the safety rules.
3 min readNov 7, 2026What Is LangChain? When to Use It (and When Not To)
LangChain is a framework for building LLM apps. Learn its core building blocks, a short sketch, and when plain code is the better choice.
4 min readNov 4, 2026How Do You Evaluate an AI or LLM System?
Building an AI system is not enough—you need to measure how accurately, reliably, and safely it performs. Learn the key metrics and methods used to evaluate modern AI and LLM applications.
2 min readSep 6, 2026SQL vs Python for Data Analysis
SQL and Python do different jobs in data analysis. See what each is best at, a side-by-side example, and which one to learn first.
4 min readNov 15, 2026Jupyter vs VS Code vs Google Colab
Where should you write your first Python code? Compare Jupyter Notebook, VS Code and Google Colab, with a simple rule for choosing.
4 min readNov 15, 2026What Is AI Automation? Real Examples
AI automation puts a language model inside a workflow to handle messy, text-heavy steps. See real examples, a small code pattern and the risks to watch.
4 min readNov 14, 2026
Comments
Sign in to join the discussion.



