Email triage at scale
A cost-aware classification system that triages 60–70k emails a day into Act, Know or Ignore, using a tiered model cascade so most emails never reach an LLM.
Summary
- A tiered cascade resolves most emails before inference: deterministic rules first, then a MinHash similarity cache.
- I validated the cache in shadow mode before it replaced a single LLM call.
- Every prompt change is evaluated offline on thousands of emails, with regressions weighed against improvements, before release.
The problem
EmailZap classifies every incoming Gmail message as Act (needs a response), Know (belongs in the daily briefing) or Ignore: 60–70k decisions a day. An LLM classifies accurately, but per-email inference makes cost scale linearly with volume, and much of that volume is templated: recurring newsletters, transactional receipts, system notifications. The goal was to route only the emails that genuinely require a model to one.
A tiered model cascade
Each email exits at the cheapest tier that can classify it:
Escalation is selective: only senders the lightweight model returns as unknown are re-queried on the escalation model, so confident predictions are never paid for twice. I also decoupled classification from summarisation: summaries are generated only for emails classified as Act or Know, eliminating inference spend on ignored mail.
Validating the similarity cache
The cache reuses a prior classification when the same sender sends a near-duplicate email. Bypassing inference is only valuable if the reused labels are correct, so the rollout was built around four safeguards:
Shadow-mode rollout. The cache first ran alongside the model with the model’s output always authoritative. Launch required two gates: a minimum hit rate, for material impact, and at least 95% agreement with the model as the hard quality bar.
Continuous sampling. 5% of cache hits are re-verified by the model in production. On disagreement, the source signature is invalidated to stop error propagation, and the event is logged as drift.
Versioned signatures. Every signature is versioned, so any change to signature generation invalidates all prior signatures atomically.
Direct comparison over LSH. MinHash is typically paired with locality-sensitive hashing (LSH), which pays off at the scale of millions of candidates. Here, candidates are pre-scoped to at most 50 recent emails per user and sender, where direct signature comparison is cheaper than maintaining an index. A dependency-free implementation also kept roughly 100 MB of numerical libraries out of a Lambda with a 250 MB package limit.
Parameters: 128 permutations over 8-character shingles of the subject and body, with a match at an estimated Jaccard similarity of 0.8 or higher. Structural labels are reused, while summaries are always regenerated, since the varying details are precisely what a summary must capture.
Offline evaluation
A prompt that improves a handful of examples can regress hundreds of emails. Every prompt change is therefore A/B tested offline, baseline against candidate, on thousands of emails, with an independent LLM-as-a-judge adjudicating every disagreement. Changes are assessed on net effect (improvements minus regressions), not on selected examples.
Classifiers run with deterministic decoding for reproducibility, and every model parameter is captured in Langfuse traces, so differences between runs are attributable.
Relationship-aware classification
For business inboxes, priority depends on both the sender’s relationship to the user and the email type: a client’s contract question needs action, while the same client’s newsletter does not. The problem is therefore factored into two classifications:
- Sender relationship (7 classes), inferred once per sender from a user profile built at onboarding and cached for all subsequent email.
- Email type (6 classes).
A 7 × 6 attention matrix maps each pair to Act, Know or Ignore, with an urgency override. Classifying relationships per sender rather than per email amortises that inference across every email the sender sends.
User feedback is captured as rules: hiding a sender or promoting an email to the briefing creates one. Conflicts resolve by specificity, then recency, with explicit blocks taking precedence.
Guardrails and cost monitoring
Prompt-injection defence. Email content is attacker-controlled input. Every classifier prompt explicitly scopes email content and the user profile as untrusted data, never as instructions.
Prompt-cache monitoring. Provider prompt caching cuts the cost of static prompt prefixes, but only above a minimum token length, so a minor prompt edit can disable it. An hourly alarm fires when the cache hit ratio falls below 30%.
What I’d do next
- Confidence-based escalation. Escalate on calibrated confidence scores rather than only on unknown labels.
- Distillation. Use escalation-model labels to train a compact classifier for the highest-volume email types.
- Per-tier observability. Track each tier’s resolution rate in production and alert on shifts, as already done for the prompt-cache hit ratio.
What I learned
- The cheapest inference is the one you avoid. A cheaper model lowers the cost per call; rules and caching remove calls entirely.
- Every bypass needs a safety net. Shadow validation, continuous sampling and versioning are what made it safe for the cache to skip the model.
- Measure net effect. Evaluate improvements against regressions at volume before shipping.