Area 07 · Deployment · Concept 07 of 20

Environment Configs

Separate settings and secrets for dev, staging and production.

01–10 · The basePhase 5 · Weeks 18–20≈ 45 min

The idea

The same code runs in several environments with different settings: database URLs, model names, log levels, feature flags, rate limits and API keys. Configuration comes from the environment, not from code (the twelve-factor principle), so one image runs everywhere.

Load and validate config at startup with a typed settings class, fail fast on missing values, and never default production secrets. Use different provider keys and budgets per environment so a staging bug cannot burn the production quota.

Keep non-secret config in version control (per-environment files) and secrets in a secret manager.

Why it matters

Config mistakes cause many outages: staging pointing at the production database, debug logging of prompts in prod.

Words to know

  • Twelve-factor appA set of principles for deployable services, including config in the environment.

Try it without code

List your settings

  1. List every setting your app reads.
  2. Mark secret or not, and the value per environment.

Use it in code

Typed settings with pydantic-settings.

python
# pip install pydantic-settings
from typing import Literal
from pydantic_settings import BaseSettings, SettingsConfigDict

class Settings(BaseSettings):
    model_config = SettingsConfigDict(env_file=".env", extra="ignore")
    env: Literal["dev", "staging", "prod"] = "dev"
    database_url: str
    anthropic_api_key: str
    chat_model: str = "claude-sonnet-5-5"
    log_prompts: bool = False
    daily_token_budget: int = 50_000

settings = Settings()       # raises a clear error if a required value is missing
if settings.env == "prod":
    assert not settings.log_prompts, "never log prompts in prod"
PRACTICE · TICK IT AFTER THIS

Replace scattered os.environ calls in your app with one Settings class and separate dev and prod values.

Common mistakes

  • Hard-coded model names in many files.
  • Defaults that silently point at production.

Go deeper