Skip to content
Visual explainer 3h to learn 5 steps

Gemini models, grounding and agent hosting on Google Cloud

Call a Gemini model on Gemini Enterprise Agent Platform with a service account, ground answers in your data and pick a managed agent runtime within data boundaries.

By Invicly Tech, , in Google Cloud Engineering and Services. About 11 minutes to read.

Topic 29 of 32

In short

Gemini Enterprise Agent Platform (formerly Vertex AI) gives your cloud apps access to Gemini models through Google Cloud IAM, so a service account can call a model without API keys in code. You can ground answers in your own data with RAG Engine or Vector Search, and host an agent on Agent Runtime. Always check where data is processed and stored.

Think of it like this: Grounding is an open-book exam. The model answers from the pages you hand it, not only from memory.

Explained simply

Gemini is Google's family of AI models. On Google Cloud you reach them through Gemini Enterprise Agent Platform, which used to be called Vertex AI. You call a model from your code the same way you call other Google Cloud services, with IAM deciding who is allowed.

In the cloud, your app should not carry a password for the model. Instead, it runs as a service account, and the Google libraries find that identity automatically. This is called Application Default Credentials, or ADC. You give the service account one role that lets it call models, and nothing more.

Models only know what they were trained on, so they may guess about your private data. Grounding fixes that. First you search your own documents, then you give the best passages to the model with the question. Google Cloud offers RAG Engine and Vector Search for that search step.

An agent is a program that uses a model to decide steps and call tools. Agent Runtime is the managed place to host one, so you do not run the servers. You can also host an agent in Cloud Run if you want full control. The Agent Development Kit, or ADK, is for writing the agent.

Press Play on a diagram to walk through it one step at a time.

SequenceA grounded answer
Cloud Run app
RAG Engine or Vector Search
Gemini model
  1. 1Search with the user question
    Cloud Run app to RAG Engine or Vector Search: Search with the user question
  2. 2Return the best matching passages
    RAG Engine or Vector Search to Cloud Run app: Return the best matching passages
  3. 3Question plus passages, using ADC
    Cloud Run app to Gemini model: Question plus passages, using ADC
  4. 4Answer based on the passages
    Gemini model to Cloud Run app: Answer based on the passages
CompareWhere to run an agent
Agent Runtime
  • Managed hosting for agents
  • Less to operate
  • Fits when you want sessions and scaling handled
Cloud Run
  • Your own container
  • Full control of code and network
  • Good for agent plus normal API in one service
GKE
  • Most control
  • You run the cluster
  • Only when you need Kubernetes features
LayersA grounded agent on Google Cloud
  1. 1
    Agent code (ADK)

    Decides steps and calls tools.

  2. 2
    Agent Runtime or Cloud Run

    Hosts the agent.

  3. 3
    Gemini model

    Reasons and writes answers.

  4. 4
    RAG Engine or Vector Search

    Finds your passages.

  5. 5
    Service account and IAM

    Limits what it may call.

In depth

Calling Gemini with ADC

With the Google Gen AI SDK you create a client that points at your Google Cloud project and region. Locally, run gcloud auth application-default login so ADC uses your user. On Cloud Run, ADC uses the service account of the service, so no key file is needed.

Grant the service account a role that allows model calls on the project. The long-standing role for this is Vertex AI User; check the current docs for the name under the new branding. Never create a key file for it. The model id changes over time, so keep it in configuration and read the current ids from the models page.

  • Locally: gcloud auth application-default login
  • On Cloud Run: the attached service account
  • Give one narrow role
  • No key files, no keys in images
  • Keep the model id in config

Grounding with RAG Engine or Vector Search

Retrieval-augmented generation, or RAG, has two parts. Retrieval: turn your documents into embeddings, which are lists of numbers that capture meaning, and store them. At question time, embed the question and find the nearest ones. Generation: send the question and the found passages to the model, and ask it to answer only from them.

RAG Engine manages much of this pipeline for you. Vector Search is the index you use when you want more control over chunking and ranking. Either way, show the sources to users so they can check, and tell the model to say it does not know when the passages do not answer.

FlowBuilding the retrieval side
  1. 1
    Collect documents

    Policies, manuals, FAQs.

  2. 2
    Split into chunks

    Short passages.

  3. 3
    Embed

    Turn each chunk into numbers.

  4. 4
    Index

    Store in RAG Engine or Vector Search.

  5. 5
    Search at question time

    Return the nearest chunks.

Choosing where an agent runs

Agent Runtime fits when you want a managed home for an agent, with deployment and scaling handled for you. Cloud Run fits when the agent shares a container with a normal API, or when you need your own networking and libraries. GKE fits only when you already run Kubernetes for other reasons.

Whatever you choose, treat the agent like any service. Give it its own service account, log what tools it calls, set a budget alert and keep a way to turn it off. Check the current Agent Runtime docs for supported frameworks, regions and pricing before you commit.

  • Agent Runtime: managed hosting
  • Cloud Run: your container, your control
  • GKE: only if Kubernetes already fits
  • Own service account per agent
  • Log tool calls and set a budget alert

Data boundaries

