Per API Key Token Bucket For Inference Request Admission

Last updated: November 29, 2026

Quick Overview

Implement a rate limiter for an LLM inference API where each API key has a token bucket with a capacity and a refill rate, and each request costs a variable amount (for example its estimated tokens). Return whether each request is admitted at a given timestamp.

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

0

0

0 solved


Implement a rate limiter for an LLM inference API where each API key has a token bucket with a capacity and a refill rate, and each request costs a variable amount (for example its estimated tokens). Return whether each request is admitted at a given timestamp.

A serverless inference platform like Fireworks bills and throttles by tokens, not by request count, so a limiter that charges a variable cost is a natural phone screen warm-up. The interviewer checks lazy refill math, per-key state, and whether the candidate notices requests larger than the bucket.

What the Interviewer Expects
  • Refill lazily from elapsed time instead of using a background timer
  • Charge a variable cost per request rather than one unit
  • Cap tokens at capacity after refill
  • Reject requests whose cost exceeds capacity outright
  • State O(1) time per call and O(keys) memory
Key Topics to Cover
Token bucket
Rate limiting
Lazy refill
Multi-tenant APIs
Hash maps
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 many API gateway replicas?
  • The real output token count is only known after generation. How do you reconcile an estimate with the actual cost?
  • How would you evict state for keys that have gone idle?
  • How would you give a burst allowance to enterprise keys without code changes?
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

allow(key, cost, now) returns True and deducts cost if the key's bucket holds at least cost tokens after refilling, otherwise False with nothing deducted. Each bucket starts full. Timestamps are...

High-Level Design

Store (tokens, last_ts) per key in a dict. On every call compute tokens = min(capacity, tokens + (now - last_ts) * rate), then compare with cost. This lazy refill avoids timers and makes each call...


Submit Your Answer
Markdown supported

Related Questions