Urgent.News

What's breaking now, across thousands of outlets.

Tech

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 Next.js on Vercel, hitting a Gemini API rate limit will abruptly kill your background run. Standard single-key environment variables fail instantly once you scale up creator analysis requests, returning a 429 Too Many Requests status and leaving your database records in a half-updated state. At PulseROI, while building our…

Dealing with Gemini API Key Rotation and Rate Limits in Next.js Cron Jobs

When running scheduled influencer data processing tasks in Next.js on the Vercel platform, encountering a Gemini API rate limit can abruptly terminate a background job. Utilizing standard single-key environment variables becomes ineffective as the number of creator analysis requests increases, resulting in a 429 Too Many Requests status and leaving database records in an incomplete state.

During the development of a YouTube creator intelligence platform for Indian Direct-to-Consumer (D2C) brands at PulseROI, the nightly creator ingestion pipeline would frequently crash midway through batches. A single API key proved insufficient to handle the required token volume for processing hundreds of creator transcripts and engagement metrics within a specific execution timeframe.

A need arose for a systematic method to rotate through multiple keys seamlessly without disrupting requests or causing the cron handler to crash.

Initial Attempts (and Why They Failed)

The initial strategy involved storing a comma-separated list of keys in the process.env.GEMINI_API_KEYS variable and selecting a random index on each request using Math.random(). However, this approach proved ineffective in production for two reasons. Firstly, Math.random() lacks the capability to remember rate limits, often selecting a key that had already been throttled just moments earlier, exacerbating 429 errors.

Secondly, sequential processing of large batches of creator profiles within the same key led to hitting concurrency limits within seconds. This random picker provided no sequential failover protection.

The Actual Solution

To address these issues, a round-robin key manager class was developed, tracking usage count, catching 429 status codes explicitly, and rotating to the next available key while maintaining a fallback queue. This class, named GeminiKeyRotator, was implemented in lib/gemini-rotator.ts. It initializes with process.env.GEMINI_API_KEYS, splits the string into an array, trims whitespace, and filters out any empty entries. If no valid keys are provided, an error is thrown.

The getActiveKey() method retrieves the current key and index, ensuring that exhausted keys are skipped. The markExhausted(index) method marks a key as exhausted, and the reset() method clears the set of exhausted keys.

In the services/creator-analysis.ts file, a helper function generateWithFailover(prompt) was created, using an instance of GeminiKeyRotator to automatically handle the rotation process when a 429 status code is encountered. This solution effectively mitigates the risk of hitting rate limits and ensures that the cron handler continues to function smoothly, even under heavy loads.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

How to use Synthetic Credit Data API: synthetic credit data for safer ML

Introduction – Why Synthetic Credit Data Matters Fintech applications, credit‑scoring models, and risk‑management pipelines need realistic transaction and borrower data to train, test, and validate…

  • CredSynth API generates realistic credit profile data without breaching privacy regulations
  • Python script fetches synthetic credit data via RESTful interface with JSON response
  • API endpoint allows customization of record count, seed, region bias, and risk profile

SEO Basics in TanStack Start: Meta Tags, robots.txt and a Dynamic Sitemap

Prefer to watch? Here is the video version of this tutorial: What is SEO? SEO stands for Search Engine Optimization. It is the practice of improving a website so it ranks higher in unpaid ("organic")…

  • Add meta tags in About page head section
  • Create seo.ts file with seo function
  • Use seo function to generate meta tags

Why South Korean Households May Not Feel the Semiconductor Boom

South Korea's semiconductor boom could lift economic growth above 4% in 2027 without delivering comparable improvements in household spending or employment.

  • South Korea's semiconductor industry growth may not boost household spending or employment.
  • Higher corporate earnings may not lead to higher wages or job creation across other industries.

More from Tuesday 17 November →