
    D_i$                    |    S r SSKJr  SSKrSSKrSSKrSSKr " S S\R                  5      r " S S\5      r	SS/r
g)z;Interface for a rate limiter and an in-memory rate limiter.    )annotationsNc                  x    \ rS rSrSr\R                  SS.S	S jj5       r\R                  SS.S	S jj5       rSr	g)
BaseRateLimiter   aQ  Base class for rate limiters.

Usage of the base limiter is through the acquire and aacquire methods depending
on whether running in a sync or async context.

Implementations are free to add a timeout parameter to their initialize method
to allow users to specify a timeout for acquiring the necessary tokens when
using a blocking call.

Current limitations:

- Rate limiting information is not surfaced in tracing or callbacks. This means
    that the total time it takes to invoke a chat model will encompass both
    the time spent waiting for tokens and the time spent making the request.
Tblockingc                   ga  Attempt to acquire the necessary tokens for the rate limiter.

This method blocks until the required tokens are available if `blocking`
is set to `True`.

If `blocking` is set to `False`, the method will immediately return the result
of the attempt to acquire the tokens.

Args:
    blocking: If `True`, the method will block until the tokens are available.
        If `False`, the method will return immediately with the result of
        the attempt.

Returns:
    `True` if the tokens were successfully acquired, `False` otherwise.
N selfr   s     V/home/james-whalen/.local/lib/python3.13/site-packages/langchain_core/rate_limiters.pyacquireBaseRateLimiter.acquire   s        c                  #    g7fr
   r   r   s     r   aacquireBaseRateLimiter.aacquire/   s     s   r   Nr   boolreturnr   )
__name__
__module____qualname____firstlineno____doc__abcabstractmethodr   r   __static_attributes__r   r   r   r   r      sA      	*.  $ 	15  r   r   c                  j    \ rS rSrSrSSSS.       SS jjrSS jrSS	.SS
 jjrSS	.SS jjrSr	g)InMemoryRateLimiterC   a   An in memory rate limiter based on a token bucket algorithm.

This is an in memory rate limiter, so it cannot rate limit across
different processes.

The rate limiter only allows time-based rate limiting and does not
take into account any information about the input or the output, so it
cannot be used to rate limit based on the size of the request.

It is thread safe and can be used in either a sync or async context.

The in memory rate limiter is based on a token bucket. The bucket is filled
with tokens at a given rate. Each request consumes a token. If there are
not enough tokens in the bucket, the request is blocked until there are
enough tokens.

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.

Current limitations:

- The rate limiter is not designed to work across different processes. It is
    an in-memory rate limiter, but it is thread safe.
- The rate limiter only supports time-based rate limiting. It does not take
    into account the size of the request or any other factors.

Example:
    ```python
    import time

    from langchain_core.rate_limiters import InMemoryRateLimiter

    rate_limiter = InMemoryRateLimiter(
        requests_per_second=0.1,  # <-- Can only make a request once every 10 seconds!!
        check_every_n_seconds=0.1,  # Wake up every 100 ms to check whether allowed to make a request,
        max_bucket_size=10,  # Controls the maximum burst size.
    )

    from langchain_anthropic import ChatAnthropic

    model = ChatAnthropic(
        model_name="claude-sonnet-4-5-20250929", rate_limiter=rate_limiter
    )

    for _ in range(5):
        tic = time.time()
        model.invoke("hello")
        toc = time.time()
        print(toc - tic)
    ```
   g?)requests_per_secondcheck_every_n_secondsmax_bucket_sizec               x    Xl         SU l        X0l        [        R                  " 5       U l        SU l        X l        g)a  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.
g        N)r$   available_tokensr&   	threadingLock_consume_locklastr%   )r   r$   r%   r&   s       r   __init__InMemoryRateLimiter.__init__x   s6    : $7  #. '^^-"&	%:"r   c                   U R                      [        R                  " 5       nU R                  c  Xl        XR                  -
  nX R                  -  S:  a'  U =R
                  X R                  -  -  sl        Xl        [        U R
                  U R                  5      U l        U R
                  S:  a  U =R
                  S-  sl         SSS5        g SSS5        g! , (       d  f       g= f)zTry to consume a token.

Returns:
    True means that the tokens were consumed, and the caller can proceed to
    make the request. A False means that the tokens were not consumed, and
    the caller should try again later.
Nr#   TF)r+   time	monotonicr,   r$   r(   minr&   )r   nowelapseds      r   _consumeInMemoryRateLimiter._consume   s     .."C yy 	IIoG111Q6%%3K3K)KK%	 %((=(=t?S?S$TD! $$)%%*%)  , -  s   B;CC
C)Tr   c                   U(       d  U R                  5       $ U R                  5       (       d7  [        R                  " U R                  5        U R                  5       (       d  M7  g)a  Attempt to acquire a token from the rate limiter.

This method blocks until the required tokens are available if `blocking`
is set to `True`.

If `blocking` is set to `False`, the method will immediately return the result
of the attempt to acquire the tokens.

Args:
    blocking: If `True`, the method will block until the tokens are available.
        If `False`, the method will return immediately with the result of
        the attempt.

Returns:
    `True` if the tokens were successfully acquired, `False` otherwise.
T)r5   r0   sleepr%   r   s     r   r   InMemoryRateLimiter.acquire   s@    " ==?"--//JJt112 --//r   c                  #    U(       d  U R                  5       $ U R                  5       (       d?  [        R                  " U R                  5      I Sh  vN   U R                  5       (       d  M?  g N7f)a  Attempt to acquire a token from the rate limiter. Async version.

This method blocks until the required tokens are available if `blocking`
is set to `True`.

If `blocking` is set to `False`, the method will immediately return the result
of the attempt to acquire the tokens.

Args:
    blocking: If `True`, the method will block until the tokens are available.
        If `False`, the method will return immediately with the result of
        the attempt.

Returns:
    `True` if the tokens were successfully acquired, `False` otherwise.
NT)r5   asyncior8   r%   r   s     r   r   InMemoryRateLimiter.aacquire   sR     " ==?"--// -- : :;;; --//  <s   AA1A/A1.A1)r+   r(   r%   r,   r&   r$   N)r$   floatr%   r=   r&   r=   r   None)r   r   r   )
r   r   r   r   r   r-   r5   r   r   r   r   r   r   r!   r!   C   s`    2n &''*!"&; #&;  %	&;
 &; 
&;P@ +/ 0 26  r   r!   )r   
__future__r   r   r;   r)   r0   ABCr   r!   __all__r   r   r   <module>rB      sF    A " 
   5cgg 5pq/ qj r   