The pressure to ship AI products quickly has never been higher—but speed without safety is just recklessness in disguise. Every week brings news of another AI product that crashed spectacularly: runaway costs that bankrupted startups overnight, hallucinating chatbots that damaged brand reputation, or API outages that cascaded into complete system failures.
123456789101112// Feature flag configuration with kill switch interface FeatureConfig { enabled: boolean; rolloutPercentage: number; killSwitch: boolean; // Override that disables regardless of other settings allowedUserIds?: string[]; // For beta testing } const AI_FEATURE_CONFIG: FeatureConfig = { enabled: true, rolloutPercentage: 10, // 10% of users killSwitch: false,
123456789101112// Kill switch with local cache and fail-closed behavior class AIKillSwitch { private cache: Map<string, { value: boolean; expiry: number }> = new Map(); private readonly TTL_MS = 30000; // 30 second cache async isEnabled(feature: string, context: KillSwitchContext): Promise<boolean> { // Check local cache first const cached = this.cache.get(feature); if (cached && cached.expiry > Date.now()) { return cached.value; }
123456789101112class AICircuitBreaker { private state: 'closed' | 'open' | 'half-open' = 'closed'; private failures: number[] = []; // timestamps of recent failures private lastStateChange: number = Date.now(); private readonly config = { failureThreshold: 5, // failures to trip failureWindowMs: 60000, // 1 minute window openDurationMs: 30000, // 30 seconds before half-open halfOpenRequests: 3, // probes before closing costAnomalyMultiplier: 5, // 5x normal cost = failure latencyThresholdMs: 10000 // 10 second timeout = failure
123456789101112import { Redis } from 'ioredis'; import { CircuitBreaker, RateLimiter, CostTracker, FeatureFlags } from './safety'; const redis = new Redis(process.env.REDIS_URL); const safetyMiddleware = { flags: new FeatureFlags(redis), circuit: new CircuitBreaker({ failureThreshold: 5, resetTimeout: 30000, monitorWindow: 60000 }),
123456789101112import { Router } from 'express'; import { safetyMiddleware } from './safety'; const healthRouter = Router(); interface HealthStatus { status: 'healthy' | 'degraded' | 'unhealthy'; timestamp: string; components: { featureFlags: ComponentHealth; rateLimiter: ComponentHealth; costTracker: ComponentHealth;