Skip to main content

openlimit-lite

Forked from https://github.com/shobrook/openlimit. Maintaining for our own use, stripping down to remove Redis dependencies.

A simple tool for maximizing usage of the OpenAI API without hitting the rate limit.

  • Handles both request and token limits
  • Precisely (to the millisecond) enforces rate limits with one line of code
  • Handles synchronous and asynchronous requests

Implements the generic cell rate algorithm, a variant of the leaky bucket pattern.

Usage

Define a rate limit

First, define your rate limits for the OpenAI model you're using. For example:

from openlimit_lite import ChatRateLimiter

rate_limiter = ChatRateLimiter(request_limit=200, token_limit=40000)

This sets a rate limit for a chat completion model (e.g. gpt-4, gpt-3.5-turbo). openlimit offers different rate limiter objects for different OpenAI models, all with the same parameters: request_limit and token_limit. Both limits are measured per-minute and may vary depending on the user.

Rate limiter Supported models
ChatRateLimiter gpt-4, gpt-4-0314, gpt-4-32k, gpt-4-32k-0314, gpt-3.5-turbo, gpt-3.5-turbo-0301
CompletionRateLimiter text-davinci-003, text-davinci-002, text-curie-001, text-babbage-001, text-ada-001
EmbeddingRateLimiter text-embedding-ada-002

Apply the rate limit

To apply the rate limit, add a with statement to your API calls:

chat_params = {
    "model": "gpt-4",
    "messages": [{"role": "user", "content": "Hello!"}]
}

with rate_limiter.limit(**chat_params):
    response = openai.ChatCompletion.create(**chat_params)

Ensure that rate_limiter.limit receives the same parameters as the actual API call. This is important for calculating expected token usage.

Alternatively, you can decorate functions that make API calls, as long as the decorated function receives the same parameters as the API call:

@rate_limiter.is_limited()
def call_openai(**chat_params):
    response = openai.ChatCompletion.create(**chat_params)
    return response

Asynchronous requests

Rate limits can be enforced for asynchronous requests too:

chat_params = {
    "model": "gpt-4",
    "messages": [{"role": "user", "content": "Hello!"}]
}

async with rate_limiter.limit(**chat_params):
    response = await openai.ChatCompletion.acreate(**chat_params)

Token counting

Aside from rate limiting, openlimit also provides methods for counting tokens consumed by requests.

Chat requests

To count the maximum number of tokens that could be consumed by a chat request (e.g. gpt-3.5-turbo, gpt-4), pass the request arguments into the following function:

from openlimit.utilities import num_tokens_consumed_by_chat_request

request_args = {
    "model": "gpt-3.5-turbo",
    "messages": [{"role": "...", "content": "..."}, ...],
    "max_tokens": 15,
    "n": 1
}
num_tokens = num_tokens_consumed_by_chat_requests(**request_args)

Completion requests

Similar to chat requests, to count tokens for completion requests (e.g. text-davinci-003), pass the request arguments into the following function:

from openlimit.utilities import num_tokens_consumed_by_completion_request

request_args = {
    "model": "text-davinci-003",
    "prompt": "...",
    "max_tokens": 15,
    "n": 1
}
num_tokens = num_tokens_consumed_by_completion_request(**request_args)

Embedding requests

For embedding requests (e.g. text-embedding-ada-002), pass the request arguments into the following function:

from openlimit.utilities import num_tokens_consumed_by_embedding_request

request_args = {
    "model": "text-embedding-ada-002",
    "input": "..."
}
num_tokens = num_tokens_consumed_by_embedding_request(**request_args)

Metadata

Release files for openlimit-lite 1.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openlimit-lite 1.3.1
File Size Uploaded Provenance
openlimit_lite-1.3.1.tar.gz 20.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for openlimit-lite 1.3.1
File Interpreter ABI Platform
openlimit_lite-1.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 39.6 kB

Release files / openlimit_lite-1.3.1.tar.gz

Download URL openlimit_lite-1.3.1.tar.gz
Size 20.0 kB
Tags Source
SHA-256 checksum
How to use checksums
0d9f359ceca71d7e9c9e9fca1f3d665eaa3121a463387b8c028758b65d11af0f
BLAKE2b-256 checksum
How to use checksums
097463ea1ecc02612263aa357b685160074f4abc2ddd51dbee285f84b73ba727
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.11.9

Release files / openlimit_lite-1.3.1-py3-none-any.whl

Download URL openlimit_lite-1.3.1-py3-none-any.whl
Size 19.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
abb953c26deaf15ba92b15c362db6bebecf93bcbd1d04cc00318d3d961be61a9
BLAKE2b-256 checksum
How to use checksums
f0b12f7906f05b0b6f7ce143de54d65e4a6810d097833b539c1c9e909e7ad737
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.11.9

Security for openlimit-lite 1.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

1.3.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page