Google now recommends discarding the practice of embedding raw Gemini API keys in code and moving to a managed‑identity approach that leverages Application Default Credentials, dedicated service accounts, and Secret Manager. Engineers and operators need to adopt this because raw keys are unrestricted bearer tokens that can be leaked, leading to uncontrolled costs and a lack of revocation control.
Why raw keys are a risk
A Gemini API key is a bearer token without an expiration date, machine binding, or built‑in spend limit. If the key is copied into a repository, chat message, or application bundle, anyone who discovers it can invoke the model at full scale. The source cites an example where a $180‑per‑month account incurred tens of thousands of dollars in charges after a key was exposed. Under the shared‑responsibility model, the customer remains liable for that usage.
Adopt managed identities for people
The first step is to stop giving developers individual raw keys. Instead, each team member should authenticate with a single login that provides Application Default Credentials. Those credentials can be used to call Gemini, Claude, and other open models. The guide notes that the initial two steps take roughly five minutes, indicating a low‑friction migration path.
Run applications with dedicated service accounts
Production workloads should no longer run under a user’s key. The recommendation is to create a service account that has only the permissions required for the specific AI workload and run the application under that identity. This isolates the workload’s access and makes revocation straightforward if the service account is ever compromised.
Secure remaining secrets and enforce guardrails
Any keys that must remain for legacy reasons should be moved into Secret Manager. Once stored there, teams can attach spend caps, guardrails, and monitoring alerts. The source also provides a small scanner that detects strings matching Google API key patterns and literal api_key= assignments. The scanner reports file locations without exposing the key value and exits with an error, making it suitable for inclusion in CI pipelines.
Related CloudNinjas coverage: Google Cloud.
What This Means For Practitioners
- Audit existing codebases. Run the provided scanner to locate raw keys in repositories, CI scripts, and configuration files.
- Replace developer keys. Switch each engineer to Application Default Credentials and remove personal keys from .env files and shell profiles.
- Provision service accounts. Define minimal‑privilege service accounts for each AI‑enabled service and configure the runtime to use them.
- Centralize secrets. Store any unavoidable keys in Secret Manager, then apply spend caps and monitoring policies.
- Automate enforcement. Add the scanner to CI pipelines to prevent new raw keys from entering the codebase.
By following these steps, teams can eliminate the uncontrolled exposure surface of raw API keys, gain visibility into usage, and align their AI prototypes with production‑grade security and cost controls.

