Throttling

Request rate limits for every Smarter api, per authenticated user and, for anonymous clients, per IP address. The rates are SMARTER_APP_API_THROTTLE_RATE_USER and SMARTER_APP_API_THROTTLE_RATE_ANON.

Request rate limits for Smarter’s apis.

Smarter proxies billable LLM requests, so every api is rate limited, per authenticated user and, for anonymous clients, per IP address. The rates are smarter_settings.app_api_throttle_rate_user and smarter_settings.app_api_throttle_rate_anon, e.g. 600/minute. Either can be set to none to disable it.

  • Django REST Framework views are throttled by REST_FRAMEWORK["DEFAULT_THROTTLE_CLASSES"].

  • The apis that a deployed LLMClient serves are plain Django views, derived from smarter.lib.django.views.SmarterOptionallyAuthenticatedApiView, which calls throttle_wait() in its dispatch().

A client that exceeds a rate receives HTTP 429 Too Many Requests, with a Retry-After header. The counters are kept in the Redis cache, through cache_results.lazy.lazy_cache, so that every pod shares them. While Redis can’t be reached, every throttle lets its requests through, as SmarterCacheFailOpenThrottleMixin explains.

smarter.lib.drf.throttling.CREDENTIALS_THROTTLE_RATE = '10/minute'

the number of sign-ins, sign-ups and password reset.

requests that one IP address may make. It is far below the api’s general rate, because each attempt guesses a password or sends an email.

Type:

The rate of SmarterCredentialsRateThrottle

smarter.lib.drf.throttling.SMARTER_THROTTLE_CLASSES = [<class 'smarter.lib.drf.throttling.SmarterAnonRateThrottle'>, <class 'smarter.lib.drf.throttling.SmarterUserRateThrottle'>]

The throttles of every Smarter api, in the order in which they are checked.

[
    <class 'smarter.lib.drf.throttling.SmarterAnonRateThrottle'>,
    <class 'smarter.lib.drf.throttling.SmarterUserRateThrottle'>,
]
class smarter.lib.drf.throttling.SmarterAnonRateThrottle[source]

Bases: SmarterCacheFailOpenThrottleMixin, AnonRateThrottle

Limit the request rate of each anonymous client, by IP address.

The rate is smarter_settings.app_api_throttle_rate_anon. Authenticated requests are not limited by this throttle, but by SmarterUserRateThrottle.

cache = <cache_results.lazy.LazyCache object>
get_rate()[source]

Return the rate, e.g. 120/minute, or None if the throttle is disabled.

Return type:

str | None

Returns:

smarter_settings.app_api_throttle_rate_anon.

class smarter.lib.drf.throttling.SmarterCacheFailOpenThrottleMixin[source]

Bases: object

Let a request through when the throttle’s counters, in Redis, can’t be read or written.

Django REST Framework’s SimpleRateThrottle.allow_request() reads and writes its cache without catching its errors, so during a Redis outage every throttled view, the web console’s and a deployed LLMClient’s included, would answer 500 (issue #1091). The rate limits, including SmarterCredentialsRateThrottle’s, are instead off for as long as Redis is away, and each request that passes unchecked logs one warning line, as @cache_results computes uncached during an outage (issue #1052).

allow_request(request, view)[source]

Check the request against the throttle, or let it through if the cache fails.

Parameters:
  • request – The request.

  • view – The view that serves the request.

Return type:

bool

Returns:

True if the request may proceed.

class smarter.lib.drf.throttling.SmarterCredentialsRateThrottle[source]

Bases: SmarterCacheFailOpenThrottleMixin, SimpleRateThrottle

Limit the rate of the actions that take a password or send an email, by IP address.

The console’s account api applies it to signing in, signing up and requesting a password reset, on top of the general throttles. Every such action from one address counts against the same limit, CREDENTIALS_THROTTLE_RATE, so an attacker can’t spread password guesses across them.

cache = <cache_results.lazy.LazyCache object>
get_cache_key(request, view)[source]

Return the cache key of the request’s IP address.

Parameters:
  • request – The request.

  • view – The view.

Return type:

str

Returns:

The key under which the address’s attempts are counted.

get_rate()[source]

Return the rate.

Return type:

str | None

Returns:

CREDENTIALS_THROTTLE_RATE.

scope = 'credentials'
class smarter.lib.drf.throttling.SmarterUserRateThrottle[source]

Bases: SmarterCacheFailOpenThrottleMixin, UserRateThrottle

Limit the request rate of each authenticated user.

The rate is smarter_settings.app_api_throttle_rate_user. Anonymous requests are limited by IP address, at the same rate, and by SmarterAnonRateThrottle.

cache = <cache_results.lazy.LazyCache object>
get_rate()[source]

Return the rate, e.g. 600/minute, or None if the throttle is disabled.

Return type:

str | None

Returns:

smarter_settings.app_api_throttle_rate_user.

smarter.lib.drf.throttling.credentials_throttle_wait(request, view)[source]

Check a request that takes a password or sends an email against SmarterCredentialsRateThrottle.

Parameters:
  • request (HttpRequest) – The request.

  • view – The view that serves the request.

Return type:

int | None

Returns:

None if the request may proceed, or the number of seconds to wait before retrying.

smarter.lib.drf.throttling.throttle_wait(request, view)[source]

Check a plain Django view’s request against every Smarter throttle.

Django REST Framework views are throttled by their throttle_classes. This applies the same throttles, and counts against the same limits, for views that are not Django REST Framework views. The request’s user decides which throttle applies: an authenticated user is limited by user, everyone else by IP address.

Parameters:
  • request (HttpRequest) – The request. Its user is the authenticated user, if any.

  • view – The view that serves the request.

Return type:

int | None

Returns:

None if the request may proceed, or the number of seconds to wait before retrying.