AI guardrails & red teaming / Core / 02.04
Protecting AI APIs
Your model sits behind a door that costs money every time it opens. Keep keys on the server, cap how much any visitor can use, and watch for trouble.
13 min readFree
The front door of your model
The last lesson, Unauthorized access, ended with a promise: next, protecting the API in front of your model. Here it is.
An API is a door that other programs knock on. Your app sends a message to the door, and a model behind it sends back an answer. Everyone who can reach the door can ask for work, and in most AI apps every answer costs money. So the door needs a lock, a counter and a limit on how much one visitor can carry in.
Before anything else, one rule for this whole course: test only systems you own or have written permission to test. Everything here is about defending your own door. The code is made-up and runs on your laptop, with no network and no keys.
By the end you will know why keys stay on the server, how limits stop cost attacks, and what to log. You will also run a small rate limiter.
Key words
- API
- A door one program uses to ask another program for something. Your app asks, the model's service answers.
- API key
- A secret password string that proves a request comes from your account. Whoever holds it can spend your money.
- Server-side
- Code that runs on a computer you control, not on the visitor's phone or browser.
- Rate limit
- A cap on how many requests one visitor may send in a stretch of time.
- Quota
- A cap on the total a visitor may use, like a monthly allowance.
- Token bucket
- A rate-limit method: a bucket fills with tokens at a steady pace, and each request takes one out.
- Denial of wallet
- An attack that tries to run up your bill until the service costs you too much to keep running.
Keep keys on the server
Think of your API key as the key to your shop's till. You would not tape it to the shop window. Yet a common beginner mistake is to put the key inside the web page or the mobile app, so the page can call the model directly.
The reason it fails is simple. Anything sent to a visitor's browser or phone is theirs to read. A browser lets anyone look at the page's code and the network calls. An app can be opened up and inspected. So a key in the page is a key given away.
The fix is a small middleman. Your page talks to your own server. Your server holds the key, adds it to the request and talks to the model service. The visitor never sees the key.
- 1
Page asks your server
The browser sends the user's message to your own address, with no key attached.
- 2
Your server checks the visitor
It confirms who is asking, applies the limits you will meet below, and checks the message size.
- 3
Server calls the model
Only now does the server add the secret key and send the request on.
- 4
Answer travels back
The server passes the answer to the page. The key never leaves the server.
Tip
Keep keys in environment settings or a secret store, never in your code files or a screenshot.
Why limits matter: cost attacks
A normal website costs about the same whether it is busy or quiet. An AI app is different. Each request makes a model do real work, and that work is billed.
OWASP's LLM10:2025 page names this risk Unbounded Consumption. It says the problem occurs when an application allows users to conduct "excessive and uncontrolled inferences". An inference is one run of the model to produce an answer. The page says the results can be denial of service, economic losses, model theft and service degradation.
Its list of common examples includes these, under its own names:
Variable-Length Input Flood
Attackers overload the model with many inputs of different lengths, which can leave the system unresponsive.
Denial of Wallet (DoW)
By starting a high volume of operations, attackers exploit the cost-per-use model of cloud AI services, causing an unsustainable bill for the provider.
Model Extraction via API
Someone uses your API to learn how your model behaves. The Model stealing lesson goes deeper on this.
The same page gives plain scenarios. In one, an attacker sends an unusually large input, causing heavy memory and CPU use that could crash or slow the service. In another, an attacker sends a high volume of requests, so the service becomes unavailable to legitimate users.
Rate limits and quotas
OWASP's mitigation list for LLM10 says to apply rate limiting and user quotas to restrict the number of requests a single source entity can make in a given time period.
The two ideas work together. A rate limit is a speed limit: no more than so many requests per minute. A quota is a total allowance: no more than so much per day or month. The speed limit stops floods. The quota stops a slow, steady drain.
The Wikipedia page on rate limiting says it is used to control the rate of requests sent or received by a network interface controller, and can be used to prevent DoS attacks and limit web scraping. It also says that when a client makes too many requests in a time frame, HTTP servers can answer with status code 429: Too Many Requests.
The token bucket
There are several ways to count requests. The Wikipedia page on rate limiting lists token bucket and leaky bucket among its algorithms, plus window-based counters. We will build the first, because the picture is easy to hold in your head.
The Wikipedia page on the token bucket describes it as based on an analogy of a fixed-capacity bucket into which tokens are added at a fixed rate. When a request arrives, the bucket is inspected to see if it contains enough tokens. If so, tokens are removed and the request passes. If not, the request does not conform, and the bucket is not changed.
Picture a jar of coins on a shelf. A coin drops in every two seconds, but the jar holds only three. Each request spends one coin. A visitor who has been quiet has a full jar and can send a quick burst of three. After that, they must wait for coins to drop in. The Wikipedia page puts it this way: a conforming flow can have an average rate up to the rate at which tokens are added, and a burstiness determined by the depth of the bucket.
Our numbers are made up for the demo: the jar holds 3 coins, and 0.5 of a coin is added per second. The formula below says how the jar is topped up on each visit.
In words: top up the jar for the time that has passed, but never above its size. Then, if at least one coin is there, spend it and say yes. Otherwise say no, and leave the jar alone.
A limiter you can run
Here is the whole thing in plain Python. Each user gets their own bucket. The clock is a fixed fake one: we pass in the second each request arrives, so the run is the same every time. The twelve requests and both users, ana and ben, are made up.
The code also checks input size before it touches the bucket. The limit is 200 characters, which is only a stand-in. Real services count tokens, the small text pieces a model reads, and often bytes too. A message that is too big is turned away without spending a coin. Nothing here calls a model, so it runs free.
CAPACITY = 3 # the bucket holds at most 3 tokens
REFILL = 0.5 # tokens added per second (1 token every 2 seconds)
MAX_CHARS = 200 # biggest message we accept
class Bucket:
def __init__(self, now):
self.tokens = CAPACITY
self.last = now
def allow(self, now):
# add the tokens earned since the last visit, never above CAPACITY
earned = (now - self.last) * REFILL
self.tokens = min(CAPACITY, self.tokens + earned)
self.last = now
if self.tokens >= 1:
self.tokens -= 1
return True
return False
def check(user, text, now, buckets):
if len(text) > MAX_CHARS:
return "too big"
if user not in buckets:
buckets[user] = Bucket(now)
return "allowed" if buckets[user].allow(now) else "limited"
# (second on the fake clock, user, message): all made up
requests = [
(0, "ana", "hi"), (0, "ana", "hi again"), (1, "ana", "hello?"),
(1, "ana", "anyone there?"), (1, "ben", "what is 2+2?"),
(2, "ana", "please answer"), (3, "ben", "thanks"),
(3, "ana", "x" * 500), (4, "ana", "ok, one more"),
(5, "ana", "and another"), (6, "ben", "bye"), (8, "ana", "back again"),
]
buckets = {}
for i, (t, user, text) in enumerate(requests, 1):
result = check(user, text, t, buckets)
print(f"{i:2} t={t} {user}: {result}")Output
1 t=0 ana: allowed 2 t=0 ana: allowed 3 t=1 ana: allowed 4 t=1 ana: limited 5 t=1 ben: allowed 6 t=2 ana: allowed 7 t=3 ben: allowed 8 t=3 ana: too big 9 t=4 ana: allowed 10 t=5 ana: limited 11 t=6 ben: allowed 12 t=8 ana: allowed
Read the output line by line. Ana sends requests 1 and 2 at second 0 and request 3 at second 1. All three pass: her jar started with 3 coins, and half a coin dripped in by second 1. Request 4, also at second 1, finds only half a coin, so it is limited.
Request 6 at second 2 passes, because another half coin arrived. Request 8 is a 500-character message, so the size check stops it with "too big" before it touches her jar. Request 9 at second 4 passes. Request 10 at second 5 is limited again, because only half a coin has dripped in. By request 12 at second 8, enough time has passed, and ana is allowed again.
The final count is 9 allowed, 2 limited and 1 too big. The limited ones are only slowed down, not banned. Ana can come back and be served once her jar refills.
Tip
Real systems often keep each user's bucket in a shared store, so that every server in your app sees the same numbers. This lesson keeps it in one dictionary to stay readable, which only works on a single machine.
Limit the size of what comes in
Rate limits count how often. Size limits count how big. You need both, because one huge request can do the work of many small ones.
OWASP's first mitigation for LLM10 is input validation: implement strict input validation to ensure that inputs do not exceed reasonable size limits. The API4 page makes the same point for any API: define and enforce a maximum size of data on all incoming parameters and payloads, such as maximum length for strings, maximum number of elements in arrays, and maximum upload file size.
For an AI app, that means a ceiling on message length, on how many past messages you send along, and on how long an answer you ask for. The API4 page also says to add server-side validation for the parameter that controls the number of records returned in a response.
The same page lists limits whose absence makes an API vulnerable. These include execution timeouts, maximum upload file size, and a third-party service provider's spending limit. Give every call a timeout, so one slow request cannot hang for ever. Cap uploads. And set a spending limit with your model provider if they offer one.
Quick check
A visitor sends one request that is 50 times longer than normal. Which defence catches it?
Validate every request
Input validation is more than size. Treat every request like a parcel at a post office: check the label, the weight and the shape before it goes any further.
Shape
Is every field there, and is each one the right kind? A name field should hold text, not a list of a thousand items.
Size
Is each field within your limit? Reject early, before any model work or any cost.
Allowed values
If a setting has three valid choices, accept only those three. Never pass an unknown setting straight through to the model service.
Do this on the server. A form in the browser can be skipped by anyone who sends the request another way, so browser checks are only a courtesy for honest users.
A validator cannot tell if a short message is trying to trick the model. That is the job of the Prompt injection defences lesson.
Log, watch and react
Limits slow attackers down. Logs tell you that someone is trying. OWASP's LLM10 list includes comprehensive logging, monitoring and anomaly detection: continuously monitor resource usage and implement logging to detect and respond to unusual patterns of resource consumption.
An anomaly is something that does not fit the usual pattern. A user who normally sends ten messages a day and suddenly sends ten thousand is an anomaly.
- 1
Log each request
Write down who asked, when, how big the message was, and what the answer cost. Leave out secrets and private message text where you can.
- 2
Count per user
Total the requests and the spend per user per hour and per day.
- 3
Set alerts
Warn a person when one user, or the whole app, goes far above the usual.
- 4
Decide what happens next
Slow the user, ask them to log in again, switch off a key, or contact them. Write the plan down before you need it.
The API4 page agrees about money: configure spending limits for all service providers and API integrations, and when setting spending limits is not possible, billing alerts should be configured instead.
A checklist: the OWASP API Security Top 10
You do not need to invent your own checklist. OWASP publishes a list of the ten biggest API risks. These are the names in the 2023 edition.
API1 to API3
Broken Object Level Authorization, Broken Authentication, and Broken Object Property Level Authorization. These are about who may see what. The Unauthorized access lesson covers this ground.
API4 to API6
Unrestricted Resource Consumption, Broken Function Level Authorization, and Unrestricted Access to Sensitive Business Flows. API4 is this lesson's main theme.
API7 to API10
Server Side Request Forgery, Security Misconfiguration, Improper Inventory Management, and Unsafe Consumption of APIs.
The page describes API6 as exposing a business flow, such as buying a ticket or posting a comment, without compensating for how the functionality could harm the business if used excessively in an automated manner. For an AI app, a flow like "generate a free trial report" fits that shape.
What to remember
Keep keys on your server, so the visitor never holds your wallet. Put a limiter in front of the model: a rate limit for speed, a quota for the total, and a size limit for each message. Validate every request on the server. Log who asks and what it costs, and watch for sudden jumps. Set spending limits with your provider, and check your app against the OWASP list.
Quick check
In a token bucket, what happens to a request that arrives when the bucket is empty?
Test only what you own, and get written permission before testing anything else.
Next: data poisoning, attacks on what the model learns from.
Sources
Last updated on 16 November 2026
Learn with me
Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.