
How to fix your team’s API keys

Most Gemini prototypes start with a raw API key from Google AI Studio pasted into a coding agent, and that speed is intentional. The trouble starts when the prototype reaches real users: many keys end up in .env files, shell profiles, chat threads, and app configs. A leaked key is a bearer token—it doesn’t expire, is not tied to a person or workload, and has no spending limit by default. The post uses the example of an account that normally spends $180 a month: once a key lands in a public repo, Slack channel, or mobile app bundle, scrapers can run large workloads and the bill can reach tens of thousands before the first alert. Under the shared responsibility model, the customer pays for usage made with its own credentials.
The guide replaces raw keys with identities. Each developer runs gcloud auth application-default-login, signs in with a work account, and gets a short-lived credential that every Google Cloud SDK finds automatically. That same login works for models across Model Garden: Gemini through the Google Gen AI SDK with enterprise=True (older samples use vertexai=True), Claude through Anthropic’s SDK, and open models through the OpenAI SDK pointed at Google Cloud. Partner models require a one-time enable on their Model Garden card. For the application, the code from earlier steps does not change; on a laptop ADC picks up the developer login, and on Cloud Run, GKE, or Compute Engine it picks up a short-lived, automatically refreshed token for the attached service account.
Permissions are kept narrow. Calling models requires only roles/aiplatform.user, granted to a Google group for people and to the service account for the app, instead of Editor or Owner. For projects in an organization, the vertexai.allowedModels policy can restrict which models any identity can call; the repo includes a one-line audit of service accounts holding Owner or Editor and org policies that block creation of new API keys and service-account keys. The remaining secrets, such as Stripe, Slack, or email provider keys, move to Secret Manager: test keys readable by the developer group, live keys readable only by the production service account. Reading with –data-file=- from standard input keeps values out of shell history, and rotating a key simply adds a new secret version without redeploying.
Additional protections: a spend cap budget (in Preview) pauses new requests to a service when estimated spend passes the configured amount, with alerts at 50, 80, and 100 percent. It covers one project and one service; enforcement is based on estimated cost, is not instant, overage is billed normally, and the service can take up to an hour to recover after the cap is lifted. Model Armor screens prompts and responses for prompt injection, jailbreaks, malicious URLs, and sensitive data. Code execution runs model-generated Python in a Google-managed sandbox, and Security Command Center plus Cloud Audit Logs provide visibility because every call now comes from a named identity.
The closing checklist fits in about 20 minutes: authenticate the team, grant roles/aiplatform.user to one group, enable partner models, move shared test keys into Secret Manager; deploy the app with a service account that holds only that role; set spend caps below what you can afford to lose; run the key scanner; then delete every AI Studio key that ever left a laptop. Runnable samples and offline tests for every step are in the companion repo.

