Handling Gemini API Key Rotation and Rate Limits Across Cron Jobs in Next.js
The Problem When running scheduled influencer data processing cron jobs in a Next.js application that process dozens of YouTube creator profiles per minute, hitting Gemini API rate limits (429 Too Many Requests) will a
The Problem
When running scheduled influencer data processing cron jobs in a Next.js application that process dozens of YouTube creator profiles per minute, hitting Gemini API rate limits (429 Too Many Requests) will abruptly halt your pipeline halfway through execution.
At PulseROI, while building our 24/7 post-campaign monitoring for Indian D2C brands, our single-key Gemini setup kept throwing RESOURCE_EXHAUSTED errors right when the evening engagement spike hit. The cron job would crash, leaving database rows in a half-processed state with incomplete metrics.
What I Tried First (and Why It Failed)
My first approach was simple: catch the error in a try/catch block, wait 2 seconds using setTimeout, and retry the request.
async function callGemini(prompt: string, retries = 3) {
try {
return await aiModel.generateContent(prompt);
} catch (err) {
if (retries > 0) {
await new Promise(res => setTimeout(res, 2000));
return callGemini(prompt, retries - 1);
}
throw err;
}
}
This failed in production. Vercel Hobby serverless functions have execution limits, and waiting 2 seconds across 50 sequential items added 100+ seconds of dead time, eventually breaching function execution duration or hitting global project-level quotas anyway. A single hardcoded key simply cannot sustain high-throughput LLM tasks.
The Actual Fix
I built a round-robin key rotator that accepts a comma-separated environment variable of API keys, tracks index state, and gracefully switches keys upon encountering quota errors without sleeping the execution thread.
import { GoogleGenAI } from '@google/genai';
class GeminiKeyPool {
private keys: string[];
private currentIndex = 0;
constructor() {
const rawKeys = process.env.GEMINI_API_KEYS || '';
this.keys = rawKeys.split(',').map(k => k.trim()).filter(Boolean);
if (this.keys.length === 0) {
throw new Error('No Gemini API keys provided in GEMINI_API_KEYS');
}
}
private getNextClient() {
const key = this.keys[this.currentIndex];
this.currentIndex = (this.currentIndex + 1) % this.keys.length;
return new GoogleGenAI({ apiKey: key });
}
async generateWithRotation(prompt: string, maxRetries = 3): Promise<string> {
let attempts = 0;
while (attempts < maxRetries * this.keys.length) {
attempts++;
const ai = this.getNextClient();
try {
const response = await ai.models.generateContent({
model: 'gemini-2.5-flash',
contents: prompt,
});
if (response.text) return response.text;
} catch (error: any) {
const isRateLimit = error?.status === 429 || error?.message?.includes('RESOURCE_EXHAUSTED');
if (isRateLimit && attempts < maxRetries * this.keys.length) {
console.warn(`[GeminiPool] Rate limit hit. Rotating key index to ${(this.currentIndex)}`);
continue;
}
throw error;
}
}
throw new Error('All Gemini keys exhausted rate limit quotas.');
}
}
export const geminiPool = new GeminiKeyPool();
By passing tasks through geminiPool.generateWithRotation(prompt), cron executions switch keys instantly on the next iteration rather than idling on exponential backoff timeouts.
What Surprised Me
Even with multiple keys distributed across different Google Cloud projects, Gemini's per-minute token limits apply strictly to model input size combined with output tokens. We noticed that passing raw transcripts of 20-minute YouTube creator videos consumed quota four times faster than sending pre-summarized metadata JSONs. Optimizing the payload size reduced our key rotation frequency by 70%.
The Result
Our cron job completion time dropped from intermittent 504 Gateway Timeouts to a steady ~14 seconds for batch processing 30 creator profiles. Zero 429 pipeline failures occurred over the last 72 hours of continuous monitoring runs.
Where to Go From Here
If you are scaling background worker tasks in Next.js, make sure your environment variables support key arrays from day one. To see how we hook this resilient LLM pipeline into real-time campaign tracking dashboards, explore pulseroi.in.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.