Ask your inbox

Search that answers plain-language questions about your email, like "what did Acme charge me last month?", using hybrid lexical and semantic retrieval with source-grounded answers.

Built at EmailZap · Started Jun 2025 · done · source private

Summary

  • Natural-language questions over a user’s inbox, answered with retrieval-augmented generation and grounded in cited source emails.
  • Lexical and semantic retrieval run in parallel, merged with reciprocal rank fusion, so no score calibration is needed.
  • I built the evaluation pipeline: every retrieval design is benchmarked on a curated golden set for precision, recall and answer correctness before it ships.

The problem

People ask EmailZap things Gmail’s search box can’t answer: “how much was my Acme bill this month?”, “what did Priya say about the roadmap review?”. Each one needs the right few emails found among thousands, then an answer built from them that the user can check.

Neither retrieval method is sufficient on its own:

  • Lexical search (BM25) is precise on entities. “Acme” matches Acme and nothing else, but it misses a “payment confirmation” when the query says “receipt”.
  • Semantic search (vector embeddings) matches “roadmap review” to an email about rescheduling the planning meeting, but an Acme invoice and a Globex invoice embed almost identically.

A hybrid retriever runs both, so each covers the other’s failure mode.

Retrieval pipeline

Query understanding extracts dates, entities and intent keywords. Lexical and semantic retrieval then run in parallel. Their rankings are merged with reciprocal rank fusion, and a grounded answer is generated from the top results with citations. Query understanding dates, entities, intent Lexical search BM25, field-boosted Semantic search vector similarity Rank fusion weighted RRF Grounded generation cited source emails
Both retrievers run concurrently, so latency is bounded by the slower branch rather than their sum.
  1. Routing and query understanding. An LLM router first decides whether the query needs retrieval at all. A structured-output call then extracts a date range, named entities and intent keywords expanded with synonyms (receipt → invoice, payment, charge).
  2. Parallel retrieval, as concurrent branches of a LangGraph graph.
  3. Rank fusion into a single result list.
  4. Grounded generation. The model answers from the top-ranked emails and returns the ones it cited. Only cited emails are surfaced, so every claim is traceable to its source.

Every query is pre-filtered by tenant before scoring, which guarantees strict data isolation between users.

Key design decisions

Rank fusion over score fusion. BM25 scores are unbounded, while cosine similarity is bounded between 0 and 1. Combining raw scores requires a calibration that drifts as the corpus changes. Reciprocal rank fusion uses only rank positions, rewarding documents that rank highly in both lists, with no calibration to maintain.

Entity-filtered semantic search. When a query names an entity, lexical search first selects the chunks containing it, and vector search runs only over that candidate set. This removes semantically similar but wrong-entity results, such as a Globex invoice for an Acme query.

Field boosting and relevance cutoffs. Sender fields carry a 2× boost, since the sender is usually the strongest signal in an email query. Lexical hits below 15% of the top score are pruned, keeping the context window small and precise.

Lazy summarisation. Long emails are summarised on first retrieval, then persisted and reused. Compute is spent only on emails that are actually queried.

Dimensionality reduction. I reduced embeddings from 1,536 to 256 dimensions, a sixth of the storage per vector, using the embedding model’s native support for shortened vectors.

Conversational retrieval

Users rarely ask one question. They follow up with “and the month before?” or “who owns the action items in the second one?”. Retrievers are stateless, so conversation context has to be resolved before retrieval, not after.

  • Query contextualisation. The last six messages and the new one are rewritten into a standalone query: “and the month before?” becomes “Acme invoices for September 2025”. Lexical and semantic retrieval then run on the rewritten query.
  • Reference resolution. When a follow-up points at earlier results, the emails cited in the previous answer are reloaded and fused with double weight, so they stay at the top.
  • Persistent threads. Conversation state is checkpointed per thread through a Postgres-backed LangGraph checkpointer, with thread-level ownership checks and history, listing and deletion APIs. Per-turn retrieval state is reset on every turn, so one answer’s documents never leak into the next.
  • Bounded growth. Each thread keeps at most 100 checkpoints, retaining those that record user input or errors, and inactive threads expire after 30 days.

Evaluation

Retrieval quality is hard to judge by inspection, so I built an offline evaluation pipeline:

  • Synthetic golden dataset. Queries span 20+ known failure categories: typos, synonyms, relative dates, attachments, long threads and tone. For each query, a generator model writes realistic matching emails, and a critic model filters them for realism and relevance. Spam is injected as hard negatives. No production user data is used.
  • Retrieval and generation metrics. Retrieval is scored on precision and recall against the expected emails. Answers are scored with LLM-as-a-judge for correctness, hallucination and relevance.
  • Experiment tracking. Each retrieval architecture runs as a Langfuse dataset experiment on the same set, for side-by-side comparison. A separate harness benchmarks latency on a fixed query set.

Reliability

  • Schema-validated outputs. Every model step returns a typed, validated result, so malformed output fails fast instead of propagating.
  • Cross-provider fallbacks. Each step has a fallback model from a different provider, and every call retries with exponential backoff and jitter.
  • Zero-result fallback. If the index returns nothing, the query fans out to the Gmail API across all linked accounts in parallel.

What I’d do next

  • Cross-encoder re-ranking. Re-rank the fused top results with a cross-encoder for higher precision, after measuring the latency cost on the golden set.

What I learned

  • Complementary retrievers beat a single tuned one. Lexical and semantic search fail in different ways, so a hybrid fixes more than tuning either alone.
  • Prefer robust over clever. Rank fusion needed less code than a learned re-ranker and no ongoing calibration.
  • Grounding builds trust. Citing source emails makes correct answers verifiable and incorrect ones easy to spot.