Gemini API Quota Exceeded in Apps Script: Backoff Plan

AI

Apps Script quota and backoff troubleshooting

If you see “quota exceeded” while calling Gemini from Google Apps Script, the script has usually sent too many requests for the active API project or model. The most common cause is a burst of calls from a loop, trigger, or multiple users that hits a per-minute limit—not a problem with ChatGPT, Copilot, Claude, or another assistant.

Why this happens

Gemini API limits depend on the project, model, and current quota settings. Apps Script can make a burst look larger than expected when several executions run at once, and repeated retries can make the burst worse.

  • Requests or tokens per minute reach the model’s limit.
  • Daily usage reaches a project limit or available billing quota.
  • Concurrent script executions send requests at the same time.

1. Confirm which project and model the script uses

Open the script at script.google.com, then select Project Settings and check the associated Google Cloud project. In Google AI Studio, confirm the API key belongs to that project; then check the model name in the request URL or payload. The fix is confirmed when the key, project, and model match the quota page you are inspecting.

2. Check the model’s current quota

Open Google AI Studio’s API key and usage or rate-limit information, or use the linked Google Cloud project’s quota pages when applicable. Check the selected model’s request and token limits, usage, and any billing status shown for that project. If usage is at a limit, wait for its reset or request higher access where offered; a limit shown for another project or model does not explain this script’s error.

3. Stop overlapping executions

In the Apps Script editor, open Executions to see whether manual runs or time-driven triggers overlap. Review Triggers from the left sidebar and remove duplicate schedules or reduce their frequency while diagnosing. If executions no longer overlap and the error stops during the same workload, concurrency was contributing to the quota hit.

4. Add exponential backoff for temporary limits

For a retryable rate-limit response, wait progressively longer before retrying, and cap the number of attempts. For example, start with a short delay and double it after each failed attempt, adding a small random delay so parallel executions do not retry together. In Apps Script, use Utilities.sleep(milliseconds) within a bounded retry loop; inspect the HTTP status and response body first, and do not retry authentication, invalid-request, or other permanent errors. A successful later attempt confirms the limit was temporary, but backoff does not increase the quota.

5. Reduce calls and tokens per execution

Combine related inputs into fewer requests when the model and task allow it, trim unnecessary prompt text, and avoid sending the same context repeatedly. If processing a sheet, read a range once with getValues(), prepare the work in memory, and write results back in a batch instead of making a Gemini call for every cell. A lower request count or token use in the project’s usage view confirms the change is helping.

6. Queue work and pace requests

For larger jobs, split items into manageable batches and process them over time instead of launching one request per row in a single run. Store a progress marker in PropertiesService and resume from a time-driven trigger, leaving enough time between batches for the applicable limit to recover. Confirm the queue advances without repeated quota errors and that each item is processed only once.

7. Check the exact response before changing billing

Log the response code and a shortened response body, but never log your API key. A rate-limit message points toward pacing or quota; an authentication or permission message calls for checking the key and project instead. Do not assume enabling billing will remove every limit: verify what the active project’s quota page says and follow its stated requirements.

Likely causeTypical symptomQuick test
Per-minute request or token limitFails during a burst, then works laterRetry one request after a pause
Overlapping executionsErrors coincide with triggers or multiple usersCheck Apps Script Executions
Daily or project limitRequests keep failing until usage resets or access changesInspect quota for the active project and model

FAQ

How do I fix gemini app script quota exceeded?

Check the active project and model, then compare their current quota with usage. Reduce concurrent calls and add bounded backoff for temporary rate limits.

Will exponential backoff fix a Gemini API quota error?

It can help when a temporary rate limit clears after a short wait. It will not fix exhausted daily quota, a disabled service, an invalid key, or a permanent permission error.

Why does Gemini work in AI Studio but fail in Apps Script?

The two tests may use different API keys, projects, models, or request volumes. Verify the key’s project and model in the script, then inspect quota for that exact combination.

Most Apps Script quota errors clear once you identify the active limit, reduce bursts, and retry temporary failures with bounded backoff. If nothing worked, save the response details without the key and compare them with the current project and model quota information before changing the integration.

Photo: Shixart1985 / CC BY 2.0 (Wikimedia Commons)

Leave a Comment