OverpayingForAIPricing desk

Lesson 7 of 8 · 12 min read · Beginner → Intermediate

Gemini API quickstart

When the API beats a subscription, how to make your first call with curl and Python, and the two features — context caching and batch mode — that cut the bill.

In this lesson you will

  • Get an API key and make a working Gemini call in under ten minutes.
  • Explain tokens and context in enough depth to estimate a bill.
  • Use context caching and batch mode to lower cost on repeat work.

The Gemini API is where the cheapest Gemini lives. There is no monthly fee, a free tier in Google AI Studio for testing, and per-token prices that start around $0.10 per million input tokens on Flash Lite at the time of writing. If any of your work is repeatable, this lesson will probably pay for itself in a month.

When the API beats the subscription

  • You run the same task many times (summaries, extraction, classification, translation).
  • Your usage is bursty — heavy one week, nothing the next — so a flat fee is wasted.
  • You want a specific model, not whatever the app decides to give you.
  • You want to build Gemini into a script, spreadsheet, or product.
  • Your monthly token cost on Flash is below the subscription price — which for most people it is.
Tokens and the context windowA token is roughly ¾ of an English word. The context window is the maximum tokens the model can hold at once — everything you send plus everything it writes.System prompt + instructionsConversation history + documentsNew answerfreeWhy it costs moneyEvery turn re-sends the whole history. A 40-message chat with a pasted PDF bills that PDF 40 times unless the provider caches it.“1M context” is a ceiling, not a target. Filling it on every request is the fastest way to overpay.rule of thumb: 1,000 tokens ≈ 750 words ≈ 1.5 pages
Figure 1.Tokens are the billing unit. Everything you send is input; everything the model returns is output. On Gemini, a very large context window means you can send a lot, but each token is billed on every call unless it is cached.

Step 1: get a key

Go to Google AI Studio, sign in, and create an API key. The free tier lets you test without a card; enabling billing moves you to paid rates and higher limits. Store the key in an environment variable named GEMINI_API_KEY. Never paste it into code you commit.

Step 2: your first call with curl

bashMinimal generateContent request. Swap the model id for any current Gemini model.
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -X POST \
  -d '{
    "contents": [{
      "parts": [{ "text": "In two sentences, explain what a token is." }]
    }]
  }'

The response is JSON. The text lives under candidates[0].content.parts[0].text, and usageMetadata reports the prompt and output token counts — read it, because that is your bill.

Step 3: the same call in Python

pythonOfficial google-genai SDK. Install with pip install google-genai. The client reads GEMINI_API_KEY from the environment.
from google import genai

client = genai.Client()

response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="In two sentences, explain what a token is.",
)

print(response.text)
print(response.usage_metadata)  # prompt and output token counts

Context caching

If you send the same large block of context repeatedly — a manual, a codebase, a long system prompt — Gemini can cache it and charge a much lower rate for the cached tokens on later calls. Our catalogue lists a cached input price for the current models: Gemini 3.1 Pro Preview at $0.20 per million cached tokens against $2.00 standard at the time of writing, and Gemini 3.8 Flash at $0.075 against $0.75. Cached content is stored for a time you set and there is a storage charge; see the official caching docs for the current rules.

Prompt caching: stop paying full price for the same prefixIf the start of your request is identical each time (system prompt, docs, tool list), the provider can serve it from cache at a fraction of the input price.Request 1Stable prefix — written to cache (full price)new questionRequest 2Stable prefix — cache hit (discounted)new questionRequest 3Stable prefix — cache hit (discounted)new questionRule: put the unchanging parts first, the changing parts last. A timestamp at the top of a system prompt silently kills the cache on every call.
Figure 2.With caching, the shared prefix is paid once at full price and then billed at the cached rate on each reuse. The saving grows with the size of the shared context and the number of calls.

Batch mode

If you do not need answers immediately, batch mode lets you submit many requests together and collect results later at a lower rate. Our catalogue carries separate batch rows — Gemini 3.1 Pro Preview (batch) is listed at $1.00 in and $6.00 out against $2.00 and $12.00 standard — which is a halving on that model at the time of writing. Overnight summaries, backfills and bulk classification are the natural fit. Details in the official batch docs.

Knowledge check

You send the same 200,000-token product manual with every customer query. What is the biggest single cost saving available?

Lesson FAQ

Is the Gemini API free?

There is a free tier in Google AI Studio with lower rate limits, suitable for testing and small projects. Paid usage is billed per token at the rates on the [Gemini pricing page](/pricing/gemini-pricing).

Gemini API or Vertex AI?

Same models. Google AI Studio and the Gemini API are the fast path for individuals and small teams. Vertex AI is Google Cloud's enterprise route with more governance. Start with AI Studio unless your company already runs on Google Cloud.

Can I call Gemini through OpenRouter or another gateway?

Yes; our catalogue prices are taken from OpenRouter's public listing. A gateway adds convenience and sometimes a small markup. Compare against the official page before you commit to volume.

Finished reading?

Mark it done to track your progress through the course.

Compare, calculate, decide — for Gemini

If our calculators helped you cut down on hidden AI wallet leaks, consider buying us a coffee. A tiny fraction of your savings keeps our pricing indexes updated daily.

Not sure which AI is cheapest for your use case? Find out in 30 seconds — no signup required.

AI cost intelligence

Stop overpaying for AI tools

Join the OverpayingForAI list for pricing updates, cheaper alternatives, and practical buying guidance.

Now tracking 50+ AI tools, models, platforms, subscriptions, coding tools, and automation products.

We use your email only for OverpayingForAI updates. Unsubscribe anytime.