Home / Function/ __init__() — langchain Function Reference

__init__() — langchain Function Reference

Architecture documentation for the __init__() function in rate_limiters.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  ccc2f71e_2271_a7c6_855b_5ec453b19836["__init__()"]
  29cf4437_3b06_c93f_9779_9154d2ed9ff9["InMemoryRateLimiter"]
  ccc2f71e_2271_a7c6_855b_5ec453b19836 -->|defined in| 29cf4437_3b06_c93f_9779_9154d2ed9ff9
  style ccc2f71e_2271_a7c6_855b_5ec453b19836 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/rate_limiters.py lines 120–163

    def __init__(
        self,
        *,
        requests_per_second: float = 1,
        check_every_n_seconds: float = 0.1,
        max_bucket_size: float = 1,
    ) -> None:
        """A rate limiter based on a token bucket.

        These tokens have nothing to do with LLM tokens. They are just
        a way to keep track of how many requests can be made at a given time.

        This rate limiter is designed to work in a threaded environment.

        It works by filling up a bucket with tokens at a given rate. Each
        request consumes a given number of tokens. If there are not enough
        tokens in the bucket, the request is blocked until there are enough
        tokens.

        Args:
            requests_per_second: The number of tokens to add per second to the bucket.
                The tokens represent "credit" that can be used to make requests.
            check_every_n_seconds: Check whether the tokens are available
                every this many seconds. Can be a float to represent
                fractions of a second.
            max_bucket_size: The maximum number of tokens that can be in the bucket.
                Must be at least `1`. Used to prevent bursts of requests.
        """
        # Number of requests that we can make per second.
        self.requests_per_second = requests_per_second

        # Number of tokens in the bucket.
        self.available_tokens = 0.0

        self.max_bucket_size = max_bucket_size

        # A lock to ensure that tokens can only be consumed by one thread
        # at a given time.
        self._consume_lock = threading.Lock()

        # The last time we tried to consume tokens.
        self.last: float | None = None

        self.check_every_n_seconds = check_every_n_seconds

Domain

Subdomains

Frequently Asked Questions

What does __init__() do?
__init__() is a function in the langchain codebase, defined in libs/core/langchain_core/rate_limiters.py.
Where is __init__() defined?
__init__() is defined in libs/core/langchain_core/rate_limiters.py at line 120.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free