| name | deepgram-rate-limits |
| description | Implement Deepgram rate limiting and backoff strategies.
Use when handling API quotas, implementing request throttling,
or dealing with rate limit errors.
Trigger with phrases like "deepgram rate limit", "deepgram throttling",
"429 error deepgram", "deepgram quota", "deepgram backoff".
|
| allowed-tools | Read, Grep, Bash(curl:*) |
| version | 1.0.0 |
| license | MIT |
| author | Jeremy Longshore <jeremy@intentsolutions.io> |
Deepgram Rate Limits
Overview
Implement proper rate limiting and backoff strategies for Deepgram API integration.
Deepgram Rate Limits
| Plan | Concurrent Requests | Requests/Minute | Audio Hours/Month |
|---|
| Pay As You Go | 100 | 1000 | Unlimited |
| Growth | 200 | 2000 | Included hours |
| Enterprise | Custom | Custom | Custom |
Instructions
Step 1: Implement Request Queue
Create a queue to manage concurrent request limits.
Step 2: Add Exponential Backoff
Handle rate limit responses with intelligent retry.
Step 3: Monitor Usage
Track request counts and audio duration.
Step 4: Implement Circuit Breaker
Prevent cascade failures during rate limiting.
Output
- Rate-limited request queue
- Exponential backoff handler
- Usage monitoring dashboard
- Circuit breaker implementation
Examples
TypeScript Rate Limiter
interface RateLimiterConfig {
maxConcurrent: number;
maxPerMinute: number;
retryAttempts: number;
baseDelay: number;
}
export class DeepgramRateLimiter {
private queue: Array<{
fn: () => Promise<unknown>;
resolve: (value: unknown) => void;
reject: (error: Error) => void;
}> = [];
private activeRequests = 0;
private requestsThisMinute = 0;
private minuteStart = Date.now();
private config: RateLimiterConfig;
constructor(config: Partial<RateLimiterConfig> = {}) {
this.config = {
maxConcurrent: config.maxConcurrent ?? 50,
: config. ?? ,
: config. ?? ,
: config. ?? ,
};
}
execute<T>(: <T>): <T> {
( {
..({
fn,
: resolve (: ) => ,
reject,
});
.();
});
}
() {
now = .();
(now - . >= ) {
. = ;
. = now;
}
(. >= ..) ;
(. >= ..) ;
(.. === ) ;
{ fn, resolve, reject } = ..()!;
.++;
.++;
{
result = .(fn);
(result);
} (error) {
(error ? error : ((error)));
} {
.--;
.();
}
}
executeWithRetry<T>(
: <T>,
attempt =
): <T> {
{
();
} (error) {
isRateLimited = error &&
(error..() || error..());
(isRateLimited && attempt < ..) {
delay = .. * .(, attempt);
jitter = .() * ;
( (r, delay + jitter));
.(fn, attempt + );
}
error;
}
}
() {
{
: .,
: ..,
: .,
};
}
}
Exponential Backoff with Jitter
interface BackoffConfig {
baseDelay: number;
maxDelay: number;
factor: number;
jitter: boolean;
}
export class ExponentialBackoff {
private attempt = 0;
private config: BackoffConfig;
constructor(config: Partial<BackoffConfig> = {}) {
this.config = {
baseDelay: config.baseDelay ?? 1000,
maxDelay: config.maxDelay ?? 60000,
factor: config.factor ?? 2,
jitter: config.jitter ?? true,
};
}
getDelay(): number {
const exponential = this.config.baseDelay *
Math.pow(this.config.factor, this.attempt);
const capped = .(exponential, ..);
(..) {
.() * capped;
}
capped;
}
(): {
.++;
}
(): {
. = ;
}
(): <> {
delay = .();
( (resolve, delay));
.();
}
}
backoff = ();
() {
maxAttempts = ;
( i = ; i < maxAttempts; i++) {
{
(url);
} (error) {
(i === maxAttempts - ) error;
(error && error..()) {
.();
backoff.();
} {
error;
}
}
}
}
Circuit Breaker Pattern
enum CircuitState {
CLOSED = 'CLOSED',
OPEN = 'OPEN',
HALF_OPEN = 'HALF_OPEN',
}
interface CircuitBreakerConfig {
failureThreshold: number;
resetTimeout: number;
halfOpenRequests: number;
}
export class CircuitBreaker {
private state = CircuitState.CLOSED;
private failures = 0;
private lastFailure = 0;
private halfOpenSuccesses = 0;
private config: CircuitBreakerConfig;
constructor(config: Partial<CircuitBreakerConfig> = {}) {
this.config = {
failureThreshold: config.failureThreshold ?? 5,
resetTimeout: config.resetTimeout ?? 30000,
halfOpenRequests: config.halfOpenRequests ?? 3,
};
}
async execute<T>(fn: <T>): <T> {
(. === .) {
(.() - . > ..) {
. = .;
. = ;
} {
();
}
}
{
result = ();
(. === .) {
.++;
(. >= ..) {
. = .;
. = ;
}
}
result;
} (error) {
.();
error;
}
}
() {
.++;
. = .();
(. >= ..) {
. = .;
.();
}
}
(): {
.;
}
}
Usage Monitor
interface UsageStats {
requestCount: number;
audioSeconds: number;
errorCount: number;
rateLimitHits: number;
startTime: Date;
}
export class DeepgramUsageMonitor {
private stats: UsageStats = {
requestCount: 0,
audioSeconds: 0,
errorCount: 0,
rateLimitHits: 0,
startTime: new Date(),
};
recordRequest(audioSeconds: number = 0) {
this.stats.requestCount++;
this.stats.audioSeconds += audioSeconds;
}
recordError(isRateLimit: boolean = false) {
this.stats.errorCount++;
if (isRateLimit) {
this.stats.rateLimitHits++;
}
}
(): & { : ; : } {
uptimeMs = .() - ...();
{
....,
: .(..),
: uptimeMs / ,
};
}
(: ): {
hours = .(seconds / );
minutes = .((seconds % ) / );
;
}
(): {
hitRate = .. / ..;
hitRate > && .. > ;
}
}
Python Rate Limiter
import asyncio
import time
from collections import deque
from typing import Callable, TypeVar
T = TypeVar('T')
class RateLimiter:
def __init__(
self,
max_concurrent: int = 50,
max_per_minute: int = 500
):
self.max_concurrent = max_concurrent
self.max_per_minute = max_per_minute
self.semaphore = asyncio.Semaphore(max_concurrent)
self.request_times: deque = deque()
async def execute(self, fn: Callable[[], T]) -> T:
await self._wait_for_rate_limit()
async with self.semaphore:
self.request_times.append(time.time())
return await fn()
async def _wait_for_rate_limit(self):
now = time.time()
while self.request_times and now - self.request_times[0] > 60:
.request_times.popleft()
(.request_times) >= .max_per_minute:
wait_time = - (now - .request_times[])
wait_time > :
asyncio.sleep(wait_time)
Resources
Next Steps
Proceed to deepgram-security-basics for security best practices.