Home / Guides / Vertex AI Matching Engine RAG tutorial: End‑to‑End Real‑Time Retrieval Pipeline
Vertex AI Matching Engine RAG tutorial: End‑to‑End Real‑Time Retrieval Pipeline
To build a real‑time RAG pipeline on Vertex AI Matching Engine, you create a vector index from document embeddings, deploy it to a private endpoint, configure a retrieval service, and connect an LLM (e.g., Gemini) that calls the endpoint for context before generation. The guide walks through each step, from data ingestion to scaling and troubleshooting.

What is Vertex AI Matching Engine and why use it for RAG?
Vertex AI Matching Engine is Google’s managed vector search service built on the ScaNN algorithm. It offers:
- Enterprise‑grade scalability – handles billions of vectors with millisecond‑level latency.
- Global availability – distributed across Google’s data centers for low‑latency access.
- Managed infrastructure – you only manage the index and endpoint, not the underlying VMs.
These properties make Matching Engine ideal for the *retrieval* part of a Retrieval‑Augmented Generation (RAG) system, where fast, accurate similarity search over a large knowledge base is critical. By pairing it with Vertex AI’s LLMs (Gemini, Llama, Claude, etc.) you get a fully managed, production‑ready RAG pipeline.
For a deeper look at the service architecture, see the official overview Vertex AI Matching Engine overview.
How do I prepare my data and generate embeddings?
- Collect documents – PDFs, Markdown files, or tables stored in Cloud Storage, BigQuery, or Drive.
- Chunk the content – split each document into 500‑‑1000 token segments with a small overlap (e.g., 100 tokens) to preserve context. The codelab recommends this size for balanced retrieval quality and index size.
- Choose an embedding model – Vertex AI Model Garden offers text‑embedding models such as
textembedding-gecko‑001. Deploy the model or call it via the Vertex AI API. - Generate embeddings – for each chunk, call the embedding endpoint and store the resulting 768‑dimensional vector together with metadata (document ID, chunk offset, source URI).
- Save to CSV/JSONL – Matching Engine’s bulk import expects a flat file where each line contains the vector and optional metadata fields.
The codelab’s “Environment Setup” section walks through enabling the Vertex AI API and installing the Python client, which you’ll also need for embedding generation.
Helpful links:
- Vertex AI Vector Search quickstart (shows how to prepare a CSV of embeddings).
- Vertex AI RAG Engine overview for context on how embeddings feed the retrieval component.

How do I create and deploy a Matching Engine index?
- Create an index definition – write a JSON metadata file describing the dimension (e.g., 768), distance measure (
DOT_PRODUCTorEUCLIDEAN), and any sharding strategy. See the *Configure an index* docs for the required fields. - Import vectors – use the
gcloud ai indexes createcommand with the--metadata-fileflag pointing to your JSON definition, and the--input-fileflag for the CSV of vectors. - Deploy an index endpoint – run
gcloud ai indexes endpoints create --region=us-central1to provision a private endpoint. - Bind the index to the endpoint –
gcloud ai indexes endpoints deploy-indexattaches your newly created index to the endpoint, specifying the machine type (e.g.,e2-standard-4). Matching Engine automatically provisions the ScaNN service. - Test the endpoint – send a query vector via the Python client’s
find_neighborsmethod. You should receive the top‑k closest chunks with their metadata.
The documentation stresses that deployments are regional; pick a region close to your users to minimise latency. For production workloads, consider a multi‑regional endpoint and enable traffic split for A/B testing of index versions.
Reference steps are in the Vertex AI docs under *Create an index* and *Create an index endpoint*.
Internal links:
- /how-to/openai-function-calling-langchain-guide/,
- /guides/claude-3-5-sonnet-zapier-tutorial/.
How do I connect a LLM for real‑time retrieval augmentation?
- Select a generation model – Gemini 1.5 Flash, Llama‑2‑70B, or Claude 3 are all available in Vertex AI Model Garden.
- Create a Retrieval Tool definition – in Vertex AI you can configure a *retrieval‑augmented generation tool* that references your Matching Engine endpoint, sets the number of retrieved documents (e.g.,
k=5), and optionally adds a ranker model. - Add the tool to the LLM request – when you call the Gemini API, include the tool in the
toolsfield. The model will first invoke the retrieval endpoint, receive the relevant chunks, and then generate its answer using that context. - Deploy as a Vertex AI Endpoint – package the LLM + retrieval tool logic into a Cloud Run container or Vertex AI Service. The codelab shows how to expose a REST endpoint that forwards user queries to Gemini with the Retrieval tool attached.
- Enable streaming responses – for low‑latency UX, enable the
streamflag on the Gemini request; the model will emit tokens as soon as they are generated, while the retrieval call happens synchronously before generation.
The managed Vertex AI RAG Engine simplifies many of these steps, but the DIY approach gives you full control over chunking, ranking, and custom post‑processing.
External resources:
- Vertex AI RAG Engine blog post explains the tool integration.
- The codelab’s *How the application works* section demonstrates a full end‑to‑end flow from upload to query.
Internal links:
- /ai-news/gpt-6-protein-design-lab-tests/,
- /how-to/azure-openai-vision-power-apps-integration/.
What to do if it does not work?
If you encounter errors during index creation, deployment, or query execution, follow these official troubleshooting steps:
- Validate the metadata file – ensure the JSON matches the schema described in *Configure an index*. Common mistakes are missing
dimensionsor an unsupporteddistanceMeasure. - Check quota limits – visit the *Vector Search quotas and limits* page; exceeding the default number of indexes or endpoint CPUs will cause
RESOURCE_EXHAUSTEDerrors. - Inspect logs – Cloud Logging captures both the import job and the endpoint serving logs. Look for messages about malformed rows or network timeouts.
- Test with a small sample – create a minimal CSV (10 vectors) and a tiny index to verify the pipeline works before scaling.
- Re‑create the endpoint – sometimes a stale endpoint retains an older index version; delete and redeploy the endpoint using
gcloud ai indexes endpoints deletefollowed bycreate. - Contact support – if the error persists, use the *Get support* link in the Vertex AI docs to open a ticket with Google Cloud support.
All steps are drawn directly from the official Vertex AI documentation sections *Manage indexes and endpoints* and *Vector Search quotas and limit*.
Internal links:
- /how-to/gemini-1-5-flash-vertexai-batch/,
- /guides/automate-ticket-triage-gpt4o/.
Menus and names can change as products update. If a step looks different, check the product's own help page.
Frequently asked questions
Do I need to host my own vector database when using Matching Engine?
No. Matching Engine is a fully managed vector store; you only upload vectors and Google handles storage, scaling, and nearest‑neighbor search.
Can I update the index after deployment?
Yes. You can add or delete vectors via the *Update index* API, then re‑deploy the updated index to the same endpoint without downtime.
What latency can I expect for real‑time queries?
Vertex AI Matching Engine is designed for sub‑100 ms latency at scale, comparable to Google Search’s internal latency figures.
Is it possible to combine keyword and vector search?
Matching Engine supports hybrid search, letting you blend traditional keyword filters with vector similarity for richer retrieval.