Before sending real data to a model, answer four questions. Where is the request processed, which region did you choose, and does the model have a regional or a global endpoint? How long are prompts and answers kept? Is your data used to train Google models? Which of your own rules, such as a privacy policy or a contract, apply to this data?

Read the data governance and region pages for the model you use, because they differ by model and change over time. Until you have checked, use synthetic or public data. Also remove personal details you do not need from prompts.

CompareQuestions to settle before real data
Where
  • Which region processes the request?
  • Regional or global endpoint?
  • Does your law require a region?
How long
  • Are prompts stored?
  • For how long?
  • Can you opt out?
For what
  • Used for training?
  • Who can see logs?
  • Which contract terms apply?

Learn it in 5 short steps

About 35m each. Track them with a timer in your plan.

  1. 1
    Call Gemini with ADC~35m
  2. 2
    Ground responses with RAG Engine or Vector Search~35m
  3. 3
    Host an agent on Agent Runtime~35m
  4. 4
    Keep data in region and out of training~35m
  5. 5
    Continue with the ADK path~35m

Key points

  • Application Default Credentials (ADC) let code use the attached service account. Give it the minimum role for calling models.
  • Grounding with RAG Engine or Vector Search lets answers cite your documents and reduces made-up facts.
  • Agent Runtime (the managed agent hosting that was Vertex AI Agent Engine) hosts agents so you do not run the servers.
  • Pick a region on purpose and read the data governance page for your models: where prompts are processed, and whether data is used for training.
  • The Agent Development Kit (ADK) is how you write the agent. The Invicly ADK path continues from here.

Key terms

Tap a card to flip it. 0/6 known

Examples

python
from google import genai

# Reads project and region from environment variables; no API key in code.
# GOOGLE_GENAI_USE_VERTEXAI=true
# GOOGLE_CLOUD_PROJECT=<project-id>
# GOOGLE_CLOUD_LOCATION=<region>
client = genai.Client()
reply = client.models.generate_content(
    model="<model-id>",  # pick a current id from the models page
    contents="Summarise our refund policy in two sentences.",
)
print(reply.text)

Needs a billing-enabled project and credentials via ADC. Model calls are paid per use.

Call a model from Cloud Run with no key

You deploy a small service whose service account has permission to call models. The environment variables tell the SDK to use Google Cloud, your project and a region. Expect a text reply. If you see a permission error, check the role on the service account. Cleanup: delete the service and any test resources. Model calls are billed per use.

python
import os
from google import genai

# Set in the service config, not in code:
# GOOGLE_GENAI_USE_VERTEXAI=true
# GOOGLE_CLOUD_PROJECT=<project-id>
# GOOGLE_CLOUD_LOCATION=<region>
client = genai.Client()
model_id = os.environ["MODEL_ID"]  # from the models page
reply = client.models.generate_content(
    model=model_id,
    contents="Say hello in five words.")
print(reply.text)

A grounded prompt

A shop answers questions about returns. The app searches the policy documents, gets two passages, and sends them with the question. The instruction tells the model to answer only from the passages. This keeps the answer tied to the real policy and gives you sources to show.

text
Question: Can I return a sale item?
Passage 1: Sale items may be returned within 14 days.
Passage 2: Refunds go to the original payment method.
Instruction: Answer only from the passages.
  If they do not answer, say you do not know.
  List the passage numbers you used.

Quick reference

  • Agent Platform was Vertex AI; Agent Runtime was Vertex AI Agent Engine
  • ADC: code uses the attached service account; no key files
  • Local login: gcloud auth application-default login
  • Grounding = search your data first, then ask the model
  • RAG Engine is managed; Vector Search gives more control
  • Keep the model id in config; models get retired
  • Check region, retention and training use before real data
  • ADK writes the agent; see the Invicly ADK path

Common mistakes

  • Putting an API key in code or in an image. Use the service account and ADC.
  • Assuming a model id stays available. Models are retired, so make the id a setting you can change.
  • Sending personal data to a model without checking region, retention and your own privacy rules.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1How should a Cloud Run service authenticate to Gemini?

Question 2

2What is the first step of grounding?

Question 3

3Which service hosts agents as a managed runtime?

Question 4

4Why keep the model id in configuration?

Question 5

5Which should you settle before sending customer data to a model?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    List the three environment variables that point the Google Gen AI SDK at Google Cloud, with placeholders.

  2. Exercise 2medium

    Write a short grounded prompt template that makes the model answer only from passages and cite them.

  3. Exercise 3hard

    Choose a runtime and write a data-boundary checklist for an internal HR assistant that uses employee data.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Do I need an API key for Gemini on Google Cloud?
No. On Google Cloud you normally use ADC and IAM. A separate API key is for the Gemini Developer API, which is a different product.
What is the difference between RAG Engine and Vector Search?
RAG Engine manages much of the retrieval pipeline for you. Vector Search is the index you tune yourself when you want more control.
Is Agent Runtime the same as Vertex AI Agent Engine?
Yes, it is the current name for the managed agent hosting that was called Vertex AI Agent Engine.
Where do I learn to build the agent?
Use the Agent Development Kit. The Invicly Google ADK with Python path continues from this topic.

Go deeper

ShareXLinkedInWhatsApp