Lesson 18 of 30
AI API calls are billed per token, in both directions — so cost scales with how much context you send AND how long the response runs, which is a different problem than the request-count throttling most rate limiting solves.
12 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 3
A chat feature enforces a request-count rate limit (e.g. 20 requests per minute per user) but resends the entire conversation history as context on every single turn. Is cost bounded?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
This track's Frontend Practices lesson on rate limiting already covers the generic mechanism: capping how many requests a client can make in a window. That mechanism still applies to AI features, but it's not the whole story — AI calls have a cost dimension a typical API call doesn't. Most APIs cost roughly the same per request regardless of payload size. An AI API bills per token, counting both the input you send (system prompt, conversation history, retrieved context) and the output the model generates — so cost scales with two things a request-count limit alone doesn't control: how much context goes in, and how long the response is allowed to run.
That distinction matters because a single feature can blow up cost without ever exceeding a request-count rate limit. The most common way this happens: an unbounded conversation, where every new turn re-sends the entire prior history as context. Turn one sends a small prompt; turn fifty sends fifty turns' worth of accumulated history, every single time, at every single turn. The request count didn't change; the cost per request quietly grew the whole time.
Three real controls address this directly:
Turn 1: send [system, msg1] -> ~200 input tokens
Turn 10: send [system, msg1..msg10] -> ~2,000 input tokens
Turn 50: send [system, msg1..msg50] -> ~10,000 input tokens
Nothing here looks like abuse. It's one engaged user having one long, legitimate conversation — and the cost per turn grew roughly linearly the entire time, purely because every turn resent everything before it. A per-request rate limit wouldn't catch this at all, since the number of requests never changed; only fixing the shape of what each request sends (truncation, summarization, or a sliding window of recent turns) actually bounds it.
"How would you stop an AI feature's costs from running away in production?" tests whether a candidate reaches specifically for token-aware controls (output caps, history truncation, per-user quotas) rather than just restating generic rate limiting, which alone doesn't bound per-token cost.