ForHosting KIT · Developer Utilities

Token bucket rate limit calculator

A bucket of 10 tokens holding 5 and refilling at 2 per second has 8 ready after 1.5 seconds, so a burst of 3 goes through and leaves 5. Enter your own numbers to see allow or deny and the wait. Free in your browser.

  • Free
  • Runs in your browser
  • No sign-up
  • Your data stays on your device
Use it from WebAPIEmailTelegramApp soon
If you enter
Capacity
10
Refill rate
2
Tokens
5
Elapsed
1.5
Request count
3
You get
Tokens available
8
Tokens remaining
5
Tokens consumed
3
Deficit
0

You keep a bucket of tokens up to a fixed capacity, refill at a steady rate over time, and spend tokens when a request or burst arrives. This token bucket rate limit calculator accepts capacity, refill rate in tokens per second, the current token count, how many seconds have elapsed since the last update, and how many tokens the next burst needs.

How the bucket makes its decision

A token bucket keeps a balance of tokens that never goes above a fixed capacity. Time adds tokens at a steady refill rate, and every burst spends as many tokens as it costs. To decide, the calculator first refills: it adds the elapsed seconds times the rate to the current balance and caps the result at the capacity. That is what you can spend right now. If it covers the cost, the burst is allowed and the cost is subtracted. In the example, 5 tokens plus 1.5 seconds at 2 per second make 8, so a burst of 3 passes and 5 remain. You supply the elapsed time yourself, so the same numbers always give the same answer.

What it is useful for, and the usual mistakes

Use it to check a gateway quota or a limiter in your own code against a reference, to explain a design in a review, or to prepare load simulations. It is especially good at the edge cases where hand-written limiters go wrong: a bucket that is already full, a partial refill, and a burst larger than the ceiling. A common mistake is forgetting the cap, which lets the balance grow without limit after a long pause and allows bursts far bigger than intended. Another is treating a burst larger than the capacity as one that only needs patience; no amount of waiting can make it fit, and the calculator says so.

Gateway implementation cross-checks

Compare Redis or in-process bucket state machines to this deterministic calculator for the same capacity, refill, tokens, elapsed, and cost inputs.

Can tokens be higher than capacity on input?

Yes. The calculator accepts a non-negative tokens value above capacity and clamps only after applying elapsed refill, matching common implementations that store an unclamped counter until the next update.

How many tokens are available after a pause?

Add the elapsed seconds times the refill rate to the current tokens, then cap the result at the capacity. With 5 tokens, a rate of 2 per second and 1.5 seconds elapsed, you have 8, as long as the capacity is at least that.

How long must I wait when a burst is denied?

The calculator divides the shortfall, the tokens you are missing, by the refill rate. That gives the number of seconds to wait before the bucket holds enough for the burst.

When can a burst never be allowed?

When it costs more tokens than the bucket can ever hold, or when the refill rate is zero and the bucket does not already have enough. In both cases the calculator says so plainly instead of giving a wait time.

What do capacity and refill rate mean in practice?

Capacity is the largest burst the bucket allows at once, its burst ceiling. The refill rate, in tokens per second, is the steady pace you can sustain over time once that burst has been spent.

Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.

POSThttps://api.kit.forhosting.com/dev/token-bucket

Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.

curl -X POST https://api.kit.forhosting.com/dev/token-bucket \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"capacity":10,"refill_rate":2,"tokens":5,"elapsed":1.5,"request_count":3}'
{
  "capacity": 10,
  "refill_rate": 2,
  "tokens": 5,
  "elapsed": 1.5,
  "request_count": 3
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "dev.token_bucket",
  "status": "queued",
  "_links": {
    "result": "/tasks/tsk_…/result"
  }
}

The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

A token-bucket rate limiter is the standard model behind API throttling, gateway quotas, and client-side back-pressure. It refills the bucket with a hard clamp at capacity, decides allow or deny, reports tokens remaining after the decision, and when the burst is denied it computes how long you must wait for enough tokens—or tells you the request can never be satisfied because it exceeds capacity or refill is zero. The same pure deterministic module powers the free browser tool and the API path, so design reviews, load-test scripts, and production simulators never disagree on edge cases such as a full bucket, a partial refill, or a request larger than the burst ceiling.

How the token bucket algorithm decides allow or deny

