Serverless AI Inference, Model Gateway, Vector Database, and RAG
How Cloudflare Workers AI, AI Gateway, Vectorize, and related services fit together to run models, control cost, and build RAG apps.
This page summarizes Cloudflare documentation on running machine learning models on its global network: hosted inference with Workers AI, outbound model proxying and controls with AI Gateway, embeddings and similarity search with Vectorize, and common patterns such as Retrieval Augmented Generation (RAG). If you need inference without managing GPUs, a single place to call OpenAI or Anthropic with caching, or edge-hosted RAG, this is the map those docs draw.
Workers AI: what serverless GPU inference is for
Section titled “Workers AI: what serverless GPU inference is for”Workers AI runs machine learning models on Cloudflare’s network using serverless GPUs. You call models from Workers, Pages, or the REST API without scaling or maintaining inference hardware yourself.
Run machine learning models, powered by serverless GPUs, on Cloudflare’s global network.
— Cloudflare, Cloudflare Workers AI
The service is positioned as pay-for-what-you-use, with a curated catalog of open-source models for tasks such as text generation, embeddings, image generation, and classification—see the Models section and browse models in the same doc set. Workers AI is available on Free and Paid plans; custom models or higher limits go through Cloudflare’s custom requirements flow linked from Workers AI.
When to choose Workers AI instead of calling OpenAI directly
Section titled “When to choose Workers AI instead of calling OpenAI directly”Use Workers AI when you want inference colocated with Workers at the edge, predictable serverless billing, and models from Cloudflare’s catalog. The build and run AI applications guide states that Workers hosts application logic, Workers AI runs inference at the edge, and AI Gateway is the layer for managing cost and observability across external providers—so hosted and external models can coexist in one architecture.
Real-time AI features document a typical edge stack: Workers handles requests near the user, a Workers AI binding runs inference without round-trips to your origin, Workers KV can cache frequent responses, and D1 can hold session or conversation state.
AI Gateway: observe and control calls to external model providers
Section titled “AI Gateway: observe and control calls to external model providers”AI Gateway sits in front of third-party and first-party model APIs. Cloudflare AI describes it as the path to observe and control AI applications with caching, rate limiting, retries, model fallback, and more—while Workers AI stays the option for models hosted on Cloudflare.
AI Gateway proxies requests to OpenAI, Anthropic, and other providers with caching and unified analytics.
— Cloudflare, AI applications
Which providers AI Gateway supports
Section titled “Which providers AI Gateway supports”The Provider Native page lists supported backends, including Workers AI, Amazon Bedrock, OpenAI, Anthropic, Groq, Mistral AI, Google Vertex AI, and others on that list. Individual provider pages (for example OpenAI or Baseten) mostly point to the vendor; the shared value is routing everything through one gateway.
Getting started with AI Gateway highlights next steps: caching for speed and cost, rate limiting for scale, and model or provider fallbacks for resilience. Control costs and improve quality links AI Gateway get started, configure caching, and Workers Analytics Engine.
You can combine gateway and hosted inference: Set up Workers AI with AI Gateway walks through Workers, an AI Gateway binding, and an LLM on the Cloudflare network.
Vectorize, AI Search, and storing context for RAG
Section titled “Vectorize, AI Search, and storing context for RAG”Vectorize is Cloudflare’s vector database for embeddings. AI Gateway related products note that Vectorize enables semantic search, recommendations, anomaly detection, or context and memory for an LLM.
Store and retrieve context explains that AI apps need storage for embeddings, conversation history, training data, and cached responses: Vectorize for RAG-style retrieval, D1 for structured data, R2 for documents and assets, and KV for edge caching.
AI Search is described as fully managed RAG pipelines, alongside Workers AI, AI Gateway, Vectorize, Workers, and R2 in its related products section.
How a RAG pipeline is wired on the platform
Section titled “How a RAG pipeline is wired on the platform”The Retrieval Augmented Generation (RAG) reference architecture splits knowledge seeding and knowledge queries:
| Phase | Main steps (per Cloudflare diagram) |
|---|---|
| Seeding | Upload documents → queue batch processing → Workers AI embedding models → vectors in Vectorize → documents in D1 |
| Query | User query → query embeddings via Workers AI → vector search in Vectorize → fetch documents from D1 → text generation with query + context |
Related tutorials include Build a RAG AI (listed on Workers AI tutorials) and Get started with AI Search.
Composable AI architecture: compute, inference, vectors, and data
Section titled “Composable AI architecture: compute, inference, vectors, and data”Cloudflare’s Composable AI architecture diagram frames the platform as composable layers:
Composability refers to the ability to assemble various AI components into tailored solutions… Standard APIs for interoperability serve as the linchpin for integrating diverse AI components.
— Cloudflare, Composable AI architecture
In that model, compute (Workers with fetch() / connect()), inference (Workers AI or external APIs, often via bindings), vector search (Vectorize), and data & storage (D1, R2, and others) are swappable. You can build entirely on Cloudflare or connect one layer to external systems—see Ingesting BigQuery Data into Workers AI for pulling Google BigQuery rows into a Worker and running Workers AI models on the result.
Broader product touchpoints from Related products include Agents, AI Crawl Control, Browser Rendering, Sandboxes for untrusted generated code, and Dynamic Workers—each is a separate doc trail; the build and run page is the usual starting point for a full application.
Getting started: Workers AI bindings, REST API, and dashboard
Section titled “Getting started: Workers AI bindings, REST API, and dashboard”Getting started with Workers AI offers three entry paths: Workers Bindings, REST API, and Dashboard. New projects can follow REST API to experiment with an LLM over HTTP, or the Dashboard flow (Workers & Pages → Create application → LLM Chat App template → deploy to a workers.dev subdomain).
Workers AI configuration covers bindings, OpenAI-compatible endpoints, Pi AI, Vercel AI SDK, and Hugging Face Chat UI integrations. The Developer Docs homepage shows CLI discovery: npx wrangler ai models and cf ai run @cf/meta/llama-3.1-8b-instruct --help.
For function-calling apps, see embedded function calling get started (Workers, Workers AI binding, ai-utils, and an LLM).
Data usage, retention, and common mistakes
Section titled “Data usage, retention, and common mistakes”Workers AI data usage defines Customer Content (prompts, outputs, embeddings, training data) and states commitments for standard inference:
Zero data retention: Workers AI processes Customer Content only for as long as needed to provide the service… Cloudflare does not retain inputs or outputs after they are no longer needed to process the request and deliver the response, subject to the limited exceptions below.
— Cloudflare, Data usage
No training: Cloudflare does not use Customer Content processed by Workers AI to train AI models.
— Cloudflare, Data usage
Retention exceptions apply when you store content (R2, KV, Durable Objects, Vectorize), enable AI Gateway logging or caching, or use stateful or batch features—see When a feature stores Customer Content. Sending data through AI Gateway to another provider is governed by that provider’s terms.
Common mistakes
- Assuming free local dev: Workers AI bills during local runs (Pages configuration).
- Enabling gateway cache or logs while expecting zero retention of prompts (data usage).
- Skipping AI Gateway for multi-vendor apps and losing unified caching, rate limits, and fallbacks (AI applications).
- Mixing up hosted vs external models: catalog inference is Workers AI; OpenAI/Anthropic traffic typically goes through AI Gateway (build and run).
The Workers AI glossary defines terms such as Inference, Function Calling, Context Window, Model Catalog, and Worker Bindings used across the docs.
How to check this on your site
Section titled “How to check this on your site”- In the Cloudflare dashboard, open Workers & Pages and confirm a Worker or Pages project has a Workers AI or AI Gateway binding as described in Workers Bindings and Set up Workers AI with AI Gateway.
- List available models with the CLI shown on Developer Docs (
npx wrangler ai models) and run a trial inference via REST API get started. - If you proxy external providers, open your AI Gateway in the dashboard and verify caching and rate limiting match Getting started and Control costs.
- For RAG, confirm embeddings land in Vectorize and queries follow the seed/query flow in RAG architecture.
- You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact sentence you are asking about, and we will correct or clarify it.