Skip to main content

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 = 5 requests 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 Request without 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:
  1. Error Tracking: Consecutive failures increment ai:circuit:errors in Redis.
  2. Tripping Threshold: When errors exceed the threshold (AI_TUTOR_CIRCUIT_ERROR_THRESHOLD = 5), the circuit trips open (ai:circuit:open).
  3. Fail-Fast Fallback: While the circuit is open, subsequent AI requests immediately return graceful fallback messages without attempting upstream connections.
  4. Automatic Reset: The circuit automatically resets after a cooldown period (AI_TUTOR_CIRCUIT_RESET_TTL = 120 seconds) to probe upstream recovery.

3. Comprehensive AI Audit Telemetry (ai_analytics_logs)

Every AI transaction across all subsystems is recorded in PostgreSQL for auditing, quality evaluation, and model fine-tuning: