An API key is a bearer credential sent as a request header (x-api-key) — anyone who has it can spend on your account, up to whatever limits and permissions that key carries. It belongs in environment variables or a secrets manager, never committed to source control, never logged in plaintext, and never placed inside a prompt (where it could end up echoed back in a response or stored in a logging pipeline downstream that wasn't built with credential handling in mind). A key committed to a public repository, even briefly and even if deleted in a later commit, should be treated as compromised and rotated — git history retains it, and automated scanners actively look for exactly this pattern across public repos.
Workspaces, scoping and least privilege
Where the platform supports it, scope keys as narrowly as the use case allows: a separate key per environment (development, staging, production) so a leaked staging key doesn't expose production spend, and organisational workspace boundaries so one team's usage and billing are isolated from another's. This mirrors ordinary least-privilege practice for any other credential type — a database password scoped to one service rather than a shared superuser credential is the same underlying principle applied to a different kind of secret.
Config as a separate axis from code
Model name/version, max_tokens, temperature, which tools are enabled, and the extended-thinking budget are configuration, not code — keeping them in environment-specific config rather than hardcoded lets you roll a model version forward in staging before production (Lesson 1.11 covers this rollout discipline directly), or dial down cost in a lower environment, without a code change and redeploy for every tweak. Treating a model-behaviour knob as a config value rather than a magic literal buried in application logic also makes it something you can audit, diff between environments, and roll back cleanly if a change causes a regression.
Key concept
The same discipline that applies to a database password applies to an API key: least-privilege scoping where the platform supports it, rotation on suspected exposure, and never in a client-side/browser or mobile-app context where any user could read it out of network traffic or extract it from the compiled binary.
The correct architecture: a backend proxy
A client application (browser or mobile) that needs Claude output should call your own backend, which holds the real API key server-side and forwards the request — never call the Messages API directly from client code. This gives you a place to enforce rate limiting per user, apply your own business logic (including the hook-style policy enforcement covered in Domain 3), and rotate or revoke the underlying key without shipping a new client build. It's more infrastructure than a direct client call, but it's the only architecture where the credential actually stays a secret.
Organisation and workspace structure
An organisation on the platform can contain multiple workspaces, each with its own API keys, usage limits, and billing visibility — a natural boundary for separating teams, products, or environments so that one workspace's runaway usage or leaked key doesn't silently exhaust another's budget or blur cost attribution. Admin-level keys and roles that can manage workspace membership, create or revoke other keys, and view organisation-wide billing are a materially higher-privilege credential than a workspace-scoped key used purely to call the Messages API, and should be handled with correspondingly tighter access control — a small number of trusted people, not embedded in any application code path at all.
Rotation as a routine practice, not just an incident response
Key rotation is often framed only as something you do after a suspected leak, but a mature setup rotates keys on a routine schedule regardless of any known incident, precisely because not every exposure is detected — a key that leaked through a channel nobody's monitoring (a misconfigured log aggregator, a screen-shared terminal, a support ticket with a debug output pasted in) doesn't announce itself. Scheduled rotation bounds the exposure window for a leak nobody has yet noticed, the same logic that underlies routine password rotation policies for other credential types.
Common exam distractor
An answer that treats "the code doesn't currently contain the key" as sufficient proof a credential is safe is a trap. Version control history, chat logs, ticket attachments, and log aggregators can all retain a secret long after it's gone from the current working files — the question is whether it was ever exposed anywhere, not whether it's visible right now.