Zum Inhalt springen

Lektion 21 · The Gemini API

API Costs, Rate Limits and Errors

Estimate Gemini API costs from token usage, understand free and paid tiers, caching, batch pricing and rate limits, and retry errors the safe way.

A demo that works can still surprise you with a large bill, or fail when traffic grows. Before you ship, learn how the Gemini API is billed, which rate limits apply and which errors you should retry.

All numbers here are as of December 2026, from Google's pricing and documentation pages. Check the current pages before you plan a budget.

Tokens and prices

You usually pay per token, with separate prices for input and output, and output prices include thinking tokens. Standard paid prices per 1 million tokens:

Model Free tier Input (USD) Output (USD)
gemini-4-carbon No 4.00 20.00
gemini-4-neon No 3.00 15.00
gemini-4-hydrogen Yes 1.50 7.50
gemini-3.8-flash Yes 0.75 through Dec 31, 2026, then 1.50 3.75 through Dec 31, 2026, then 7.50
gemini-3.5-flash-lite Yes 0.30 2.50

Some input types, features and service options are priced differently, and tools such as search can have their own charges. According to the pricing page, free-tier content for these free models is used to improve Google's products, while paid-tier content is not.

Estimate a monthly bill

Python Estimate monthly API cost

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Include thinking tokens in your output estimate. Different models can also use different numbers of tokens for the same task, so measure real requests. interaction.usage has fields such as total_input_tokens, total_output_tokens, total_thought_tokens and total_cached_tokens.

Ways to spend less

  1. Choose the lightest model that passes your tests.
  2. Lower the thinking level for simple tasks.
  3. Send less. Include only the context the task needs.
  4. Use caching. For Gemini 4 models, Google's long-context guide says implicit caching applies once a shared prompt prefix reaches 4,096 tokens, with a large discount on matching input tokens and no storage fee. Put repeated material first and keep it unchanged. Check total_cached_tokens to see whether it worked.
  5. Use the Batch API for work that can wait. It is billed at 50% of the standard price, with a target turnaround of 24 hours. Google's batch documentation says it is available with the generateContent API, not the Interactions API.

Rate limits

Limits are usually measured in requests per minute (RPM), input tokens per minute (TPM) and requests per day (RPD). Going over any one of them causes an error. Limits apply per project, not per API key, and daily request quotas reset at midnight Pacific time. Paid usage tiers raise limits as your account qualifies, and there are also spend-based limits. Look up your project's current limits in AI Studio instead of copying numbers from articles.

Common errors

Status Meaning What to do
400 Bad request, such as an unsupported parameter Fix the request; do not resend it unchanged
402 Prepaid credits are used up Check billing
403 Permission denied, for example a blocked key or a project problem Fix the cause
408 Timeout Retry with backoff
429 A rate or spend limit was reached Wait, retry with backoff, reduce load
5xx A temporary service problem Retry with backoff

A 400 error can also come from sending temperature, top_p or top_k to Gemini 4 or to Gemini 3.6 and later. A safety block also arrives as a 400 error with a code that says why. Show a helpful message instead of retrying the same input.

Retry the right way

Google's guidance is to retry only temporary errors (408, 429 and 5xx), wait longer each time, add random jitter and set a maximum number of attempts. It also says the Python SDK already retries temporary errors automatically. If you add your own retry loop around it, count the total number of attempts that can happen.

Python Stand-in: retry with exponential backoff

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This demo skips the waiting by passing a sleep function that does nothing. In real code, pass time.sleep. Also think about actions: retrying a request that led to a payment or a message must not repeat the action.

Protect your budget

  • Set billing budgets and alerts.
  • Limit input size and output length in your app.
  • Add per-user limits, and stop runaway agent loops with a maximum number of rounds.
  • Log usage for each feature so you know where the money goes.

An alert only tells you about spending. Do not assume that it stops spending, and build your own hard limits.

Load and capacity planning

Rate limits matter even when your budget is fine. Estimate your busiest minute, not just your daily average: many users may arrive at once after a newsletter or at the start of a work day. Compare that peak with your project's request and token limits. If it is close, queue work, smooth traffic or move jobs that can wait to batch processing. Show users a clear "please try again shortly" message when your own queue is full.

Common mistake: retrying every error immediately in a loop. It can turn a short outage into a flood of requests.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which errors should you normally retry with backoff?

    These are usually temporary. Errors such as 400 and 403 need a fix first.
  2. 2. According to Google's documentation, what do Gemini API rate limits apply to?

    Creating more keys in one project does not raise its limits.
  3. 3. How can you get a lower price for work that can wait?

    Batch jobs are billed at 50 percent of the standard price.
  4. 4. Do thinking tokens affect cost?

    Google's pricing page says output prices include thinking tokens.

Summary

  • Cost depends on the model, tokens (including thinking) and tools.
  • Save with lighter models, less context, caching and batch jobs.
  • Know your project's rate limits.
  • Retry only temporary errors with backoff, and build hard spending limits.

Next: Checking Answers and Staying Accurate

Dein Fortschritt wird in diesem Browser gespeichert. Kein Konto nötig.