Token-Based Rate Limiter for an LLM Inference API

Last updated: November 29, 2026

Quick Overview

Implement a per-API-key rate limiter that enforces a tokens-per-minute budget, where each request's cost is its estimated prompt plus max output tokens, and the charge is corrected once the real usage is known.

Together AI
Coding & Algorithms
Software Engineer
Together AI
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Implement a per-API-key rate limiter that enforces a tokens-per-minute budget, where each request's cost is its estimated prompt plus max output tokens, and the charge is corrected once the real usage is known.

Together AI's serverless inference API bills and throttles by tokens, not requests, so a short phone-screen coding problem around a token bucket fits the platform team well. The interviewer checks that you pick a lazy-refill design, handle the post-hoc correction when actual output is shorter or longer than estimated, and keep the per-key state O(1).

What the Interviewer Expects
  • Chooses a token bucket with lazy refill instead of a background timer
  • Charges an up-front estimate and reconciles after generation finishes
  • Rejects requests larger than the bucket capacity outright
  • Keeps per-key state constant size and operations O(1)
  • Discusses how this works across many gateway replicas
Key Topics to Cover
token bucket
rate limiting
lazy refill
usage reconciliation
multi-tenant API
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you share limits across 50 gateway instances without a central lock on every request?
  • Should a streaming request that runs over its estimate be cut off mid-stream?
  • How would you support separate limits for requests per minute and tokens per minute together?
  • What happens to fairness if one key sends a few huge 128K-context prompts?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Each API key has a tokens-per-minute (TPM) limit. A request's true cost is prompt tokens plus generated tokens, but generated tokens are unknown at admission time. So we admit on an estimate (prompt t...

High-Level Design

Use a token bucket per key: capacity equals the per-minute limit, refill rate is limit / 60 tokens per second. Store only (available, last_timestamp) per key and refill lazily on each call: `avail...


Submit Your Answer
Markdown supported

Related Questions