Preventing Runaway LLM Bills: Building a Resilient AI Worker Queue with Redis, BullMQ, and Model Fallbacks A developer released resilient-ai-worker-kit, an open-source TypeScript toolkit that pairs BullMQ and Redis to cap runaway LLM API spend. The kit pre-checks estimated tokens against an atomic Redis daily budget counter before dispatch, routes requests through a multi-provider failover chain on 429 or 500 errors, and records actual token usage after each job, with jittered exponential retries in the worker. Building autonomous agents or AI-driven background tasks is exciting—until a bad retry loop or unexpected 429 rate-limit cascades into a $2,000 API bill overnight. In production, naive API calls with standard try/catch fail for three predictable reasons: In this tutorial, we will construct a production-ready AI Worker Queue in TypeScript using BullMQ and Redis , featuring: php flowchart TD A Incoming Task Request -- B BullMQ Job Queue B -- C{Daily Budget Pre-Check} C -- Exceeds Spend Ceiling -- D Abort / Kill Switch Triggered C -- Within Budget Limit -- E Resilient Multi-Provider Router E -- Primary 429/Timeout -- F Fallback Provider: Groq / Anthropic E -- Primary Success -- G Worker Output Delivered F -- G G -- H Atomic Redis Token Increment Before an expensive prompt is dispatched to an LLM provider, we pre-calculate estimated tokens and verify against an atomic Redis counter: export class BudgetManager { private redis: Redis; private keyPrefix = "ai budget:daily:"; public async canExecute estimatedTokens: number : Promise<{ allowed: boolean; currentUsage: number; reason?: string } { const today = new Date .toISOString .split "T" 0 ; const key = ${this.keyPrefix}${today} ; const current = parseInt await this.redis.get key || "0", 10 ; if current + estimatedTokens MAX DAILY TOKEN BUDGET { return { allowed: false, currentUsage: current, reason: Daily token limit breached: ${current} + ${estimatedTokens} ${MAX DAILY TOKEN BUDGET} , }; } return { allowed: true, currentUsage: current }; } public async recordUsage tokens: number : Promise