Chatbot Kit

Rate Limiter

The Rate Limiter workflow is designed to prevent abuse and ensure fair usage of your AI chatbot by controlling the number of requests a user (identified by IP) can make within a specified time period.

This workflow uses Valkey with Redis nodes as the backend store to efficiently count requests and enforce limits.

How It Works

  1. Triggered by Another Workflow

    • The rate limiter is not a standalone entrypoint.
    • It’s designed to be executed by other workflows whenever a new request comes in.
    • Input:
      • ip → the identifier for the requester (usually the client’s IP address).
  2. Define Rate Limiter Settings

    • Generates a Redis key for the requester:

      rateLimiter:<ip>
      
    • Defines two parameters:

      • rateLimit: maximum number of requests allowed (default 20).
      • rateLimitTTL: time window in seconds (default 86400 → 24 hours).
  1. Redis Counter

    • Increments the counter stored at the requester’s key.
    • If the key does not exist, it’s created with the defined TTL.
  2. Rate Limit Check

    • Compares the current counter against the configured rateLimit.
    • Sets a boolean rateLimitReached:
      • true → user exceeded the limit.
      • false → user is within the allowed quota.

Example

Suppose the rate limit is set to 20 requests per 24 hours:

  • User with IP 192.168.0.10 makes a request.
  • Workflow increments the counter rateLimiter:192.168.0.10.
  • If the counter ≤ 20 → rateLimitReached = false.
  • On the 21st request (within 24h) → rateLimitReached = true.

Usage in Your Chatbot

  • This workflow is called from the chat and text-to-speech workflows before processing the request.

  • Check the output rateLimitReached.

    • If true → block or delay the request, and return a rate-limit message.
    • If false → allow normal chatbot processing.

Default Configuration

  • Requests allowed: 20
  • Time window: 24 hours
  • Storage: Valkey

You can adjust the limit and TTL in the Define Rate Limiter Settings node to fit your needs.

This is a simple implementation and can be extended with features like user subscriptions or premium tiers.