Working with Rate Limit Errors in LangChain

Posted: Nov 13, 2024.

When building applications with LangChain that need rate limiting capabilities, you might encounter the UpstashRatelimitError. This guide explains what this error means and how to properly handle it in your applications.

What is UpstashRatelimitError?

UpstashRatelimitError is an exception that gets raised when you exceed the configured rate limits when using the Upstash rate limiting functionality in LangChain. This error helps you manage and respond to situations where your application hits either token-based or request-based rate limits.

Reference

The UpstashRatelimitError class accepts the following parameters:

ParameterTypeDescription
messagestrThe error message describing the rate limit violation
typestrThe type of limit that was reached - either "token" or "request"
limitint (optional)The maximum limit that was reached (only for request-type limits)
resetfloat (optional)Unix timestamp in milliseconds indicating when the limits will reset (only for request-type limits)

How to Handle UpstashRatelimitError

Here are different ways to work with and handle this error in your LangChain applications:

1. Basic Error Handling

The most straightforward way to handle the error is using a try-except block:

from langchain_community.callbacks import UpstashRatelimitError
from langchain_core.runnables import RunnableLambda

chain = RunnableLambda(str)

try:
    result = chain.invoke("Hello world!", config={"callbacks": [handler]})
except UpstashRatelimitError as e:
    # Handle the error based on type
    if e.type == "token":
        print("Token limit exceeded. Please wait before sending more requests.")
    else:
        print(f"Request limit exceeded. Limit resets at {e.reset}")

2. Implementing Retry Logic

You can implement a retry mechanism when hitting rate limits:

import time
from langchain_community.callbacks import UpstashRatelimitError

def execute_with_retry(chain, input_text, max_retries=3, delay=5):
    for attempt in range(max_retries):
        try:
            return chain.invoke(input_text, config={"callbacks": [handler]})
        except UpstashRatelimitError as e:
            if attempt == max_retries - 1:
                raise
            
            # Calculate delay based on reset time if available
            wait_time = (e.reset - time.time() if e.reset else delay)
            print(f"Rate limit hit. Waiting {wait_time} seconds before retry...")
            time.sleep(wait_time)

3. Different Handling for Token vs Request Limits

You might want to handle token and request limits differently:

from langchain_community.callbacks import UpstashRatelimitError

def handle_chain_execution(chain, input_text):
    try:
        return chain.invoke(input_text, config={"callbacks": [handler]})
    except UpstashRatelimitError as e:
        if e.type == "token":
            # For token limits, maybe try to break down the request
            return handle_token_limit(chain, input_text)
        else:
            # For request limits, implement queuing
            return queue_request(chain, input_text, e.reset)

def handle_token_limit(chain, input_text):
    # Logic to handle token limits (e.g., break down into smaller requests)
    pass

def queue_request(chain, input_text, reset_time):
    # Logic to queue request for later execution
    pass

4. Graceful Degradation

You can implement a fallback mechanism when rate limits are hit:

from langchain_community.callbacks import UpstashRatelimitError

def execute_with_fallback(primary_chain, fallback_chain, input_text):
    try:
        return primary_chain.invoke(input_text, config={"callbacks": [handler]})
    except UpstashRatelimitError:
        print("Rate limit hit, falling back to alternative service")
        return fallback_chain.invoke(input_text)

These patterns help you build more resilient applications that can handle rate limiting gracefully. Remember to always consider your specific use case when implementing error handling strategies, as different applications might require different approaches to handling rate limits.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs

Join 10,000+ subscribers

Every 2 weeks, latest model releases and industry news.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs