Google Cloud Platform (GCP)¶
Cloud infrastructure and services configuration guide for Google Cloud.
Overview¶
Google Cloud Platform excels in data analytics, machine learning, and Kubernetes. This section covers setup, CLI configuration, and best practices for GCP development — including AI/LLM API access via Vertex AI.
Phase 1: Install gcloud CLI¶
On Ubuntu/Debian, install the Google Cloud SDK via apt:
1 2 3 4 5 6 7 8 9 10 11 | |
Verify the installation:
1 | |
Phase 2: Create a Project and Link Billing¶
You need a project and an attached billing account before you can use Vertex AI or any paid GCP service.
Log in¶
1 | |
This opens a browser window for authentication. In headless environments (e.g. WSL without a browser), append --no-launch-browser and follow the printed URL manually.
Use an existing project¶
If you already have a GCP project, list your projects and activate the one you want:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Then skip ahead to link billing if not already linked, or go straight to Phase 3.
Create a new project¶
Choose a globally unique project ID (lowercase letters, digits, hyphens):
1 2 | |
Link a billing account¶
1 2 3 4 5 | |
Billing required
Vertex AI usage incurs charges. Without a linked billing account, API calls will fail with a 403 error even after enabling the service.
Phase 3: Enable the Vertex AI API and Create an API Key¶
Enable Vertex AI¶
1 | |
Create an API key¶
1 | |
Retrieve the key string¶
1 | |
Look for the KEY_STRING column in the output. Copy that value — it starts with AIzaSy.... You will need it in Phase 5.
Phase 4: Restrict the API Key (Security)¶
An unrestricted API key can be used to call any enabled GCP service. Restrict yours so it can only invoke Vertex AI.
Find the key's resource name¶
From the gcloud services api-keys list output, copy the NAME field. It looks like:
1 | |
Apply the restriction¶
1 2 | |
Replace KEY_NAME with the full resource name from the previous step.
Why this matters
If your key leaks (e.g. committed to a public repo), an attacker can only call Vertex AI — not spin up VMs, modify storage, or access other services. Always restrict keys to the minimum required surface.
Phase 5: Configure LibreChat (.env)¶
Add these variables to your LibreChat .env file:
1 2 3 4 5 6 7 8 | |
403 Permission Denied?
This usually means one of:
- Billing is not linked to the project
- The API key restriction is blocking the call (check the service name matches)
- The Vertex AI API was not enabled (
gcloud services enable aiplatform.googleapis.com)
Optional: Set a Budget Alert¶
Protect yourself from unexpected charges by creating a billing alert via the CLI. Google will email you when spending crosses your threshold.
1 2 3 4 5 6 7 8 9 10 11 | |
Note
Budget alerts require the billing.budgets.create IAM permission on the billing account. If you get a permission error, create the alert in the GCP Console under Billing → Budgets & alerts instead.
Troubleshooting¶
invalid_grant — Stale or Expired Tokens¶
The invalid_grant error means your local security tokens are stale or corrupted. Common causes:
- You haven't logged in for a while and the tokens expired
- Your Google account password changed, immediately invalidating all local tokens
- Your project is in "Testing" mode on the OAuth Consent Screen — user tokens expire every 7 days in this mode
- You manually revoked the "Google Cloud SDK" app from your Google Account security settings
Fix: Refresh both login types¶
Run these two commands in order. Each opens a browser window — use the same Google account for both:
1 2 3 4 5 | |
Fix: Re-set the quota project¶
After refreshing tokens, re-link your project for billing attribution:
1 | |
To keep the CLI config and ADC in sync at all times, also set:
1 | |
Verify everything is working¶
1 | |
If this prints a long string starting with ya29..., you are back in business.
Understanding the two login commands
gcloud auth login— authenticates you as a user for runninggcloudCLI commandsgcloud auth application-default login— creates credentials that applications and SDKs (like LibreChat) use at runtime
These are stored separately and both can expire independently, which is why you need to refresh both.
Stop tokens expiring every 7 days
If your project's OAuth Consent Screen is set to "Testing", tokens expire weekly. To fix this permanently, go to APIs & Services → OAuth Consent Screen in the GCP Console and publish the app to "Production" (no formal review needed for internal/personal projects).
Best Practices¶
Key hygiene¶
- ✅ DO: Restrict every API key to specific services
- ✅ DO: Rotate keys regularly and revoke unused ones
- ❌ DON'T: Commit API keys to version control — use
.envfiles excluded by.gitignore
Project isolation¶
- ✅ DO: Use separate projects for dev, staging, and production
- ✅ DO: Set per-project billing budgets and alerts
- ❌ DON'T: Share credentials across unrelated applications
Least privilege¶
- ✅ PREFER: Service accounts with narrowly scoped IAM roles over API keys for server-to-server calls
- ✅ PREFER: Workload Identity Federation for CI/CD pipelines instead of long-lived keys
LibreChat Configuration¶
The "Two Geminis" Problem — 403 Generative Language API Disabled¶
If LibreChat throws an error like:
1 2 3 | |
The giveaway is the domain generativelanguage.googleapis.com. Google exposes Gemini through two completely separate API endpoints:
| Door | API | Used for |
|---|---|---|
| AI Studio | generativelanguage.googleapis.com |
Rapid prototyping, third-party apps (LibreChat with GOOGLE_KEY) |
| Vertex AI | aiplatform.googleapis.com |
Enterprise features, service accounts |
When LibreChat is configured with GOOGLE_KEY, it uses the AI Studio door — even if you are running inside a Google Cloud project. That endpoint must be explicitly enabled.
Fix: Enable the Generative Language API¶
1 | |
Wait 2–3 minutes for the change to propagate across Google's global infrastructure, then retry in LibreChat.
Fix: Update your API key restriction¶
If you followed Phase 4 and restricted your key to aiplatform.googleapis.com only, the key is now blocked for the AI Studio endpoint. Add generativelanguage to the allowed list:
1 2 3 4 | |
Warning
You must specify both targets in a single --api-target update — omitting one will remove it from the allowed list.
3-step checklist if it still fails¶
- Wait — API activation can take a few minutes to propagate
- Check the model ID — use the exact model string LibreChat expects (e.g.
gemini-2.0-flash,gemini-1.5-pro) - Verify the key restriction — run
gcloud services api-keys listand confirm both APIs appear under the key's restrictions
Test the key manually¶
Use curl to confirm the key itself works before debugging LibreChat:
1 2 3 | |
A successful response lists available models. A 403 means the key or API is still misconfigured.