Sub topic 10
Separate Keys, Permissions, and Spend Limits
This chapter assumes you can load a key from an environment variable, keep .env out of Git, and call an HTTP API from server-side code. Here you limit the damage a key can do if it leaks anyway.
Why one key is not enough
A single key used everywhere has a large blast radius. If it leaks from a test script, the attacker also gets your production access. You cannot revoke it without breaking every system that uses it.
The fix is to create several keys, each with a narrow job. Most provider consoles let you name keys, and many let you group them into projects or workspaces. Exact options differ by provider, so check your console.
One key per environment and purpose
Create a separate key for each place the code runs and, where it helps, for each application.
| Key name | Used by | Suggested limits |
|---|---|---|
| `myapp-dev-alice` | One developer's laptop | Low spend cap |
| `myapp-ci` | Automated tests | Low spend cap, cheap models only |
| `myapp-staging` | Staging server | Moderate cap |
| `myapp-prod` | Production backend | Higher cap, alerts on |
Separate keys give you three things:
- Containment. A leaked dev key cannot be used to run up production costs.
- Attribution. Usage reports show which key, and so which system, spent the money.
- Independent revocation. You can disable one key without an outage elsewhere.
Use the same variable name in every environment, such as LLM_API_KEY. Only the value changes, so your code stays identical.
Permissions
Some providers let you restrict a key, for example to specific models, specific API endpoints, or read-only access. Apply the principle of least privilege. A key should be able to do what its job needs and nothing more. A CI key that only runs tests does not need access to expensive models or to account administration.
If your provider offers no permission controls, project-level separation and spend limits become your main protection.
Spend limits and alerts
A leaked LLM key is costly because attackers can send large volumes of requests that you pay for. Providers usually offer some of these controls:
- A monthly budget or hard spend cap per project or account.
- A usage alert that emails you at a threshold, such as 50% and 80% of budget.
- Rate limits on requests or tokens per minute.
A hard cap stops spending. An alert only tells you about it, so use both. Set limits to about twice your normal expected usage, not to an unlimited default.
Checking usage in code
Some providers return usage data in each response. Logging token counts, never the key, helps you notice odd spikes. The field names below are placeholders, so use the names from your provider's documentation.
import logging
logger = logging.getLogger("llm")
def log_usage(data: dict) -> None:
usage = data.get("usage", {}) # placeholder field name; check your provider
logger.info(
"llm_call input_tokens=%s output_tokens=%s",
usage.get("input_tokens"),
usage.get("output_tokens"),
)
log_usage({"usage": {"input_tokens": 12, "output_tokens": 40}})Common mistakes
- Reusing the production key "just for a quick test".
- Leaving a spend cap unset because the account is new.
- Setting an alert but sending it to an inbox nobody reads.
- Giving every teammate the same shared key, which removes attribution.
Exercise
Your team has one key shared by laptops, CI, and production. A developer's laptop is stolen. Why is this setup a problem, and what would you change?
Show answer
Revoking the leaked key would also break CI and production, and you could not tell which system spent what. Create separate keys per environment (for example dev per developer, CI, and prod), give each its own spend cap and alerts, and restrict permissions where the provider allows. Then you can revoke only the stolen laptop's key.