API Reference
Welcome to the Choked API reference. This section provides detailed documentation for all classes, functions, and modules in the Choked library.Overview
Choked provides a powerful yet simple API for dual rate limiting (requests + tokens) using the token bucket algorithm. The main components are:- Choked class: The primary class for creating rate limiting decorators
- Token Bucket: The underlying token bucket implementations
Quick Reference
Main Class
Key Parameters
Choked Class Constructor
redis_url: Redis connection URL (mutually exclusive with api_token)api_token: API token for managed service (mutually exclusive with redis_url)
Decorator Parameters
key: Unique identifier for the rate limit bucketrequest_limit: Request rate limit (e.g., “10/s”, “100/m”) - optionaltoken_limit: Token rate limit (e.g., “1000/s”, “100000/m”) - optionaltoken_estimator: Token estimation method (“openai”, “voyageai”, “default”) - required for token_limit
Backend Selection
Choked supports two backend types:- Redis Backend: When
redis_urlis provided - best for self-hosted, high-performance scenarios - Managed Service: When
api_tokenis provided - best for zero-infrastructure setups
Rate Limiting Types
Request-Only Limiting
Token-Only Limiting
Dual Limiting
Token Estimation
Choked includes built-in token estimators for popular AI services:- OpenAI: Uses tiktoken with GPT-4 tokenizer
- VoyageAI: Uses HuggingFace voyageai/voyage-3.5 tokenizer
- Default: Same as OpenAI estimator
Error Handling
Validation Errors
Runtime Behavior
- Network failures: Automatic retry with exponential backoff
- Token estimation failures: Graceful fallback to simpler estimators
- Redis connection issues: Retries with backoff (Redis backend only)
Thread Safety
- All operations are thread-safe and async-safe
- Redis backend uses atomic Lua scripts
- Managed service backend uses HTTP-based coordination
- Safe for use in multi-threaded and multi-process applications
Next Steps
- Learn about the Choked class in detail
- Understand Token Bucket implementations