Chapter 18 · Hands-on: set up your AI workbench
Use a hosted model API safely
Hosted APIs need a secret key; keep it out of your code, watch your usage and never share it.
Chapter 18 · Hands-on: set up your AI workbench
Hosted APIs need a secret key; keep it out of your code, watch your usage and never share it.
Unsafe: the secret sits in the code you may share.
An API key is like a password that also spends money. If it sits in your code and the code is shared, anyone can use it. Put it in an environment variable: a named value your computer keeps outside your files.
This lesson works with any hosted model service that gives you a key. Many services accept the same chat format as the local runner (messages in, reply out). Your provider’s documentation gives the exact web address and model name. Use a service you trust and read its pricing page yourself before you start.
On your provider’s website, sign in, open the API or developer area and create a key. Copy it once. If the provider lets you set a monthly spending limit or an alert, set a small one now.
Check: you have a key saved in a safe place (a password manager), and a limit if one is offered.
This stores the key for this terminal window only; it disappears when you close it. Replace the example text with your real key, and the other two with your provider’s address and model name.
$env:MY_API_KEY = "paste-your-key-here"
$env:MY_API_URL = "paste-the-chat-address-here"
$env:MY_MODEL = "paste-the-model-name-here"Your shell may save typed commands in its history file. Never share that file or your screen while a key is on it.
Check: no output appears. The next step proves the variable is set.
Never print a key just to look at it. Ask only whether it exists.
if ($env:MY_API_KEY) { "key is set" } else { "key is missing" }Check: it prints: key is set
Save this as hosted.py in my-ai-project. Notice the key never appears in the file: the script reads it from the environment, and stops with a clear message if it is missing. max_tokens caps how long the reply can be, which caps the cost of one call.
import os
import requests
api_key = os.environ.get("MY_API_KEY")
if not api_key:
raise SystemExit("MY_API_KEY is not set. Set it in your terminal first.")
url = os.environ["MY_API_URL"]
reply = requests.post(
url,
headers={"Authorization": f"Bearer {api_key}"},
json={
"model": os.environ["MY_MODEL"],
"messages": [{"role": "user", "content": "Say hello in five words."}],
"max_tokens": 50,
},
timeout=60,
)
reply.raise_for_status()
data = reply.json()
print(data["choices"][0]["message"]["content"])
print("tokens used:", data.get("usage"))Check: the file contains no real key, only os.environ lookups.
With the virtual environment active, run the script. The last line shows how many tokens the call used, as in the tokens lesson. Compare it with the usage page on your provider’s dashboard.
python hosted.pyCheck: a short reply prints, followed by a tokens used line. If you get an error code, read the message: 401 usually means a wrong or missing key, 404 a wrong address or model name.
If you use version control, never add a file that holds a key. A .gitignore file lists files to leave out. If a key is ever shared by mistake, revoke it on the provider’s site at once and make a new one: deleting it from the code is not enough.
.env
.venv/
__pycache__/Check: you know which file would be left out of a shared folder.
Paste a piece of your own code (without real secrets) or edit the sample. The checker flags lines that look like a real key written into the code.
api_key = "sk-FAKEFAKEFAKEFAKE1234567890"Move it into an environment variable and read it with os.environ. If it was ever shared or uploaded, revoke it on the provider's site and make a new one.Where should an API key live?
You accidentally uploaded a key with your code. What should you do first?
Why add a max_tokens limit?