The token bucket stores a non-negative token balance that never exceeds capacity. Elapsed time is caller-supplied so the math stays deterministic—no wall clock is read—so the same inputs always produce the same allow decision, remaining balance, and wait seconds across runtimes and goldens. Between checks, tokens accumulate at refill_rate tokens per second for the elapsed duration, then the balance is clamped with min(capacity, tokens + elapsed times refill_rate). That clamped value is tokens_available—the amount you can spend right now. A burst that costs request_count tokens is allowed when tokens_available is at least request_count; on allow, tokens_remaining becomes tokens_available minus request_count and tokens_consumed equals request_count. On deny, no tokens are spent: tokens_remaining stays at tokens_available, tokens_consumed is zero, and deficit equals request_count minus tokens_available. Wait time is deficit divided by refill_rate when refill is positive and the request is not larger than capacity; otherwise the request cannot be satisfied later either (can_satisfy is false), which covers both a cost above the burst ceiling and a frozen bucket with zero refill. Capacity is the burst allowance: a full bucket of ten with a slow refill still lets a single ten-token spike through, which is why gateways choose capacity for peak bursts and refill_rate for sustained average traffic.

Input contract, response fields, and validation errors

POST a JSON body with five required finite numbers. capacity must be greater than zero and is the maximum tokens the bucket can hold after refill. refill_rate must be greater than or equal to zero and is measured in tokens per second; zero means the bucket never recovers once empty. tokens is the balance before applying this elapsed interval and must be non-negative; values above capacity are accepted and are clamped when computing tokens_available after refill. elapsed is seconds since the last update and must be non-negative. request_count is how many tokens this burst needs and must be greater than zero. Missing fields, non-numeric strings, NaN, Infinity, and out-of-range signs fail with invalid_input naming the field and are not billed. On success the body echoes the five inputs, then returns tokens_available after refill, allowed as a boolean, tokens_remaining after the spend decision, tokens_consumed, deficit, wait_seconds (zero when allowed or when the request can never pass), can_satisfy, and a short formula string restating the update rule. Example: capacity ten, refill_rate two per second, tokens five, elapsed one and a half seconds, request_count three yields tokens_available eight (five plus three from refill, still under capacity), allowed true, tokens_remaining five, and wait_seconds zero. A second call that asks for nine tokens with the same available eight is denied with deficit one and wait_seconds of one half second at two tokens per second.

Where a token-bucket calculator earns its keep

Edge and gateway authors use the calculator as an oracle while implementing Redis or in-memory buckets, comparing their local state machine to this closed-form result for identical inputs. Educators and SRE interviews use it to demonstrate that token bucket is not the same as a pure leaky bucket: bursts are allowed up to capacity, then smooth refill restores headroom. Failed validation does not consume balance. API product teams size capacity and refill before shipping a quota: they plug candidate numbers and sample burst sizes to see whether legitimate spikes pass while abusive floods wait. Load-test harnesses and chaos scripts call the same pure endpoint to assert that a synthetic client that follows wait_seconds never over-spends relative to the documented limit. Keep requests small, pin the rounded numeric fields in tests, and you get reproducible allow and wait answers for design docs, SDK examples, and production dry-runs without standing up a full rate-limit service.

API quota and burst sizing

Tune capacity and refill_rate against realistic request_count bursts so product limits allow legitimate spikes while still bounding sustained traffic.

Load-test and client back-off oracles

Drive synthetic clients with the returned wait_seconds and assert they never exceed the documented token-bucket contract under concurrent scenarios.

How does the token bucket rate limit calculator work?

It computes tokens_available = min(capacity, tokens + elapsed × refill_rate), allows the burst when that value is at least request_count, subtracts on allow, and otherwise returns deficit and wait_seconds = deficit / refill_rate when refill can eventually cover the cost.

What units should I use for refill_rate and elapsed?

Use the same time unit for both: refill_rate is tokens per second and elapsed is seconds. Scaling both to milliseconds works only if you keep them consistent; the API does not convert units for you.

What if request_count is larger than capacity?

The burst is denied with can_satisfy false and wait_seconds zero. A single request that needs more tokens than the bucket can ever hold will never pass, even with a full refill.

What does the token bucket calculator cost?

The browser tool is free. API requests use prepaid KIT balance at $0.002 per successful call; invalid input is not billed.

Read the full KIT documentation →