Sub topic 8
Checking that it works without printing the key
This chapter assumes you can run shell commands, read short Python snippets, and know that an HTTP API takes requests and returns status codes. It also assumes your key is already stored in an environment variable named LLM_API_KEY, as set up in earlier chapters.
Why verification needs care
The obvious way to check a setup is print(api_key). That puts the secret in your terminal scrollback, in shell history if you paste it, in CI logs, and in screenshots you share when asking for help. Any of these can leak the key.
You can check the setup with signals that carry no secret: whether the variable exists, how long it is, and what status code the provider returns.
Step 1: Confirm the variable is set
This shell check prints only the length of the value:
export LLM_API_KEY="paste-your-key-here"
echo "${#LLM_API_KEY} characters set"If it prints 0 characters set, the variable is empty or missing. A length that is much shorter than your provider's keys usually means a paste was cut off. A length that is too long often means a stray space or newline came along with the paste.
Step 2: Confirm your code can see it
Your program may run in a different shell or process than the one where you exported the variable. Check from inside the program, and fail with a clear message:
import os
from dotenv import load_dotenv # pip install python-dotenv
load_dotenv() # reads .env into os.environ; real environment variables are not overridden by default
api_key = os.environ.get("LLM_API_KEY")
if not api_key:
raise RuntimeError("LLM_API_KEY is not set")The error names the variable but never shows its value.
Step 3: Make one real request
A key can be present and still be wrong. The only proof is a request to the provider. Print the status code and nothing else:
import os
import requests
api_key = os.environ["LLM_API_KEY"]
response = requests.post(
"https://api.example.com/v1/chat", # placeholder URL: use your provider's documented endpoint
headers={"Authorization": f"Bearer {api_key}"},
json={"prompt": "Say hello"},
timeout=30,
)
print(response.status_code)Printing response.status_code avoids echoing the request headers, which contain the key. Avoid printing the whole response.request object or enabling verbose HTTP debugging for the same reason.
Reading the result
| Status | Usual meaning | What to check |
|---|---|---|
| 200 | The key was accepted. | Nothing. The setup works. |
| 401 | The key is missing, mistyped, or revoked. | Length check, extra whitespace, and whether the key was revoked in the console. |
| 403 | The key is valid but not allowed to do this. | Permissions, model access, and region or project settings. |
| 429 | Rate limit or spend limit reached. | Usage dashboard, billing, and limits. |
Exact codes and meanings vary by provider, so confirm them in your provider's documentation.
Common mistakes
- Printing the key "just for a second" while debugging. Print its length instead.
- Testing in one terminal and running the program in another, where the variable is not set.
- Letting
.envcontain quotes or trailing spaces that become part of the value. - Pasting a failing request, including its headers, into a chat or issue. Remove the
Authorizationline first.
Exercise
Your test request returns 401, and echo "${#LLM_API_KEY}" prints 52 when your provider's keys are 51 characters long. What is the most likely cause, and how do you check it without printing the key?
Show answer
The value probably has one extra character, such as a trailing space or newline from copying. Re-export the key after trimming whitespace, then confirm the length is 51 and send the request again. If it still returns 401, check in the provider console whether the key was revoked.
Exercise
Why is printing response.status_code safer than printing response.request.headers in the verification script?
Show answer
The request headers include the Authorization header, which holds the key. The status code holds no secret and still tells you whether the key was accepted.