48.3 Error patterns and what they usually mean

Overview and links for this section of the guide.

Error Patterns

Error Likely Cause Fix
QUOTA_EXCEEDED Rate limit hit Add retry with backoff, increase quota
CONTEXT_LENGTH_EXCEEDED Input too long Truncate context, summarize, use RAG
INVALID_JSON Model didn't output valid JSON Use responseMimeType, add examples
SAFETY_BLOCKED Content triggered safety filter Review input, adjust safety settings
TIMEOUT Request took too long Use smaller model, reduce context
MODEL_NOT_FOUND Invalid model name Check model name spelling
AUTHENTICATION_ERROR Invalid API key Check API key is valid and has permissions
// Retry pattern for transient errors
async function withRetry(fn, maxRetries = 3) {
  for (let i = 0; i < maxRetries; i++) {
    try {
      return await fn();
    } catch (e) {
      if (!isRetryable(e) || i === maxRetries - 1) throw e;
      await sleep(1000 * Math.pow(2, i));  // Exponential backoff
    }
  }
}

function isRetryable(error) {
  return ['QUOTA_EXCEEDED', 'TIMEOUT'].includes(error.code);
}

Where to go next