Overview
Gemini's grounded search regularly returns 429 rate limit and 503 service unavailable errors during peak traffic, leaving your agent without web data. This workflow detects Gemini grounding failures in real time, routes the same query to Scavio's search API as a fallback, logs every fallback event for capacity planning, runs periodic health checks against Gemini, and auto-recovers to the primary path once Gemini stabilizes. Your agent never goes blind, and you get full visibility into Gemini reliability.
Trigger
Gemini API returns 429 or 503 error
Schedule
Event-driven (on Gemini error)
Workflow Steps
Detect Gemini error
Intercept 429 (rate limit) or 503 (service unavailable) responses from Gemini grounding API calls.
Route to Scavio fallback
Re-execute the same search query through Scavio's API. Map Gemini grounding format to Scavio response format.
Log fallback event
Record the timestamp, error code, query, and latency delta for capacity planning and SLA tracking.
Health check Gemini
Every 60 seconds, send a lightweight probe to Gemini. Track consecutive failures to detect extended outages.
Auto-recover to primary
Once Gemini returns 3 consecutive healthy responses, switch traffic back from Scavio to Gemini as primary.
Python Implementation
import requests, os, json, time
from datetime import datetime
H = {"x-api-key": os.environ["SCAVIO_API_KEY"]}
fallback_log = []
def search_with_fallback(query, primary="gemini"):
"""Try Gemini grounding first, fall back to Scavio on error."""
start = time.time()
if primary == "gemini":
# Simulate Gemini call - replace with actual Gemini grounding API
gemini_healthy = False # Set based on actual Gemini response
if not gemini_healthy:
# Fallback to Scavio
r = requests.post("https://api.scavio.dev/api/v1/search", headers=H,
json={"platform": "google", "query": query, "ai_overview": True},
timeout=10).json()
latency = round(time.time() - start, 3)
fallback_log.append({
"ts": datetime.utcnow().isoformat(),
"query": query,
"error": "gemini_429",
"fallback_latency_s": latency,
"scavio_results": len(r.get("organic", []))
})
return {"source": "scavio_fallback", "results": r.get("organic", [])[:5],
"ai_overview": r.get("ai_overview", {}).get("text", "")}
return {"source": "gemini_primary", "results": []}
def health_check_gemini():
"""Probe Gemini health. Returns True if healthy."""
# Replace with actual Gemini health check
return False
# Run fallback chain
queries = ["search api pricing 2026", "best serp api for agents", "web search api comparison"]
for q in queries:
result = search_with_fallback(q)
print(f"[{result['source'].upper()}] {q}: {len(result['results'])} results")
print(f"\nFallback events logged: {len(fallback_log)}")
for event in fallback_log:
print(f" {event['ts']} | {event['query']} | {event['fallback_latency_s']}s")JavaScript Implementation
const H = {"x-api-key": process.env.SCAVIO_API_KEY, "Content-Type": "application/json"};
const fallbackLog = [];
async function searchWithFallback(query, primary = "gemini") {
const start = Date.now();
if (primary === "gemini") {
// Simulate Gemini call - replace with actual Gemini grounding API
const geminiHealthy = false; // Set based on actual Gemini response
if (!geminiHealthy) {
const r = await fetch("https://api.scavio.dev/api/v1/search", {
method: "POST", headers: H,
body: JSON.stringify({platform: "google", query, ai_overview: true})
}).then(r => r.json());
const latency = ((Date.now() - start) / 1000).toFixed(3);
fallbackLog.push({
ts: new Date().toISOString(), query,
error: "gemini_429", fallbackLatencyS: latency,
scavioResults: (r.organic || []).length
});
return {source: "scavio_fallback", results: (r.organic || []).slice(0, 5),
aiOverview: r.ai_overview?.text || ""};
}
}
return {source: "gemini_primary", results: []};
}
async function healthCheckGemini() {
// Replace with actual Gemini probe
return false;
}
(async () => {
const queries = ["search api pricing 2026", "best serp api for agents", "web search api comparison"];
for (const q of queries) {
const result = await searchWithFallback(q);
console.log(`[${result.source.toUpperCase()}] ${q}: ${result.results.length} results`);
}
console.log(`\nFallback events: ${fallbackLog.length}`);
fallbackLog.forEach(e => console.log(` ${e.ts} | ${e.query} | ${e.fallbackLatencyS}s`));
})();Platforms Used
Web search with knowledge graph, PAA, and AI overviews