Build a Token-Denominated Rate Limiter With Post-Completion Settlement

Last updated: November 29, 2026

Quick Overview

Design a per-API-key limiter where the budget is measured in LLM tokens per second, requests reserve an estimated token count up front, and the actual usage is settled once the completion finishes. Return a retry-after value when a request is throttled.

Inception
Coding & Algorithms
Software Engineer
Inception
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Design a per-API-key limiter where the budget is measured in LLM tokens per second, requests reserve an estimated token count up front, and the actual usage is settled once the completion finishes. Return a retry-after value when a request is throttled.

Inception prices and protects its API by tokens, and because output length is unknown until generation ends, admission control has to work from estimates. Backend engineers on the API platform own this logic. The interviewer looks for a correct token bucket, a sane reconciliation policy, and correct retry-after math.

What the Interviewer Expects
  • Implement lazy refill based on elapsed time rather than a background timer
  • Reserve an estimate (prompt tokens plus max_tokens) before admitting
  • Settle the difference after completion, allowing a bounded negative balance
  • Distinguish requests that can never fit from ones that should retry
  • Compute an exact retry-after from the deficit and refill rate
Key Topics to Cover
Token Bucket
Rate Limiting
API Design
Usage Metering
Concurrency
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 this state across many API gateway replicas?
  • Should input and output tokens draw from separate budgets given different prices?
  • How would you protect against a client that always requests a huge max_tokens?
  • How do you keep the limiter correct when clocks differ between hosts?
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

Per key: capacity C tokens, refill r tokens per second. try_reserve(key, estimate, now) returns allowed or a retry-after. settle(key, estimate, actual, now) refunds unused tokens or charges oversh...

High-Level Design

Store (tokens, last_ts) per key and refill lazily on every touch: tokens = min(C, tokens + (now - last) * r). Reservation subtracts the estimate. Settlement adds estimate - actual, which can pus...


Submit Your Answer
Markdown supported

Related Questions