# Rate limits

Turn on Kalshi's token buckets, so a read past the balance bucket's allowance returns Kalshi's 429 too_many_requests error.

Check that your bot backs off when Kalshi's token buckets run dry instead of retrying at once. With no setting, three balance reads in a row all succeed. [`rate_limits: real`](https://www.rystic.ai/docs/network-profiles.md) turns on Kalshi's token buckets, such as the balance and transfer buckets below.

The balance bucket lets `GET /portfolio/balance` through once a second and banks nothing for a burst. The transfer bucket lets 60 transfers through at once. Here three reads go out with the setting off, then three more with it on:

```bash
export RYSTIC_URL=$(rystic run kalshi-twin -d --port 0 --name ratelimits --scenario empty_book --json | jq -r .data.url)
BAL="$RYSTIC_URL/trade-api/v2/portfolio/balance"
curl -s -w '%{http_code}\n' $(for i in $(seq 3); do echo "-o /dev/null $BAL"; done) | sort | uniq -c | awk '{print $2, $1}'
# -> 200 3
curl -s -X POST "$RYSTIC_URL/_rystic/network" -o /dev/null -w '%{http_code}\n' -d '{"rate_limits": "real"}'
# -> 204
curl -s -X POST "$RYSTIC_URL/_rystic/reset" -o /dev/null -w '%{http_code}\n'
# -> 204
curl -s -w '%{http_code}\n' $(for i in $(seq 3); do echo "-o /dev/null $BAL"; done) | sort | uniq -c | awk '{print $2, $1}'
# -> 200 1
# -> 429 2
curl -s -D - "$BAL" | tr -d '\r' | awk 'NR == 1 {print $2} tolower($0) ~ /^(retry-after|x-ratelimit-)/ {n++} /^\{/ {print} END {print n + 0, "Retry-After or X-RateLimit headers"}'
# -> 429
# -> {"error":{"code":"too_many_requests","message":"too many requests"}}
# -> 0 Retry-After or X-RateLimit headers
curl -s "$RYSTIC_URL/trade-api/v2/account/limits" | jq -c '{usage_tier, read, write}'
# -> {"usage_tier":"basic","read":{"bucket_capacity":10,"refill_rate":10},"write":{"bucket_capacity":20,"refill_rate":10}}
sleep 1
curl -s -o /dev/null -w '%{http_code}\n' "$BAL"
# -> 200
curl -s -o /dev/null -w '%{http_code}\n' "$BAL"
# -> 429
curl -s -X POST "$RYSTIC_URL/_rystic/reset" -o /dev/null -w '%{http_code}\n'
# -> 204
curl -s "$RYSTIC_URL/_rystic/network" | jq -r .rate_limits
# -> real
curl -s -o /dev/null -w '%{http_code}\n' -H 'KALSHI-ACCESS-KEY: one-key' "$BAL"
# -> 200
curl -s -o /dev/null -w '%{http_code}\n' -H 'KALSHI-ACCESS-KEY: another-key' "$BAL"
# -> 429
XFER="$RYSTIC_URL/trade-api/v2/portfolio/intra_exchange_instance_transfer"
curl -s -X POST -w '%{http_code}\n' $(for i in $(seq 61); do echo "-o /dev/null $XFER"; done) | uniq -c | awk '{print $2, $1}'
# -> 400 60
# -> 429 1
ORDERS="$RYSTIC_URL/trade-api/v2/portfolio/events/orders"
curl -s -X DELETE -w '%{http_code}\n' $(for i in $(seq 100); do echo "-o /dev/null $ORDERS/missing"; done) | uniq -c | awk '{print $2, $1}'
# -> 404 100
curl -s -X POST -w '%{http_code}\n' $(for i in $(seq 100); do echo "-o /dev/null $ORDERS/batched"; done) | uniq -c | awk '{print $2, $1}'
# -> 400 100
```

The second and third reads were refused, and the next one shows the body Kalshi documents and no `Retry-After`, so your bot picks its own wait. A second later the bucket held one read again, and the read after it was refused.

The reset refilled the balance bucket while the setting stayed on, and after a read under one key, a read under another key found it spent, since every caller draws on one set of buckets.

The 61 transfers carry no body, so the first 60 return 400 and still spend the transfer bucket, and the 61st returns 429. A hundred single cancels and a hundred batch creates were not refused, since the simulator counts no route Kalshi charges other than 10 tokens.

**Differs from Kalshi:** Kalshi keeps one set of buckets per account, and the simulator shares one set among all callers, even keys of different accounts.

**Differs from Kalshi:** the 429 has none of the `X-RateLimit-*` headers Kalshi's does.

**Differs from Kalshi:** `GET /account/limits` reports `usage_tier` `basic` with `read.bucket_capacity` 10 and `write.bucket_capacity` 20 with the setting on. Kalshi reports the account's own tier, such as Advanced with 900 for each.

---

Source: https://www.rystic.ai/docs/simulators/kalshi/features/rate-limits · Markdown: https://www.rystic.ai/docs/simulators/kalshi/features/rate-limits.md
