# UpstashRatelimitHandler

> **Class** in `langchain_community`

📖 [View in docs](https://reference.langchain.com/python/langchain-community/callbacks/upstash_ratelimit_callback/UpstashRatelimitHandler)

Callback to handle rate limiting based on the number of requests
or the number of tokens in the input.

It uses Upstash Ratelimit to track the ratelimit which utilizes
Upstash Redis to track the state.

Should not be passed to the chain when initialising the chain.
This is because the handler has a state which should be fresh
every time invoke is called. Instead, initialise and pass a handler
every time you invoke.

## Signature

```python
UpstashRatelimitHandler(
    self,
    identifier: str,
    *,
    token_ratelimit: Optional[Ratelimit] = None,
    request_ratelimit: Optional[Ratelimit] = None,
    include_output_tokens: bool = False,
)
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `identifier Union[int, str]` | `unknown` | Yes | the identifier |
| `token_ratelimit Optional[Ratelimit]` | `unknown` | Yes | Ratelimit to limit the number of tokens. Only works with OpenAI models since only these models provide the number of tokens as information in their output. |
| `request_ratelimit Optional[Ratelimit]` | `unknown` | Yes | Ratelimit to limit the number of requests |
| `include_output_tokens bool` | `unknown` | Yes | Whether to count output tokens when rate limiting based on number of tokens. Only used when `token_ratelimit` is passed. False by default. |

## Extends

- `BaseCallbackHandler`

## Constructors

```python
__init__(
    self,
    identifier: str,
    *,
    token_ratelimit: Optional[Ratelimit] = None,
    request_ratelimit: Optional[Ratelimit] = None,
    include_output_tokens: bool = False,
)
```

| Name | Type |
|------|------|
| `identifier` | `str` |
| `token_ratelimit` | `Optional[Ratelimit]` |
| `request_ratelimit` | `Optional[Ratelimit]` |
| `include_output_tokens` | `bool` |


## Properties

- `raise_error`
- `identifier`
- `token_ratelimit`
- `request_ratelimit`
- `include_output_tokens`

## Methods

- [`on_chain_start()`](https://reference.langchain.com/python/langchain-community/callbacks/upstash_ratelimit_callback/UpstashRatelimitHandler/on_chain_start)
- [`on_llm_start()`](https://reference.langchain.com/python/langchain-community/callbacks/upstash_ratelimit_callback/UpstashRatelimitHandler/on_llm_start)
- [`on_llm_end()`](https://reference.langchain.com/python/langchain-community/callbacks/upstash_ratelimit_callback/UpstashRatelimitHandler/on_llm_end)
- [`reset()`](https://reference.langchain.com/python/langchain-community/callbacks/upstash_ratelimit_callback/UpstashRatelimitHandler/reset)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-community/blob/d5ea8358933260ad48dd31f7f8076555c7b4885a/libs/community/langchain_community/callbacks/upstash_ratelimit_callback.py#L47)