Configuring Environment Variables and Secret Handling in ADK Projects
A practical guide to .env variables in ADK and agents-cli projects: GEMINI_API_KEY vs GOOGLE_* Vertex settings, ADC auth, deploy-time secrets, and telemetry flags.
Published on • November 24, 2026
AI Assistant

ADK projects read configuration from environment variables at three distinct layers: local model calls (API key or Google Cloud project), the coding-agent toolchain around them (agents-cli), and the deployed service. Mixing those layers up is the most common cause of “it works locally, authenticates differently in production” — and of API keys accidentally shipping to Cloud Run.
This tutorial walks the .env contract for scaffolded ADK projects, the two mutually exclusive auth modes, and the deploy-time rules for keeping secrets out of your container image.
The scaffolded .env
adk create my_agent generates a project with agent.py and a .env file. The quickstart’s minimal key setup:
# my_agent/.env (Gemini API key mode)
echo 'GOOGLE_API_KEY="YOUR_API_KEY"' > .env
Agents CLI projects carry a fuller file. The default adk_python template’s .env sets the Google Cloud mode:
# .env — Google Cloud (Gemini platform) mode
GOOGLE_GENAI_USE_ENTERPRISE=true
GOOGLE_CLOUD_PROJECT=your-project-id
GOOGLE_CLOUD_LOCATION=global
| Variable | Purpose | Notes |
|---|---|---|
GEMINI_API_KEY | Gemini API key from AI Studio | GOOGLE_API_KEY also works; no cloud project needed |
GOOGLE_CLOUD_PROJECT | GCP project for Gemini platform calls | Scaffolded by agents-cli |
GOOGLE_CLOUD_LOCATION | Region for Gemini platform models | global by default; not every model ships everywhere |
GOOGLE_GENAI_USE_ENTERPRISE | Routes Python ADK to Gemini platform | Set true for Python projects |
GOOGLE_GENAI_USE_VERTEXAI | Same switch for ADK Go projects | Go also hard-codes genai.BackendVertexAI in app/agent.go |
AGENTS_CLI_TELEMETRY | agents-cli usage telemetry opt-out | 0, false, no, or off (case-insensitive) |
DO_NOT_TRACK | Standard Do Not Track opt-out | export DO_NOT_TRACK=1 |
The key rule from the agents-cli authentication guide: the GOOGLE_* variables take precedence, so switching to an API key means commenting them out rather than adding alongside:
# .env — API key mode for local development
# GOOGLE_GENAI_USE_ENTERPRISE=true
# GOOGLE_CLOUD_PROJECT=your-project-id
# GOOGLE_CLOUD_LOCATION=global
GEMINI_API_KEY="your-key-here"
This swap works in Python projects without code changes. A Go project needs a change to app/agent.go as well, because the generated backend forces Gemini platform. ADK’s own quickstart uses the same idea in reverse: adk deploy cloud_run --with_ui --env GOOGLE_GENAI_USE_ENTERPRISE=1 <agent-folder> shows that adk commands accept --env flags or read a .env file.
Who loads .env, and when
The agents-cli dev commands (playground, run, eval) load the project-root .env automatically, which is why the quickstart never calls python-dotenv by hand. For plain ADK scripts, load it explicitly:
from dotenv import load_dotenv
import os
load_dotenv() # reads .env into the process environment
api_key = os.environ["GEMINI_API_KEY"]
For cloud auth, agents-cli login -i (or gcloud auth login --update-adc) signs in both the gcloud CLI and Application Default Credentials; agents-cli login --status shows which method and project are active. With only ADC configured — a CI runner using GOOGLE_APPLICATION_CREDENTIALS — agents-cli passes an ADC access token to the gcloud commands it runs.
Resolving which mode you are in
Scaffolded projects also carry a .env.example documenting the expected keys. When auth misbehaves, walk ADK’s precedence rather than guessing:
1. GOOGLE_GENAI_USE_ENTERPRISE / GOOGLE_GENAI_USE_VERTEXAI + GOOGLE_CLOUD_PROJECT
set? -> Gemini platform mode (these vars take precedence)
2. Else GEMINI_API_KEY or GOOGLE_API_KEY present?
-> Gemini API mode (AI Studio key, no project needed)
3. Else Application Default Credentials / gcloud
-> cloud mode via ADC
| Activity | Variables that apply |
|---|---|
agents-cli playground, run, eval (local) | Project-root .env, loaded automatically |
adk web / adk run (plain ADK) | .env only if you load it (load_dotenv) or export vars |
agents-cli deploy | Copies .env into the service; ADC required for the deploy itself |
Terraform observability (agents-cli infra) | GOOGLE_CLOUD_PROJECT or ADC as the log destination |
Note the asymmetry: local dev commands read .env for you, but the deploy command itself authenticates through gcloud/ADC — the same credential you would use for gcloud run deploy. A Gemini API key alone is enough for local playground, run, and eval, but never for deployment.
Secrets at deploy time
This is where environment handling goes wrong. From the agents-cli security reference:
agents-cli deploycopies every entry in the project-root.envinto the deployed service’s environment variables on Agent Runtime, Cloud Run, and GKE. Remove API keys and other secrets from.envbefore you deploy.- On Agent Runtime and Cloud Run, pass secrets from Secret Manager with
--secrets ENV=SECRETinstead:
agents-cli deploy --project my-project --secrets GEMINI_API_KEY=projects/my-project/secrets/gemini-key/versions/latest
- Cloud Run services are private by default: every deploy passes
--no-allow-unauthenticated, and callers need an identity token (agents-cli run "PROMPT" --url SERVICE_URL --mode a2aadds one for you). - A bare
agents-cli deploysetsADK_CAPTURE_MESSAGE_CONTENT_IN_SPANS=falseunless you set it yourself, keeping prompt content out of trace spans.
Two related pitfalls from the deployment guide: on Agent Runtime with Agent Identity, the image starts once before its env vars and secrets are applied, so the container must boot without them; and if you use Terraform-managed observability, run agents-cli infra single-project --apply before your first deploy, never after — Terraform cannot take over a resource agents-cli deploy already created.
For CI, the same discipline applies: inject GEMINI_API_KEY or ADC from your CI secret store, never from a committed .env, and remember agents-cli telemetry logs only metadata (command name, exit code, duration) to GOOGLE_CLOUD_PROJECT — file contents, arguments, and secrets are never recorded. Once these variables are in place, the prompts they unlock become your next audit surface; context-engineering-managing-what-agent-sees.md covers what should and shouldn’t reach the model from there.
Takeaway: ADK configuration is layer-correct only when GEMINI_API_KEY/GOOGLE_API_KEY and the GOOGLE_CLOUD_* + GOOGLE_GENAI_USE_ENTERPRISE (or GOOGLE_GENAI_USE_VERTEXAI for Go) blocks are mutually exclusive in .env, the file is stripped of secrets before agents-cli deploy copies it wholesale into the service, and real secrets arrive via Secret Manager --secrets ENV=SECRET or ADC instead.