Handle API errors and rate limits
Every Kite Connect endpoint enforces rate limits (approximate, check current docs): quote endpoints ~1 req/sec, order placement ~10 req/sec but with a 200 orders/day cap on some plans, historical data ~3 req/sec. Exceeding these gets you NetworkException or TooManyRequests errors, and repeated abuse can get your app throttled for longer periods.
Exception types (kiteconnect.exceptions)
from kiteconnect.exceptions import (
TokenException, # access token expired/invalid -> re-auth (ch 10)
NetworkException, # connectivity issue, rate-limit, or 5xx from broker
OrderException, # order-specific rejection (ch 57)
InputException, # malformed request from your own code — fix the bug
GeneralException, # everything else
)
Wrap every call with retry + backoff, except where retrying is unsafe
import time, logging
def call_with_retry(fn, *args, max_attempts=3, base_delay=1.0, **kwargs):
for attempt in range(1, max_attempts + 1):
try:
return fn(*args, **kwargs)
except NetworkException as e:
if attempt == max_attempts:
raise
delay = base_delay * (2 ** (attempt - 1))
logging.warning(f"NetworkException, retry {attempt}/{max_attempts} in {delay}s: {e}")
time.sleep(delay)
except TokenException:
raise # don't retry — needs re-auth, not a resend
The one call you must NEVER blindly retry
Order placement. If a place_order() call times out, you don't know if the order reached the exchange before the timeout. Blindly retrying can create a duplicate order. Chapter 58 (idempotent order placement) covers the correct pattern — check the order book for a matching already-placed order before resending, using your own client-generated tag/reference.
Practical rate-limit discipline
- Batch quote requests (
kite.quote([...])accepts a list) instead of looping one symbol at a time. - Cache the instrument master (chapter 21) locally — don't refetch it per request, it changes at most once a day.
- Add a small fixed sleep between successive order placements in a loop (e.g. basket order legs, chapter 61) rather than firing them as fast as possible.