Sub topic 3
Separate Keys, Permissions, and Spend Limits
Assume you know basic command-line use, Git, and how an HTTP API works. The goal is to limit the damage if one LLM API key is exposed.
Use separate keys with limited access
Create different keys for local development, testing, and production. A developer key should not access production resources. Give each key the smallest permissions it needs, such as calling only selected models or endpoints.
Set spending limits and usage alerts where the provider supports them. A limit reduces unexpected cost, but it does not replace revocation. A stolen key can still consume its entire allowed budget.
| Key | Use | Protection |
|---|---|---|
| Development | Local experiments | Low spend limit |
| CI | Tests and builds | Restricted permissions |
| Production | Deployed backend | Strong limit and monitoring |
Use secrets in CI and deployment
Store keys in your CI provider’s encrypted secret store. Pass them to a job as environment variables. Do not write them into workflow files, command arguments, or build artifacts.
env:
LLM_API_KEY: ${{ secrets.LLM_API_KEY }}Deployment platforms usually provide a similar secret setting. Configure the value there, then restart or redeploy the service if required.
Catch secrets before they are committed
Add .env files to .gitignore, and enable secret scanning in your repository host. A pre-commit hook can scan staged files before Git creates a commit. Also check logs, screenshots, issue comments, and copied terminal output.
Rotate and revoke keys
Rotation means replacing an existing key with a new one. Create the replacement, configure every authorized environment, test it, and then revoke the old key. Keep the overlap brief.
Respond to a leaked key
Revoke the key first. Then inspect provider usage and billing, identify where it appeared, remove it from current files and systems, and replace it everywhere. If it was committed, rewrite history only after coordinating with collaborators. History cleanup does not make the old key safe; revocation does.
Move to a secret manager
Use a dedicated secret manager when several services or people need access, production deployments need auditing, or secrets must rotate automatically. It centralizes access control and avoids distributing long-lived values through files and dashboards.
Exercise
Why should a CI job use a separate, restricted key instead of the production key?
Show answer
A compromised CI environment then has less access and a smaller possible cost impact.