Unit 02.04: Keeping configuration out of the code
Configuration in code is configuration you cannot change without a deploy, and secrets in code are secrets in your repository.
Environment for values, constants for versions
Model, temperature and limits come from the environment with sensible defaults. The API key is read and never printed.
The code assembles a config and confirms the key is present without revealing it.
import json
import os
CONFIG = {
"model": os.environ.get("APP_MODEL", "fake-model-for-tests"),
"temperature": float(os.environ.get("APP_TEMPERATURE", "0")),
"max_tokens": int(os.environ.get("APP_MAX_TOKENS", "512")),
"prompt_version": "answer-v3",
"policy_version": "support-policies-v4",
}
print(json.dumps(CONFIG, indent=2))
secret = os.environ.get("APP_API_KEY")
print(f"\napi key present: {secret is not None} (never printed, never logged)")
# The two version fields are the ones people leave out and the ones that make a
# run reproducible. A trace recording the model but not the prompt version
# cannot be explained once someone edits the prompt -- which happens weekly and
# is rarely thought of as a change to the system.
The two version fields are the ones people leave out. A trace recording the model but not prompt_version cannot be explained once someone edits the prompt - which happens weekly and is almost never thought of as a change to the system's behaviour.
The default for model is a fake, which is deliberate. A misconfigured environment then fails loudly in tests rather than silently calling a paid API with production settings.
The mistake this prevents
The mistake is logging the config object for debugging. It is one line, it is enormously useful, and the moment someone adds the key to it you have credentials in your log store. Keep secrets out of the config object entirely and read them separately.
Takeaway
Read configuration from the environment, include prompt and policy versions, and keep secrets out of any object you might log. Default to a fake model so misconfiguration fails safely.
