Sub topic 13
Protecting endpoints with AI Gateway
This chapter assumes you can already deploy an agent or call a served model, as covered in the earlier chapters on serving endpoints and deployment. It explains how AI Gateway adds controls to those endpoints before real users reach them.
What AI Gateway does
AI Gateway is a set of controls attached to a model serving endpoint. Every request passes through it on the way to the model, and every response passes through it on the way back. You configure it per endpoint, so your application code does not change.
It covers four areas:
- Rate limits cap how much traffic an endpoint accepts.
- Guardrails filter unsafe content and sensitive data.
- Usage tracking records who called what, and how much.
- Payload logging saves requests and responses to a Unity Catalog table for later analysis.
Which options are available depends on the endpoint type. Check the current Databricks documentation for your workspace before you plan around a specific feature.
Rate limits
A rate limit protects your budget and keeps one caller from starving the others. You can limit by queries per minute or, for language models, by tokens per minute. Limits can apply at several scopes:
| Scope | What it limits | Typical use |
|---|---|---|
| Endpoint | Total traffic to the endpoint | Protect a shared model from overload |
| User | Traffic from each individual user | Stop one person from using the whole quota |
| User group | Traffic from a named group | Give a team a fixed share |
| Service principal | Traffic from an application identity | Limit a batch job or another service |
A request over the limit is rejected, so the calling code needs retry logic with backoff. Test this path before launch. An agent that loops over tools can hit a tokens-per-minute limit faster than you expect.
Guardrails
Guardrails inspect the content of requests and responses. Two common kinds are:
- Safety filtering, which blocks harmful content in prompts or outputs.
- Sensitive data handling, which detects personal information such as email addresses or card numbers. Depending on the setting, it blocks the request or masks the value.
Guardrails reduce risk, but they are not a complete defense. They can miss content, and they can block legitimate requests. Keep your own checks in the application for rules that matter to your business, and review blocked requests in the logs to tune your setup.
Usage tracking and payload logging
Usage tracking records request counts and token counts for each endpoint and caller. These records land in Databricks system tables, so you can query them with SQL and attribute cost to teams or applications. Payload logging stores the full request and response in a Unity Catalog table. That table is useful for debugging and for building evaluation datasets from real traffic. It also stores user text, so treat it as sensitive data. Apply the same Unity Catalog permissions you would to any table that holds customer content.
Common errors
- Setting one endpoint-wide limit only. One heavy user can then consume the whole quota. Add a per-user limit.
- Skipping retry handling. Clients see rejected requests as failures instead of waiting briefly and trying again.
- Assuming guardrails replace review. Sample the logged payloads regularly.
- Leaving payload tables open. Logged prompts can contain personal data. Restrict access.
Exercise
A shared chat endpoint serves a support team and a nightly batch job. Last week the batch job used almost all the available tokens and support users got rejected requests. Which AI Gateway setting addresses this, and what should the batch client do when it is rejected?
Show answer
Add rate limits scoped to the batch job's service principal (and a per-user limit for the support team) so the batch job cannot use the whole endpoint quota. The batch client should catch the rejection and retry with exponential backoff instead of failing.