Secrets Management
Store API keys and credentials in vaults-never in prompts, repos, or client bundles.
In plain words
Keep passwords and API keys in a locked safe that only your servers can open, never in your code, in your prompts or in a website anyone can inspect.
Think of it like…
A hotel's key system: master keys are kept in a safe and signed out per shift, guests get a card that works for one room and expires at checkout, and nobody writes the safe combination on the door.
Where you’ve seen it
A startup's chatbot called the LLM API directly from the browser with a key embedded in the JavaScript bundle (NEXT_PUBLIC_ variables are shipped to every visitor). Within two days the key appeared in a scraper's list and was used for 10,000 requests an hour at about $0.012 each, $120 an hour or $2,880 a day, until the provider's spending limit was hit. The fix: move calls behind a backend route that holds the key in the cloud secret manager and injects it at runtime through the service's workload identity, authenticate users and rate-limit per user, set a hard per-key budget of $200 a day with alerts at 50% and 80%, use a separate key per environment and per service so a leak has a small blast radius, and add secret scanning to CI and a pre-commit hook (the first scan finds 3 old keys in git history, all rotated). Prompts contain no secrets: tool credentials are configured server-side and the model only emits tool names and arguments.
Words you’ll hear
- Secret manager / vault
- A hardened service that stores, encrypts, audits and distributes secrets.
- Workload identity
- Short-lived credentials issued to a running service by its platform, avoiding static keys.
- Secret scanning
- Automated detection of credentials in code, history, logs and artifacts.
- Spend cap
- A hard budget on a key or project that limits the damage of theft.
Visual diagram
Theory & deep dive
Where secrets leak and where they belong
Common leak paths: committed to git (history keeps them even after deletion), bundled into client code (framework prefixes such as NEXT_PUBLIC_ or VITE_ intentionally expose variables to the browser), baked into container images and build logs, pasted into notebooks, tickets and chat, written to application logs and traces, included in prompts and system messages (and therefore in provider logs, traces and extractable completions), and shared in long-lived .env files copied between laptops. LLM-specific traps include agent tools that accept credentials as parameters chosen by the model, and prompt templates that interpolate keys.
The right pattern is a secret manager with fine-grained access policies and an audit trail, and runtime injection into the process that needs the secret. Prefer workload identity (cloud IAM roles for services, Kubernetes service-account federation, OIDC from CI) which removes static keys altogether. Where static API keys are unavoidable (most model providers), use one key per service and environment, store it in the vault and mount it as an environment variable or file at start-up, restrict who can read it and keep it out of logs. The LLM provider call must be made from a backend you control; the front end calls your API, which authenticates the user, enforces quotas and then calls the provider.
A stolen key used at 10,000 requests an hour × $0.012 = $120 an hour, $2,880 a day. With a $200 daily provider cap the maximum loss is $200 per day per key, and per-service keys mean a leak affects only one service.
Preventing, detecting and responding
Prevent: add pre-commit hooks and CI secret scanning (gitleaks, trufflehog, GitHub secret scanning with push protection) to block commits containing key patterns; scan container images and build artifacts; run periodic scans of git history and repositories; set least-privilege scopes on keys (provider projects with limited models and budgets); store different secrets per environment; and avoid passing secrets through command-line arguments, which appear in process lists. Educate developers on the front-end exposure rules and give them an easy, approved path to get secrets (so they don't resort to .env files in chat).
Detect: monitor key usage (requests per hour, models, IPs, regions), alert on anomalies and approaching spend caps, enable the provider's leaked-key detection if available and watch audit logs for unusual secret reads. Respond: when a secret is exposed, treat it as compromised immediately: revoke or rotate it (see API key rotation), check audit logs for use between exposure and revocation, scan for related secrets in the same location, remove the secret from history where possible (but rotation is what protects you) and review how it leaked. Keep a runbook with owners and target times (for example, revoke within 1 hour of detection). Test the process with drills so that rotation is routine, not an emergency.
Running gitleaks over the full git history finds 3 old keys (a provider key from a prototype, a database URL in a script, a webhook secret in a test). All three are rotated the same day, even though the files were deleted long ago, because history still contains them.
How it works, step by step
Step 1 of 41. Inventory and classify secrets
List every key, token and password: owner, environment, scope, where stored and who can read it.
- Per service
- and environment
When to use it & trade-offs
What it is
Secrets management is how an AI system stores, delivers, uses and retires credentials (model-provider API keys, database passwords, tool tokens, signing keys) without exposing them. The rules are: secrets live in a vault or secret manager (HashiCorp Vault, AWS Secrets Manager, GCP Secret Manager, Azure Key Vault), are injected at runtime into the service that needs them (environment variable, mounted file or, better, workload identity that avoids static keys), are never placed in prompts, source repositories, container images or client-side bundles, and are scoped, short-lived, rotated and monitored. For LLM apps there is one extra rule: the model must never see a secret, because anything in its context can be extracted.
Why it matters: Leaked API keys are exploited fast: automated scanners watch public repositories and paste sites and often find new keys within minutes, and a stolen LLM key lets an attacker run up a large bill, read data sent through your account or impersonate your product. Front-end frameworks make it easy to bundle a key into JavaScript that every visitor can read. Good secrets hygiene prevents the most common and most preventable class of AI-app breach and makes incident response (revoke, rotate, audit) quick.
When to reach for it
- Any app calling LLM providers or tools
- CI/CD and deployment pipelines
- Agent tool configuration
When to avoid or be careful
- Embedding keys in front-end bundles
- Putting credentials in prompts or tool arguments chosen by the model
- Sharing .env files in chat
Where to keep a secret
Strengths: Encrypted, audited, rotatable, access-controlled.
Watch out: Needs setup and identity configuration.
Strengths: Short-lived credentials; nothing to leak.
Watch out: Not available for every third-party API.
Strengths: Convenient.
Watch out: Easily leaked via git, logs, chat and images.
Interview signals & numbers to know
When this topic comes up
- “Our API key was found on GitHub”
- NEXT_PUBLIC_ environment variables
- Secret scanning
- Workload identity
- Spend caps
Benchmarks & rules of thumb
- $120/hour · $2,880/day
- cost of a stolen key used at 10,000 requests an hour
- $200/day
- example hard budget per key
- 3
- old keys found in git history by the first scan
Common mistakes & follow-ups
Common interview mistakes
- Deleting a committed key from the repository but not rotating it.
- Using one provider key for every service and environment.
- Letting the LLM choose or receive credentials.
Likely interviewer follow-ups
- How would you secure LLM provider keys in a web app?
- Why isn't deleting a leaked key from git enough?
- How can you limit the damage of a stolen API key?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Secrets Management with generative AI interview Q&A and hands-on practice.