Model Management and Inference

Model Management and Inference

Token Budget Management — Stack

Token budget management › Stack

33 of 49 in this book · 2 min read

Technology landscape as of 2026-10-10. Knowledge cutoff May 2026.

Selection criteria

Two separate problems hide here: counting tokens correctly, and enforcing a limit across distributed callers. Evaluate the pieces separately.

  • Tokenizer fidelity for the target model. Does the counter use the exact encoding the target model uses, and is it updated when a new model family appears? An encoder that is close is not adequate at the boundary.
  • Counting what is actually billed. Whether the mechanism counts images, tool definitions, system content, cached input, and reasoning output — all billable, all commonly omitted.
  • Counting cost. Running a full tokenizer over a large assembled context on every request has measurable CPU cost at volume; check throughput, not just correctness.
  • Distributed enforcement. For shared ceilings: atomic increments, bounded clock skew, and a defined behaviour when the counter store is unreachable.
  • Refund semantics. Whether a reservation taken before a call can be reconciled against actual usage afterwards, including on failure and client disconnect.
  • Attribution granularity. Whether usage can be attributed to tenant, feature, and request id at write time rather than reconstructed from logs later.

Options

Option Best for Watch out for
tiktoken Exact counting for OpenAI model encodings, in Python and via bindings Only covers encodings it ships; using it as a universal estimator is the common misuse
Hugging Face tokenizers with the model's own tokenizer.json Exact counting for open-weight families, and for any model that publishes its tokenizer Must track the model's revision; a changed tokenizer file changes every count
Provider token-counting endpoints Authoritative counts including images and tool definitions, for models with no public tokenizer A network call per count; unusable in a tight assembly loop without caching
Usage figures returned on the response Ground truth for billing and reconciliation Arrives after the spend has happened; useless for admission control on this request
Redis or similar with atomic counters and sliding windows Shared per-tenant ceilings across many processes Needs a documented fail-open or fail-closed stance when unreachable, and that choice has a price tag
Gateway-enforced spend and rate limits (LLM gateways, API gateways) One enforcement point for every service, no client cooperation required Enforces on the next request, not the one in flight; a single large request can still overshoot
Cost-observability platforms (Langfuse, Helicone and similar) Attribution, dashboards, and alerting on spend per tenant and feature Observability is not enforcement; a dashboard has never stopped a runaway loop

Context-assembly libraries that offer automatic history trimming belong in this evaluation too, but check which strategy they implement — dropping oldest turns and summarising them are very different products with the same configuration flag name.