AI Governance, Resilience & Resilience Engineering
To operate generative AI safely, reliably, and cost-effectively in production, LearnWay enforces strict governance layers, circuit breakers, and comprehensive telemetry.1. Multi-Tier Safety Guardrails
Tier 1: Identity & Quota Control
- Per-Lesson Quotas: Restricts in-lesson AI tutoring requests (
AI_TUTOR_LESSON_LIMIT = 5requests per lesson) to prevent automated scraping and encourage independent problem solving. - Daily Quotas: Caps total daily requests per user (
AI_TUTOR_DAILY_LIMIT = 20) to ensure equitable GPU resource allocation. - Redis Atomic Tracking: Tracked atomically using Redis keys with automatic daily midnight expirations.
Tier 2: Pre-Inference Sanitization & Profanity Filtering
Before any prompt reaches Google Gemini:- Incoming text is evaluated through
containsProfanity()(ai-tutor.profanity.ts). - Abusive or violating prompts are rejected immediately with a
400 Bad Requestwithout consuming LLM inference tokens or backend latency.
2. Upstream Resilience: Circuit Breakers & Timeout Races
Generative AI calls are external dependencies that must never cascade failures into core learning navigation or user progress.5-Second Latency Race (Promise.race)
Every call to Google Gemini is wrapped in a strict timeout race:
Redis Circuit Breaker State Machine
If upstream Gemini endpoints experience network partitions or elevated error rates:- Error Tracking: Consecutive failures increment
ai:circuit:errorsin Redis. - Tripping Threshold: When errors exceed the threshold (
AI_TUTOR_CIRCUIT_ERROR_THRESHOLD = 5), the circuit trips open (ai:circuit:open). - Fail-Fast Fallback: While the circuit is open, subsequent AI requests immediately return graceful fallback messages without attempting upstream connections.
- Automatic Reset: The circuit automatically resets after a cooldown period (
AI_TUTOR_CIRCUIT_RESET_TTL = 120 seconds) to probe upstream recovery.