What you need to know
Where the key lives
| Place | Verdict |
|---|---|
| Browser JavaScript, mobile app bundle, local storage | Never: anyone can extract it, encoded or split or not |
| Source code, config files in git, Docker images | Never: history and copies keep it forever |
| System prompts or anything the model sees | Never: context can leak |
| Environment variables injected at runtime from a secret manager | Yes |
| A server-side proxy that adds the key to requests | Yes, and it is the pattern for clients and for agents that can run code |
Scope keys with workspaces
- One workspace per environment (development, staging, production) and, where useful, per team or product. A leaked development key does not touch production, and rotating one does not break another.
- Set spend and rate limits per workspace, so a leaked key's damage is capped.
- Developers experiment with keys in a development workspace, never the production key, never a teammate's key.
Admin keys are different
Admin API keys manage the organization: workspaces, members, API keys, usage reports. They are not for Messages API calls and must be held by very few people or services.
CI and workloads
Use workload identity federation to exchange the CI system's short-lived identity token for access, instead of storing a static key in pipeline settings.
Rotation and response
- Rotate keys on a schedule, so rotation is routine rather than an emergency.
- A key committed to a public repository is compromised even if deleted in the next commit: revoke or rotate immediately and review its usage.
- Enable secret scanning in repositories.
Monitoring
Watch usage per key and workspace (for example, through the Usage and Cost API) for spikes or unexpected models and times. Log request IDs, never keys.
Customer-supplied keys
If customers bring their own keys: encrypt them at rest, use each only server-side for that customer's requests, never return or display them after saving, never log them, never use one customer's key for another.
In the exam
Items describe where a key is used or stored, or an incident, and ask for the correct handling. Pick server-side proxying, workspace separation with limits, secret managers, rotation, federation for CI and monitoring. Distractors encode or hide keys in clients, reuse production keys, use Admin keys for model calls, or treat a deleted commit as safe.
Check your understanding
A React app plans to call Claude directly with the key in the bundle. Instead?
Show answer
Answer: A. Client code is visible; the backend keeps the key secret.
What is an Admin API key for?
Show answer
Answer: B. It is for organization administration, not model calls.
A key was committed to a public repo and removed in the next commit. What now?
Show answer
Answer: D. History keeps the key; treat it as compromised.
Apply it: Fix Bluestem's key handling
Bluestem uses one API key in development, staging, production and CI. A developer committed it to a public repository; rotating it broke production for an hour. Static keys in CI must end next quarter, and finance wants to know quickly if spending spikes.
- Respond to the leak.
- Redesign key scope across environments.
- Fix CI authentication.
- Set up monitoring and limits.
Done when: a leak in one place cannot reach production or run up unlimited spend.
Show model answer
1. Revoke the leaked key at once; issue new keys; review usage for the exposure window for unknown activity; enable secret scanning on all repositories.
2. Workspaces for development, staging and production, each with its own keys, rate limit and monthly spend limit; developers use development keys only; production keys live in the secret manager and are injected at runtime; rotation every 90 days per workspace, so rotating one never breaks another.
3. Workload identity federation from the CI provider to the staging workspace; no static key in pipeline settings.
4. A daily job reads usage per workspace through the Usage and Cost API and alerts on spend over 150% of the 7-day average or on unexpected models; Admin keys held by two platform engineers only.