Katkat

Design a Billing-Token Rate Limiter for AI Agents

GoogleSoftware Engineer · Onsite

Last updated

Design a distributed rate limiter and quota enforcement service tailored for AI agent workloads where consumption is measured in variable billing tokens rather than fixed request counts.

Scenario

AI agent platforms execute long-running, multi-step tasks using Large Language Models (LLMs). Unlike traditional web APIs where each HTTP call consumes one unit of quota, LLM calls consume a variable number of input and output "billing tokens" (e.g., 500 prompt tokens + 1,200 generated completion tokens). Because completion length is unknown before inference finishes, traditional request-rate limiters cannot prevent overages or manage customer token budgets effectively.

Design a distributed token-rate limiter and quota management system that enforces per-minute, per-hour, and monthly billing-token limits across multiple AI agents belonging to an organization.

Requirements

Functional Requirements

  • Reserve-and-Commit Workflow: Support reserving an estimated number of tokens prior to LLM inference, followed by committing the actual tokens consumed (or rolling back unused reserved tokens).
  • Hierarchical Scopes: Enforce limits across multiple levels: per-agent, per-user, and organization-wide pools.
  • Configurable Throttling Policies: Support both hard limits (reject request immediately with HTTP 429) and soft limits / queuing (allow prioritized burst or delay execution).
  • Streaming Support: Handle token reconciliation for streaming LLM responses that output tokens over several seconds or minutes.

Non-Functional Requirements

  • Low Latency Overhead: Reservation checks must complete in under 5 ms at p99 to avoid stalling generation pipelines.
  • High Availability & Fault Tolerance: Failure of the rate limiter must not silently drop generation results; define a clear fail-open or fail-closed strategy.
  • Accuracy vs. Throughput: Balance strict consistency against network latency for high-frequency callers.
  • Observability: Emit real-time metrics on quota saturation, rejection counts, and token burn rate for billing pipelines.

Scale Assumptions

  • 100,000 active organizations.
  • 50,000 reservation/commit operations per second at peak.
  • Mean reservation duration (time between reserve and commit): 4 seconds.
  • 90% of requests are non-streaming; 10% are streaming responses that last up to 60 seconds.

What to Cover

  1. API Design: Define endpoints and payloads for reserving, committing, and adjusting token consumption.
  2. Architecture & Data Storage: Detail the data store choice (e.g., Redis clusters, local memory, Cassandra) and the rate-limiting algorithm adapted for token units (e.g., Token Bucket, Sliding Window Counter).
  3. Reserve/Commit Lifecycle: Explain how you prevent token leakage if an agent crashes midway through generation.
  4. Distributed Scaling & Consistency: Explain how quotas are shared across distributed limiter nodes and multiple geographic regions.
  5. Trade-offs: Evaluate strict transactional consistency vs. eventual consistency with token overage allowances.

Hints

Hint 1

Because LLM output sizes are non-deterministic, consider a two-phase reservation pattern where a lease with a TTL holds an estimated maximum quota until inference finishes.

Hint 2

To achieve sub-5 ms latency, evaluate using in-memory distributed data stores like Redis with Lua scripts or local token-bucket batching on API gateways.

Hint 3

Consider what happens when workers crash after reserving tokens: an automatic TTL expiration on reservations is vital to prevent permanent quota leakage.

Follow-up questions

  • How would you handle streaming responses where token usage accumulates gradually over tens of seconds without requiring an HTTP commit on every individual token chunk?
  • If an enterprise customer demands absolute hard limits with zero overage allowance, how must your consistency model and architecture shift compared to customers who tolerate brief burst overages?
  • How would you synchronize token quotas across multiple cloud regions without adding cross-region roundtrip latency to the reservation path?