Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 45 ยท AI on Google Cloud

Vertex AI and the Gemini API

Learn what Vertex AI is, how Gemini models are called from code, how prompts, grounding, tokens, costs and quotas work, with a runnable cost estimator stand-in.

Vertex AI is Google Cloud's platform for building with machine learning and generative AI. It brings together model access, training, deployment, evaluation and monitoring under one project, one IAM system and one bill. The best-known models you reach through it are Gemini, Google's family of multimodal models that can work with text, images, audio, video and code.

This lesson is about using a model from your own application: how a call looks, what a prompt contains, how grounding reduces made-up answers, and how tokens turn into cost. The real service needs a Google Cloud project with billing, so the real code below is read-only. The stand-ins that follow run in your browser and show the arithmetic and the shape of a request.

What Vertex AI offers

Vertex AI in four parts

Gemini models Generate and understand text, images and more.
Model Garden Browse Google and partner models.
Training and tuning Adapt models or train your own.
Endpoints Serve models for your apps.

Model names and availability change often. Check the current list.

1 / 2

Model names, versions and which regions support them change frequently, so treat any model name in an example as a placeholder and look up the current list when you build something.

Calling Gemini from code

There are two main entry points. Google's Gen AI SDK can talk either to the Gemini Developer API (with an API key) or to Vertex AI (with Google Cloud credentials). For a production application on Google Cloud you normally want the Vertex AI route, because it uses IAM and service accounts instead of a long-lived key.

A minimal Python example (read-only; install the SDK first, and treat the model name as an example):

Python
from google import genai

client = genai.Client(
    vertexai=True,
    project="my-project-id",
    location="us-central1",
)

response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="Explain a Cloud Storage bucket in two sentences.",
)

print(response.text)
print(response.usage_metadata)

No key appears in the code. On your laptop you would run gcloud auth application-default login; on Cloud Run the service's attached service account supplies the credentials. That service account needs a role that allows calling the model, such as the Vertex AI User role (roles/aiplatform.user).

The same call can also be made over plain HTTPS with a REST request to a generateContent method. The response contains the candidate answers and a block of usage metadata, including the token counts you are billed for.

Anatomy of a prompt

A good request is more than a question. Typical parts:

  • System instruction: stable rules, such as role, tone and format ("You are a support assistant. Answer in three bullet points.").
  • Contents: the user's message, plus any examples or documents.
  • Generation settings: for example temperature (higher values give more varied output), a maximum output length, and a requested output format such as JSON.
  • Safety settings: thresholds that block categories of harmful content.

Examples inside the prompt (called few-shot prompting) often improve consistency more than long instructions. Asking for a structured format, such as JSON that matches a schema, makes the answer easy to parse in code. Always validate the output in your program; a model can still return something unexpected.

Grounding: answers tied to real data

A model only knows what it learned in training, and it can state wrong things with confidence. Grounding connects the model to a source of truth at request time so that answers can be based on, and checked against, that source. Common approaches on Google Cloud are:

  • Grounding with Google Search for fresh public information,
  • Grounding with your own data, using a search index or a vector store over your documents (often called retrieval-augmented generation, or RAG),
  • Function calling, where the model asks your code to run a function (for example lookup_order) and uses the result in its answer.

Grounding does not make errors impossible. Show sources to users where it matters, and keep a human in the loop for important decisions.

Tokens, cost and quotas

Models do not read characters or words; they read tokens, small pieces of text. As a rough rule for English, one token is a few characters, so 1,000 tokens is a few hundred words. Pricing is usually per million tokens, with a different price for input and output tokens, and output is typically more expensive. Images, audio and video are counted in their own units. Some features, such as caching of repeated context, change the price.

Your bill for one call is roughly:

cost = input_tokens ร— input_price + output_tokens ร— output_price

Quotas limit how many requests or tokens you may use per minute, per project and region. If you exceed them you get a "resource exhausted" error (HTTP 429). Handle that by retrying with exponential backoff, and request a quota increase if you truly need more. The prices in the stand-in below are invented for teaching and are not Google's; always read the current pricing page.

Stand-in: a token and cost estimator

This stand-in estimates tokens with a crude rule (about four characters per token). Real tokenizers are different and give different counts, so for exact numbers use the API's token-counting method or the usage metadata in the response.

JS Stand-in: estimate tokens and cost

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

No output.

Small numbers per call multiply quickly at scale. That is why teams choose the smallest model that does the job, shorten prompts, and cache repeated context.

Stand-in: a canned model

To practice the shape of a call without a real model, this stand-in fakes one. It looks up a fixed answer, returns it in a structure similar to a response, and shows a retry on a simulated quota error. It does not understand language at all.

JS Stand-in: a canned model with retries

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

No output.

Notice that the retry waits longer after each failure. That pattern, exponential backoff, is the standard way to deal with quota errors, and the real SDKs and the API's documentation recommend it.

Staying safe and in budget

  • Prefer attached service accounts to API keys, and never commit a key.
  • Set a budget alert before you experiment, and see cost management for more controls.
  • Do not send data to a model that you are not allowed to share; check your organization's data policies.
  • Log request IDs and token usage so you can find what drives cost.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does grounding do?

    Grounding supplies trusted data at request time so answers rely on it instead of only on training data.
  2. 2. Why is the Vertex AI route usually better than an API key in a production app on Google Cloud?

    An attached service account gives short-lived credentials and least-privilege control, with nothing to leak.
  3. 3. How is the cost of one model call roughly calculated?

    Pricing is commonly per million tokens, with separate prices for input and output.
  4. 4. A call fails with HTTP 429. What is a good first response?

    429 means a quota or rate limit was hit. Backing off gives the limit time to recover.

Summary

  • Vertex AI is Google Cloud's platform for generative AI and ML; Gemini models are reached through the Gen AI SDK or REST.
  • Use service account credentials instead of API keys in production, and give only the roles needed.
  • A prompt combines system instructions, content, examples and settings; always validate the output.
  • Grounding with search, your own data or function calling reduces unsupported answers but does not remove them.
  • Cost depends on input and output tokens, so control model size, prompt length and quotas, with retries and backoff.

Next: Pre-trained AI APIs and Machine Learning Workflows