API Keys and Cost
Most RAG systems use cloud LLMs like OpenAI's GPT or Cohere. You need API keys to access them. This chapter covers how to get API keys, store them safely, and manage costs.
Getting API Keys
- OpenAI: Sign up at platform.openai.com, add billing, create API key.
- Hugging Face: Sign up at huggingface.co, get access token (free for many models).
- Cohere: Sign up at dashboard.cohere.com (free trial).
Storing API Keys Securely (Never Hard‑Code!)
Create a
.env file in your project folder:OPENAI_API_KEY=sk-...
HUGGINGFACE_TOKEN=hf_...Then load them in Python:from dotenv import load_dotenv
import os
load_dotenv()
openai_key = os.getenv("OPENAI_API_KEY")Add .env to .gitignore so you never commit secrets.Cost Awareness
- OpenAI charges per token (input + output). A typical RAG query may cost $0.001‑$0.01.
- Set spending limits in your OpenAI account.
- Use smaller, cheaper models (e.g., GPT‑3.5‑turbo instead of GPT‑4).
- Consider local models (Ollama) for free, unlimited use (covered later).
Two Minute Drill
- Get API keys from OpenAI, Hugging Face, or Cohere.
- Store keys in
.envfile and load withpython-dotenv. - Never hard‑code API keys or commit them to Git.
- Be aware of token costs; set spending limits.
Need more clarification?
Drop us an email at career@quipoinfotech.com
