🔴 Setting Multiple API Keys on OpenClaw
Multiple Google Gemini API Keys Setup for OpenClaw on Ubuntu
🎯 Why multi‑key configuration? Eliminate single points of failure, avoid quota exhaustion, and distribute workload across Gemini providers. OpenClaw gains self-healing capabilities for OCR, NLP, Selenium automation, and reasoning chains.
📌 Unified model: gemini-2.5-flash — stable, cost‑efficient & fast for agent workflows.
Prepare Ubuntu Environment
Run system updates and install core tooling:
sudo apt update && sudo apt upgrade -y
sudo apt install -y python3 python3-pip curl git nano jq
Create OpenClaw Structure + Virtual Environment
mkdir -p ~/openclaw/{config,scripts,logs,workspace}
Tree layout: ~/openclaw/ ├── config/ ├── logs/ ├── scripts/ └── workspace/
cd ~/openclaw
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
Obtain Google Gemini API Keys
Visit Google AI Studio → Create 3 distinct keys:
KEY_1KEY_2KEY_3AIzaSy_PRIMARY , AIzaSy_FALLBACK , AIzaSy_BACKUP (replace with real keys)
OpenClaw Configuration (JSON)
Create ~/openclaw/config/openclaw.json with multi-provider fallback chain:
{
"providers": {
"google-1": { "type": "google", "apiKey": "AIzaSy_PRIMARY" },
"google-2": { "type": "google", "apiKey": "AIzaSy_FALLBACK" },
"google-3": { "type": "google", "apiKey": "AIzaSy_BACKUP" }
},
"agents": {
"defaults": {
"model": {
"primary": "google-1/gemini-2.5-flash",
"fallbacks": [
"google-2/gemini-2.5-flash",
"google-3/gemini-2.5-flash"
]
}
}
}
}
Save: CTRL+O → ENTER → CTRL+X
Fallback Mechanism & Workflow
1️⃣ google-1 (primary)
2️⃣ google-2 (fallback)
3️⃣ google-3 (emergency)
Request → google-1 ✅
↳ Quota/429/timeout → google-2 ✅
↳ Still failed → google-3 ✅
Add Environment Variables (.bashrc)
nano ~/.bashrc
Append these lines:
export GOOGLE_API_KEY_1="AIzaSy_PRIMARY"
export GOOGLE_API_KEY_2="AIzaSy_FALLBACK"
export GOOGLE_API_KEY_3="AIzaSy_BACKUP"
source ~/.bashrc
echo $GOOGLE_API_KEY_1 # verification
Test Script & Validation
Create ~/openclaw/scripts/test_provider.py to inspect config:
import json
config_path = "/home/$USER/openclaw/config/openclaw.json"
with open(config_path, "r") as f:
config = json.load(f)
providers = config["providers"]
for name, provider in providers.items():
print(f"Provider: {name}")
print(f"Type: {provider['type']}")
print(f"API Key: {provider['apiKey'][:12]}...")
print("-" * 30)
Expected output:
Provider: google-1
Type: google
API Key: AIzaSy_PRIM...
...
Provider: google-2
API Key: AIzaSy_FALL...
Install AI Libraries & Routing Example
pip install google-generativeai
pip install selenium pillow opencv-python pytesseract pandas requests beautifulsoup4
Simple router example (~/openclaw/scripts/router.py):
import random
providers = ["google-1", "google-2", "google-3"]
selected = random.choice(providers)
print(f"Using provider: {selected}")
Restart Service & Debugging
If OpenClaw runs via systemd user service:
systemctl --user restart openclaw
systemctl --user status openclaw
Debug commands:
cat ~/openclaw/config/openclaw.json
journalctl --user -u openclaw -f
grep -i quota ~/openclaw/logs/*
jq . ~/openclaw/config/openclaw.json
Production Best Practices
| Provider | Recommended Role |
|---|---|
google-1 | User chat + interactive tasks |
google-2 | Automation (OCR, scraping, selenium) |
google-3 | Emergency fallback / critical workflows |
✅ Always use the same model (gemini-2.5-flash) for consistent behavior across fallbacks.
✅ Isolate heavy workloads (document parsing, reasoning chains) from real-time chat to avoid cross‑quota interference.
Final Verification & Readiness
python3 -c "print('OpenClaw Ready')"
jq . ~/openclaw/config/openclaw.json
python3 ~/openclaw/scripts/test_provider.py
✨ With this setup OpenClaw automatically rotates between Gemini keys — your AI workflows become resilient, scalable, and quota‑proof.
Provider architecture summary
google-1 → google-2 → google-3 | Same model: gemini-2.5-flash
Automatic fallback triggers on: quota exceeded, HTTP 429, network timeout, or 5xx errors.
Ideal for OCR/NLP pipelines, browser automation, and agentic workflows.
Comments