AI security: attack and defend · Chapter 16 of 21
Rate limits and cost
By the end of this chapter you can say what one conversation with your assistant costs, in rupees, and put a ceiling on it: on how long a reply may be, how many requests one message may cause, how long a message may be, and how many messages one customer may send. You will price real runs, set the three limits, and build a rate limiter per customer. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Defend it
- 3 tasks
- Python
- In the all labs plan
The first 2 chapters of AI security: attack and defend are free. This one comes with the all labs plan.
Why you need this
Every request Mitra sends to its model is paid for, by the token. A normal question costs a few paise. Nobody notices. Then the Diwali sale brings two lakh customers, and somebody discovers that one short message makes Mitra write until the model's limit, and another makes it search the help centre again and again. A third pastes the same complaint three hundred times.
None of these steals data or money. They steal budget, and capacity: while Mitra is busy with them, real customers wait. OWASP calls it LLM10, unbounded consumption. Devika asks the question every founder asks about an AI feature: "What does it cost us per customer, and what is the worst case?" Today nobody at Chillar can answer either half.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
run_cost.py: what one run costs
In run_cost.py, write prices() -> dict: each model's price, read from the API's model list (OpenAI().models.list(); each model's price_inr_per_million has input and output), as {model id: {"input": ..., "output": ...}}. Write rupees(usage, price) -> float: what a run's usage (its .prompt and .completion tokens) costs at that price, in rupees ((prompt × input + completion × output) / 1,000,000).
Under if __name__ == "__main__":, run question N01, then attacks A21 and A23 (each in its own world) on version 0, and for each print, with lab-chat's price:
N01 prompt P completion C ₹R
with R to 4 decimal places (f"{r:.4f}"), two spaces apart.
- Task 2
limits.py: a ceiling on every run
In limits.py, set defences = Defences(...) with three limits: max_tokens (the most tokens one reply may have), max_steps (the most model requests one run may send) and a check_input that blocks a message longer than 2,000 characters (InputCheck(True, reason)). Choose max_tokens and max_steps yourself: the check runs A21, A22 and A23 with your defences (all must fail: A21 is judged on 500 completion tokens, A22 on 6 requests, A23 on 4,000 prompt tokens in one request) and all 16 normal questions (all still answered).
- Task 3 · interview question
Interview: token_bucket.py, a rate limit per customer
"Design a rate limiter." In token_bucket.py, write make_limiter(capacity, per_second, now=time.monotonic): it returns allow(customer) -> bool. Each customer has a bucket of tokens: it starts full (capacity), refills per_second tokens a second (never above capacity), and each allowed message spends one token; with less than one token left, the message is refused (and spends nothing). now() returns the time in seconds.
Under if __name__ == "__main__":, use a fake clock (a variable now returns), make_limiter(3, 0.1, ...), and try, at these seconds: C1001 four times at 0 s, C1002 at 0 s, C1001 at 5 s, twice at 10 s, and at 60 s. Print each as 0s C1001 allowed (or refused), two spaces apart.
What you will learn
- Pricing a run: tokens in, tokens out, and the price list.
- Three meters: one long reply, many requests, one huge message, and a limit for each.
- Choosing limits that stop the attack and not the customer.
- Rate limiting per customer with a token bucket, tested with a fake clock.
More in Defend it
Build "Rate limits and cost" yourself
Chillar, a UPI wallet from Indore, has a support assistant with refund and email tools. You red-team it, then harden it until 23 attacks fail and every real question still gets answered. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of AI security: attack and defend are free. This one comes with the all labs plan.