# Advanced Failover & Hybrid Intelligence Strategy ## 1. Cascading Key & Model Failover (Chain of Best) The system implements a multi-tier failover strategy to ensure 100% uptime: - **Intra-Model Rotation**: If a model hits a rate limit (429), it immediately rotates to the next available API key in the pool. - **Exhaustive Search**: The system checks all keys. If all are on cooldown, it identifies the one with the soonest availability. - **Cross-Model Cascading**: If all keys for Model A are exhausted (Daily Quota reached), it switches to **Model B** (Next Best Model) and resets the key index to 0, ensuring a fresh attempt with all keys. - **Reverse Search Logic**: The cyclic rotation ensures that even if specific keys are throttled, the system eventually finds an entry point. ## 2. Hybrid Intelligence Extraction (LLM + Regex) To prevent data loss during LLM downtime, the extraction pipeline is now strictly decoupled: - **Regex Baseline**: Every incoming message is first processed by high-performance Regex patterns in `app/utils/extractors.py`. - **LLM Augmentation**: The LLM runs in a `try-except` block. It validates findings and discovers "soft" intelligence (names, context) that Regex might miss. - **Guaranteed Persistence**: If the LLM crashes or stalls, the `IntelligenceExtractor` catches the error and returns the Regex findings. Intelligence is never lost. - **Validation**: LLM-extracted data is cross-validated against Regex patterns to filter out "hallucinated" phone numbers or UPI IDs. ## 3. High-Quality Static Fallbacks When the system enters "Survival Mode" (all APIs down), it uses high-quality templates in `PersonaEngine`: - **Persona Consistency**: Replies like "Main drive kar raha hoon, ruko" or "Net problem hai" maintain the deceptive persona even without AI generation. - **Phase-Awareness**: Fallbacks vary based on the conversation stage (Hook, Engage, Extract, Stall). - **Time-Awareness**: If it's late at night, the static reply includes a sleep-deprived context ("Itni raat ko? Kal baat karein?"). ## 4. Verification Check Verified with `test_hybrid_failover.py`: ✅ Simulated LLM Crash during extraction. ✅ Baseline Regex intelligence (UPI, Bank, Phone) successfully captured. ✅ System stability maintained.