···11+# Hybrid Approach: Rules + AI for Unknown States
22+33+## Overview
44+55+The **hybrid approach** (`filter-hybrid.gscript`) combines the best of both worlds:
66+77+- ✅ **Fast rule-based classification** for known patterns (100% accuracy, instant)
88+- ✅ **AI fallback** for uncertain/unknown cases (adaptability)
99+- ✅ **Confidence-based routing** (only call AI when needed)
1010+1111+## How It Works
1212+1313+```
1414+Email arrives
1515+ ↓
1616+┌─────────────────────────┐
1717+│ 1. Rule-Based Classifier│ (instant, free)
1818+└─────────────────────────┘
1919+ ↓
2020+Confidence ≥ 0.5?
2121+ ├─ YES → Use rules result (fast path) ✅
2222+ │ ~90% of emails take this path
2323+ ↓
2424+ └─ NO → Ask AI (slow path) 🤖
2525+ ~10% of emails need AI
2626+ ↓
2727+ AI verifies + strict overrides
2828+ ↓
2929+ Final decision
3030+```
3131+3232+## Performance Comparison
3333+3434+| Approach | Speed | Cost | Accuracy | Adaptability |
3535+|----------|-------|------|----------|--------------|
3636+| **Rules-only** | ⚡⚡⚡ | Free | 100%* | ❌ |
3737+| **AI-only** | ⚡ | $$ | ~85-90% | ✅ |
3838+| **Hybrid** (recommended) | ⚡⚡ | $ | 100%* | ✅ |
3939+4040+*100% on known patterns
4141+4242+## When AI is Used
4343+4444+The AI is **only called** when:
4545+4646+1. ✅ Confidence < 0.5 (uncertain cases)
4747+2. ✅ AI_API_KEY is set
4848+3. ✅ Not rate limited
4949+4. ✅ Within execution limits
5050+5151+Examples of uncertain emails (AI needed):
5252+- Unusual scholarship formats
5353+- New types of college communications
5454+- Edge cases not in training data
5555+- Complex multi-topic emails
5656+5757+Examples of certain emails (rules-only):
5858+- Security alerts (100% match)
5959+- Application confirmations (100% match)
6060+- Newsletter spam (100% match)
6161+- Marketing emails (100% match)
6262+6363+## Configuration
6464+6565+### Confidence Threshold
6666+6767+```javascript
6868+const AI_CONFIDENCE_THRESHOLD = 0.5; // Adjust this
6969+```
7070+7171+- **Lower (0.3)**: More AI calls, more adaptable
7272+- **Higher (0.7)**: Fewer AI calls, faster/cheaper
7373+- **Recommended: 0.5** - Good balance
7474+7575+### Other Settings
7676+7777+```javascript
7878+const MAX_THREADS_PER_RUN = 75; // Process up to 75/run
7979+const MAX_AI_CALLS_PER_HOUR = 100; // Rate limit for AI
8080+```
8181+8282+## Statistics & Logging
8383+8484+The hybrid script tracks:
8585+8686+```
8787+Summary:
8888+ RulesOnly=45 # Emails classified by rules alone
8989+ AICalls=5 # Emails that needed AI
9090+ Uncertain=5 # Low confidence cases
9191+ AppliedInbox=8
9292+ AppliedFiltered=42
9393+```
9494+9595+**Logs show decision path:**
9696+9797+```
9898+[Thread abc] RULES-ONLY Relevant=false Confidence=0.95 Reason="Marketing/newsletter"
9999+[Thread def] LOW CONFIDENCE (0.3) - Asking AI...
100100+[Thread def] AI RESULT Relevant=true Reason="Scholarship info" (Rules suggested: false)
101101+```
102102+103103+## Migration from AI-Only
104104+105105+If you're using the original AI-based script:
106106+107107+1. **Backup** current script
108108+2. **Copy** `filter-hybrid.gscript`
109109+3. **Keep** your existing `AI_API_KEY`
110110+4. **Test** with `DRY_RUN = true`
111111+5. **Go live** when satisfied
112112+113113+**Benefits:**
114114+- 20x faster for most emails (rules)
115115+- 90% reduction in AI calls
116116+- Still adaptive for edge cases
117117+- Same accuracy guarantees
118118+119119+## Migration from Rules-Only
120120+121121+If you want to add AI adaptability:
122122+123123+1. **Copy** `filter-hybrid.gscript`
124124+2. **Set** `AI_API_KEY` in Script Properties
125125+3. **Test** with `DRY_RUN = true`
126126+4. **Adjust** `AI_CONFIDENCE_THRESHOLD` if needed
127127+128128+**Benefits:**
129129+- Handles unknown email types
130130+- Learns from edge cases
131131+- More robust over time
132132+133133+## Choosing the Right Approach
134134+135135+### Use **Rules-Only** (`filter-optimized.gscript`) if:
136136+- ✅ You want maximum speed (20x faster)
137137+- ✅ You want zero cost (free, unlimited)
138138+- ✅ Your email patterns are consistent
139139+- ✅ You'll label edge cases manually
140140+141141+### Use **Hybrid** (`filter-hybrid.gscript`) if:
142142+- ✅ You want adaptability for unknown states
143143+- ✅ College emails change formats frequently
144144+- ✅ You want AI as safety net
145145+- ✅ You're okay with small AI cost (~10% of emails)
146146+147147+### Use **AI-Only** (original `filter.gscript`) if:
148148+- ✅ You don't want to maintain rules
149149+- ✅ Cost/speed isn't a concern
150150+- ✅ You prefer black-box approach
151151+152152+**Recommendation: Hybrid** - Best of both worlds!
153153+154154+## Monitoring & Tuning
155155+156156+### Watch for High Uncertainty
157157+158158+If logs show many `Uncertain` emails:
159159+160160+```
161161+INFO: 15 emails had low confidence. Consider labeling them to improve rules.
162162+```
163163+164164+**Action**: Label those emails and update rules:
165165+1. Export uncertain emails
166166+2. Label in web interface (`bun label`)
167167+3. Run `bun evaluate` to check accuracy
168168+4. Update patterns in classifier
169169+5. Re-deploy hybrid script
170170+171171+### Adjust Threshold
172172+173173+Track `RulesOnly` vs `AICalls` ratio:
174174+175175+- **Want faster**: Increase threshold to 0.6-0.7
176176+- **Want more adaptive**: Decrease to 0.3-0.4
177177+- **Balanced**: Keep at 0.5
178178+179179+## Cost Estimates
180180+181181+Based on typical college email volume:
182182+183183+| Emails/day | AI calls (10%) | Cost/month* |
184184+|------------|----------------|-------------|
185185+| 50 | 5/day | ~$0.50 |
186186+| 100 | 10/day | ~$1.00 |
187187+| 200 | 20/day | ~$2.00 |
188188+189189+*Assuming $0.001 per AI call (varies by provider)
190190+191191+Compare to:
192192+- **Rules-only**: $0/month
193193+- **AI-only**: $5-20/month
194194+195195+## Troubleshooting
196196+197197+### "Too many AI calls"
198198+199199+**Symptoms**: Logs show `AICalls` close to total emails
200200+201201+**Causes**:
202202+- Threshold too low
203203+- Rules not matching well
204204+- Many edge cases
205205+206206+**Solutions**:
207207+1. Increase `AI_CONFIDENCE_THRESHOLD` to 0.6
208208+2. Review uncertain emails and add rules
209209+3. Check if patterns need updating
210210+211211+### "Missing important emails"
212212+213213+**Symptoms**: Relevant emails going to filtered
214214+215215+**Causes**:
216216+- Rules returning low confidence
217217+- AI making wrong decision
218218+219219+**Solutions**:
220220+1. Check logs for those emails
221221+2. Add specific patterns to rules
222222+3. Adjust strict overrides in `enforceStrictRules_()`
223223+224224+### "Still getting spam"
225225+226226+**Symptoms**: Marketing emails in inbox
227227+228228+**Causes**:
229229+- New spam patterns not in rules
230230+- AI being too lenient
231231+232232+**Solutions**:
233233+1. Label those emails as not relevant
234234+2. Add patterns to `checkIrrelevant_()`
235235+3. Lower confidence for unknown emails
236236+237237+## Best Practices
238238+239239+1. **Start with hybrid** - Get benefits of both
240240+2. **Monitor stats** - Watch RulesOnly vs AICalls ratio
241241+3. **Label edge cases** - Improve rules over time
242242+4. **Tune threshold** - Based on your needs
243243+5. **Review logs** - Weekly check for patterns
244244+245245+## Example Log Output
246246+247247+```
248248+[2025-12-05 10:15:30] Processing up to 50 threads
249249+[Thread 123] RULES-ONLY Relevant=false Confidence=0.95 Reason="Newsletter" Subject="Campus Events This Week"
250250+ Applied: Added "College/Filtered" and archived
251251+[Thread 124] RULES-ONLY Relevant=true Confidence=1.0 Reason="Security alert" Subject="Password Reset Required"
252252+ Applied: Removed "College/Auto" and moved to Inbox
253253+[Thread 125] LOW CONFIDENCE (0.3) - Asking AI... Subject="Your Future at State U"
254254+[Thread 125] AI RESULT Relevant=false Reason="Generic marketing" (Rules suggested: false)
255255+ Applied: Added "College/Filtered" and archived (AI verified)
256256+257257+Summary: RulesOnly=48, AICalls=2, Uncertain=2, AppliedInbox=8, AppliedFiltered=42
258258+```
259259+260260+Perfect balance! 96% handled by rules, 4% needed AI.