Environment Configs
Separate dev/stage/prod settings for models, keys, and feature flags.
In plain words
Environment configs let the same app run safely on your laptop, in testing and in the real world by changing settings, not code.
Think of it like…
The settings dial of an appliance: the same machine runs gently in the showroom, normally at home and at full power in the factory, and each mode is a labelled setting rather than a rebuilt machine.
Where you’ve seen it
An assistant's settings class (Pydantic Settings) reads defaults from code, an environment-specific file (config/prod.yaml), then environment variables, then secrets from the cloud secret manager, with later layers overriding earlier ones; the app refuses to start if a required value is missing or invalid (fail fast). Differences by environment: dev uses a small model costing about $0.000717 per request versus $0.01195 for the production model, 16.7 times cheaper, with a $5 daily budget and 10 requests per minute; staging uses the production model and prompt version with a $50 budget and production-like retrieval over a sampled index; production pins the exact model snapshot and prompt label, sets a $2,000 daily budget and per-tenant rate limits. A CI check diffs the effective config of staging and production and fails if anything outside an allow-list of 6 intentional differences (budget, keys, endpoints, log level, replicas, index name) among 25 settings differs. Every request log includes a short hash of the effective config and the environment name, so an incident can be tied to a precise configuration. Feature flags have safe defaults (off) if the flag service is unreachable, and secrets never appear in config files or logs.
Words you’ll hear
- Twelve-factor config
- Store config in the environment, separate from code.
- Typed settings
- A validated object (for example Pydantic Settings) that parses and checks configuration at start-up.
- Config drift
- Unintended differences between environments that undermine testing.
- Fail fast
- Refuse to start on missing or invalid configuration.
Visual diagram
Theory & deep dive
Layering, typing and secrets
Use layers with clear precedence: sensible code defaults, then a per-environment file committed to the repository (non-secret values such as model IDs, timeouts, flags, thresholds), then environment variables injected by the platform, then secrets resolved from a secret manager at start-up. Parse everything into a typed settings object (pydantic-settings, Zod, a config library) with types, ranges and required fields: a temperature outside 0 to 2, a negative timeout or a missing model ID stops the process before it serves traffic. Keep secrets out of config files and logs; configuration objects should redact secret fields in their string representation. Prefer immutable configuration per deployment (changing it triggers a new rollout) except for values meant to change at runtime, which belong in a feature-flag or config service with audit logs.
For AI systems specifically, make these first-class settings: model ID with an exact snapshot in production (see model versioning), generation parameters (temperature, max tokens, top-p), prompt label or version, retrieval parameters (top-k, reranker on/off, index name), budgets and rate limits (daily spend caps, per-tenant quotas), timeouts and retry policy (see fallbacks), safety filters and thresholds, logging and sampling rates for traces and payload capture, and feature flags with defaults. Document each setting's purpose, owner and safe range, and provide a generated config reference so engineers don't guess.
class Settings(BaseSettings): model_id: str; temperature: confloat(ge=0, le=2) = 0.2; daily_budget_usd: PositiveFloat; prompt_label: Literal["staging", "production"] The service raises at start-up if model_id is missing or temperature is 3.0 instead of running with a bad value.
Environment parity, drift control and cost safety
Parity keeps tests meaningful: staging should run the same image, model snapshot, prompt version and retrieval configuration as production, differing only in scale, credentials, data and budgets. Make differences explicit: define an allow-list (budget, keys, endpoints, log level, replica count, index name: 6 of 25 settings in the example) and have CI render the effective configuration for each environment and fail on unlisted differences. Developers need a cheap loop: dev defaults to a small model (16.7 times cheaper per request in the example), small indexes and tight budget caps ($5 a day) so mistakes don't become bills, with an easy switch to the production model for targeted tests.
Operational practices: log a config hash and environment with each request or trace; keep config changes in version control with review (config as code) and a rollout process like code (canary where possible); never copy production secrets into lower environments (use separate keys with separate quotas); give feature flags safe defaults if the flag service is unreachable; test config changes in CI (schema validation, diff checks, a smoke run with the rendered config); and maintain a runbook for emergency changes. Watch for hidden configuration (defaults buried in code, provider-side dashboard settings, prompt text edited in a vendor UI) and bring it under version control. When configs diverge, treat it as an incident precursor and reconcile.
25 settings total. Allow-list of 6 may differ across staging and production: daily_budget, api_key (by reference), endpoint, log_level, replicas, index_name. CI fails if prompt_label, model_id, temperature or retrieval_top_k differ between them.
How it works, step by step
Step 1 of 41. Inventory settings
List every setting that varies (models, prompts, budgets, timeouts, flags), with owner, type and safe range.
- Find
- hidden config
When to use it & trade-offs
What it is
Environment configuration is how the same code behaves differently in development, staging and production without code changes. In AI applications the settings that vary include model IDs and parameters (a small cheap model in dev, a pinned production model), API endpoints and keys, rate limits and daily budgets, feature flags, prompt and retrieval versions, timeouts, logging and tracing levels and data sources (a sandbox index versus the real one). Good practice follows the twelve-factor idea: strictly separate config from code, keep it in environment variables or a config service, load it into a typed, validated settings object at start-up, keep secrets in a secret manager and make non-production environments mirror production except for deliberate, documented differences.
Why it matters: Configuration mistakes cause many incidents: staging running a different prompt version than production so tests prove nothing, a dev key with production quota, a temperature of 0.9 copied into the wrong environment, a feature flag defaulting on. AI systems add expensive failure modes (using the largest model in every developer's laptop loop, no spend cap in staging). Making configuration explicit, validated, versioned and diffable removes a large class of "works in staging" surprises.
When to reach for it
- Running the same service across dev, staging and production
- Controlling cost with per-environment budgets
- Auditing what configuration produced a given output
When to avoid or be careful
- Hard-coding model IDs or keys in code
- Copying production secrets into staging
- Letting staging silently differ from production
Where configuration lives
Strengths: Versioned, reviewable, simple.
Watch out: Changes require deploys.
Strengths: Encrypted, audited, rotatable.
Watch out: Extra integration.
Strengths: Change without redeploying; targeting.
Watch out: Another dependency; needs safe defaults.
Interview signals & numbers to know
When this topic comes up
- “Works in staging, fails in production”
- Twelve-factor app
- Pydantic settings
- Config drift
- Per-environment budgets
Benchmarks & rules of thumb
- 16.7×
- cost ratio of the production model to the dev model per request
- 6 of 25
- settings allowed to differ between staging and production
- $5 / $50 / $2,000
- daily budgets by environment in the example
Common mistakes & follow-ups
Common interview mistakes
- Using the largest model in every local test run.
- Defaulting a risky feature flag to on when the flag service is down.
- Not logging which configuration served a request.
Likely interviewer follow-ups
- How would you manage model and prompt settings across environments?
- What does fail fast mean for configuration?
- How do you detect configuration drift?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Environment Configs with generative AI interview Q&A and hands-on practice.