Every enterprise deploying LLMs hits the same wall: governance at scale. You start with one OpenAI endpoint. Then someone adds Anthropic. A team fine-tunes Llama on provisioned throughput. Another team spins up a custom model. Before you know it, you have five different API keys scattered across three clouds, no unified logging, no rate limiting, and a CFO asking why the AI bill tripled last month.
Mosaic AI Gateway is Databricks’ answer to this chaos. It’s a centralized proxy layer that sits between your applications and every AI model — whether hosted on Databricks, routed through OpenAI, Anthropic, Bedrock, or any custom provider. One API surface. One governance model. One place to enforce guardrails, track costs, and prove compliance.
In this article, we’ll go beyond the overview and get hands-on: configure multi-model endpoints, set up rate limiting, enable guardrails, wire up cost attribution, and query the unified audit trail — all with real code you can run today.
Why AI Gateway Matters #
Without a gateway, the typical enterprise AI architecture looks like this:
|
|
Every model provider has its own auth, its own rate limits, its own logging format. Your security team can’t audit what prompts went where. Your finance team can’t attribute costs to projects. Your compliance team can’t prove PII isn’t leaking into third-party models.
With AI Gateway, this collapses:
|
|
One API. One token. One governance layer. Every request — whether it’s going to GPT-4o, Claude Sonnet, or a fine-tuned Llama 3.1 — flows through the same control plane.
Prerequisites #
Before you start:
- A Databricks workspace on AWS, Azure, or GCP
- Workspace admin privileges (to create serving endpoints)
- A Databricks personal access token (PAT) or service principal token
- Python 3.8+ with
databricks-sdkandopenaipackages installed - Basic familiarity with REST APIs and the Databricks UI
- A Unity Catalog metastore attached to your workspace (for logging and audit)
Architecture: How AI Gateway Works #
AI Gateway is built into Databricks Model Serving infrastructure. It’s not a separate service you deploy — it’s a configuration layer on top of serving endpoints.
|
|
Key architectural properties:
- Horizontally scalable — the inference server, auth layer, proxy, and rate limiter all scale independently
- Credential isolation — API keys are stored via Databricks Secrets, never exposed to application code
- OpenAI-compatible API — your existing OpenAI SDK code works unchanged, just point it at the gateway
- Unity Catalog native — all logs flow into governed Delta tables
Step 1: Create a Multi-Model Endpoint #
Let’s start by setting up an endpoint that routes to an external model. This is how you bring OpenAI, Anthropic, or any provider under the AI Gateway umbrella.
External Model — Anthropic #
|
|
External Model — OpenAI (Azure) #
|
|
External Model — Amazon Bedrock #
|
|
Custom Provider #
Got a model running on your own infrastructure? No problem:
|
|
Notice the pattern: every provider’s API key is stored in Databricks Secrets — never in code, never in environment variables. The {{secrets/scope/key}} notation is resolved server-side.
custom provider covers everything else with a simple URL + auth configuration.
Step 2: Query via the OpenAI SDK #
Here’s the part that makes AI Gateway seamless: it speaks the OpenAI API. Your existing code works with a two-line change.
|
|
Or query directly from a Databricks notebook using SQL:
|
|
The application doesn’t know or care whether it’s talking to Anthropic, OpenAI, or a self-hosted model. It just calls the gateway endpoint. That’s the point.
Step 3: Traffic Splitting and Fallbacks #
Real production deployments need more than a single model behind an endpoint. AI Gateway supports traffic splitting for A/B testing and fallbacks for reliability.
Traffic Splitting #
Route 70% of traffic to one model and 30% to another — useful for gradual rollouts or comparing model quality:
|
|
Fallbacks #
If the primary model returns a 429 (rate limited) or 5XX error, the gateway automatically tries the next model. Set traffic percentage to 0% for fallback-only models:
|
|
Maximum two fallback models per endpoint. The gateway tries them sequentially — if the first fallback also fails, it tries the second before returning an error.
Step 4: Rate Limiting #
Rate limiting is the simplest and most effective governance control. AI Gateway supports a three-tier structure:
| Level | Scope | Description |
|---|---|---|
| Endpoint | Global | Max QPM/TPM ceiling for all traffic to this endpoint |
| User Default | Per-user | Default limit applied to every user unless overridden |
| Custom | Specific | Limits for specific users, service principals, or groups |
Configure via the Databricks SDK #
|
|
Why this matters #
Without rate limiting, a single runaway script can:
- Burn through your entire OpenAI quota in minutes
- Trigger provider-side throttling that affects all users
- Generate a five-figure invoice from a weekend experiment
With AI Gateway, you set the guardrails once and every request — from notebooks, jobs, apps, or APIs — respects them.
Step 5: AI Guardrails #
Rate limits control volume. Guardrails control content. AI Gateway offers two built-in guardrail types, both in Public Preview.
Safety Filtering #
Powered by Meta Llama Guard 2-8B, safety filtering prevents models from interacting with unsafe content. It applies to both inputs (what users send) and outputs (what models return).
|
|
When safety filtering blocks a request, the gateway returns HTTP 400 — your application gets a clear signal that the content was rejected, not a model hallucination about why it can’t help.
PII Detection #
Three modes: None (disabled), Block (reject requests containing PII), or Mask (redact PII before forwarding to the model).
|
|
Detected PII categories (U.S.): credit card numbers, Social Security numbers, phone numbers, email addresses, names, and addresses.
This is where AI Gateway and Unity Catalog’s security model converge. You’ve already locked down data access with column masking and row-level security. Now you’re locking down the AI layer too — preventing sensitive data from leaking through prompts or model responses.
Step 6: Payload Logging and Usage Tracking #
Every request and response can be logged to inference tables — Delta tables managed by Unity Catalog. This gives you a complete audit trail of what was asked, what was answered, and who asked it.
Enable Inference Table Logging #
|
|
Once enabled, every request generates a row in the inference table with:
- Full request payload (prompt, parameters)
- Full response payload (completion, token counts)
- Caller identity and timestamp
- Latency metrics
Usage Tracking via System Tables #
For aggregate usage analytics without storing full payloads, query the system table:
|
|
Cost Attribution with Usage Context #
The real power for finance teams: usage context lets you tag every request with project, team, or user identifiers for precise cost attribution.
|
|
Then query costs by project:
|
|
No more arguing about which team ran up the bill. Every token is accounted for.
Step 7: Putting It All Together #
Here’s a complete configuration that combines everything — multi-model routing with fallbacks, rate limiting, guardrails, and logging:
|
|
That’s a production-ready AI governance layer — deployed in under 50 lines of Python.
Databricks Foundation Models: Pay-Per-Token #
AI Gateway isn’t just for external providers. Databricks hosts its own Foundation Model APIs — pre-deployed models you can query immediately with no infrastructure to manage.
Available models include:
| Provider | Models |
|---|---|
| Meta | Llama 4 Maverick, Llama 3.3 70B, Llama 3.1 405B/8B |
| Anthropic | Claude Opus 4, Claude Sonnet 4.5, Claude Haiku 4.5 |
| Gemini 2.5 Pro/Flash | |
| OpenAI | GPT-4o, GPT-4o mini |
Two pricing models:
- Pay-per-token: Billed in DBUs per token. Ideal for experimentation and intermittent workloads. No minimum commitment.
- Provisioned throughput: Reserved capacity with guaranteed performance. Recommended for production workloads with predictable traffic.
Query a foundation model the same way as an external endpoint:
|
|
All AI Gateway features — rate limiting, guardrails, logging, cost attribution — apply uniformly to foundation models. Same governance, whether you’re calling Claude via Anthropic’s API or via Databricks’ hosted instance.
When to Use AI Gateway vs. Direct API Calls #
| Use AI Gateway when… | Use direct API calls when… |
|---|---|
| Multiple teams share AI model access | You have a single, isolated use case |
| You need unified cost attribution | Cost tracking per provider is sufficient |
| Compliance requires prompt/response logging | Logging isn’t a regulatory requirement |
| You want provider-agnostic failover | You’re committed to a single provider |
| Security policy mandates PII filtering | Your data pipeline already strips PII |
| You’re already on Databricks | You have no Databricks footprint |
Architecture Pattern: Enterprise AI Governance Stack #
Here’s how AI Gateway fits into the broader Databricks governance story:
|
|
The governance stack is layered:
- Unity Catalog governs data access (who can query what)
- AI Gateway governs model access (who can call which model, how much, with what content)
- Together they create an end-to-end audit trail from data to AI output
Current Limitations #
- AI Guardrails are in Public Preview — safety filtering and PII detection may evolve
- Custom Guardrails (bring your own safety model) are in Private Preview
- Fallbacks are limited to 2 per endpoint and only supported for external models
- PII detection covers U.S. categories only — international PII patterns are not yet supported
- TPM rate limiting is not available for custom provider models
- Request batch size cannot exceed 16 when guardrails are active
- Streaming is not supported with output guardrails for embeddings models
- Configuration updates take 20-40 seconds to propagate; rate limit changes may take up to 60 seconds
Conclusion #
AI Gateway is one of those platform capabilities that seems optional until you need it — and by the time you need it, you’re already in trouble. The runaway cost incident. The compliance audit. The prompt injection that leaked customer data to a third-party model.
The practical value is clear:
- Unified access — one API surface for every model provider, no scattered API keys
- Cost control — rate limiting and usage attribution before the bill arrives, not after
- Compliance — every prompt and response logged to governed Delta tables in Unity Catalog
- Safety — guardrails that filter content at the platform level, not the application level
- Resilience — automatic fallbacks across providers when primary models are unavailable
If you’re running AI workloads on Databricks — or even considering consolidating your model access — AI Gateway should be your first configuration step, not an afterthought. The governance layer you set up today prevents the incident you’ll otherwise investigate tomorrow.
AI Gateway is GA for serving endpoints across AWS, Azure, and GCP. The new AI Gateway experience (with coding agent integration and MCP server governance) is in Beta. Check the Databricks documentation for the latest features and availability.