This appendix makes the assumptions behind common RAG components explicit. The equations are families, not claims that every implementation uses the same normalization or loss.
1. Sparse retrieval
Let \(f(t,d)\) be term frequency, \(n_t\) document frequency, \(N\) corpus size, \(|d|\) document length, and \(\overline{|d|}\) average length. A common positive BM25 IDF and score are
The inverted index stores postings only for nonzero terms, making exact search efficient and incremental updates natural. Fields can have separate weights; filters can enforce tenant, time, document type, and authority before scoring.
Learned sparse models such as SPLADE replace observed term counts with sparse vocabulary weights predicted by a transformer. A FLOPS-like regularizer penalizes expected activation across the collection so postings remain usable. This gives semantic expansion without abandoning inverted-index infrastructure, but index size and latency are learned hyperparameters rather than fixed facts.
2. Dense dual-encoder retrieval
A dual encoder maps a query and passage independently:
or cosine similarity after normalization. Given a positive passage \(d^+\) and negatives \(\mathcal N\), an InfoNCE-style loss is
In-batch positives from other questions provide many cheap negatives. Hard negatives from BM25, a previous dense model, or a cross-encoder teach distinctions near the decision boundary. False negatives are dangerous: a passage may be unlabeled but valid, so the loss pushes useful evidence away.
At serving time, maximum-inner-product or cosine search uses an approximate nearest-neighbor index. Important parameters include vector dimension, quantization, graph/list construction depth, search probes, rerank candidate count, and exact-verification stage. Approximation error must be measured by comparing ANN with exact search on a representative subset. The embedding revision is part of the index schema; changing it normally requires re-encoding.
Single-vector retrieval compresses an entire passage into one point. It handles paraphrase but can discard rare terms, fine-grained relations, and multiple topics. Domain length, language, instruction prefix, normalization, and training negatives can change ranking substantially.
3. Cross-encoders and late interaction
A cross-encoder jointly processes \([q;d]\) and returns a relevance or utility score:
It models every query-document interaction but cannot precompute a document score independent of the query. It therefore reranks tens or hundreds of candidates rather than millions.
Late-interaction systems occupy the middle. ColBERT retains contextual token vectors and uses
Document token vectors are precomputable; query-token maxima preserve detailed alignment. The cost is a multi-vector index. ColPali applies the same operation between query tokens and page-image patches.
A reranker for RAG should ideally predict downstream evidence utility, not only topical relevance. A passage can be relevant yet redundant, stale, contradictory, or too ambiguous to improve the answer. Utility labels can be defined by claim support or by the causal change in a fixed reader when a passage is included, but reader-specific labels may not transfer.
4. Latent-document generation
Let \(z\) be a retrieved document and \(y\) an answer. The ideal marginal is
The corpus is too large, so RAG truncates to top \(k\). RAG-Sequence assumes one latent document for the full sequence:
RAG-Token moves the document sum inside the token product:
Negative log likelihood sends learning signal to the query encoder when a retrieved document makes the target likely. It does not establish that the document is factually supporting: spurious correlation or parametric memory can produce the answer. The top-\(k\) operator is discrete; documents outside it receive no gradient. A stale fixed document index means the updated query encoder scores vectors produced by an older document encoder.
5. Multi-passage fusion
Naively concatenating \(k\) passages of length \(L\) into one transformer encoder gives self-attention complexity approximately \(O((kL)^2)\). FiD encodes each passage independently, about \(O(kL^2)\), concatenates encoder states, and lets the decoder cross-attend over all of them. This makes 100 passages feasible but decoder attention and memory still grow with total encoded length.
Reader-to-retriever learning uses a teacher distribution \(p_T(d\mid q,a)\) from reader attention or per-document answer likelihood and minimizes
Attention is easy to extract but not necessarily causal. Per-document answer likelihood is closer to utility but can reward answer leakage. Stop-gradient is often used so the auxiliary retriever signal does not distort the reader.
6. Query transformation and multi-query fusion
Query rewriting seeks a transformation \(q'=g(q,h)\), possibly conditioned on conversation history \(h\), that improves evidence retrieval. HyDE instead generates a hypothetical answer document \(\tilde d\) and retrieves with \(E_D(\tilde d)\). Multi-query methods generate \(q_1,\ldots,q_m\), search each, and fuse candidates.
Generated expansions can introduce detail that was never requested. Evaluate intent preservation, source recall, latency, model calls, and sensitivity to sampling. Keep the original query in the fusion so generated text cannot erase rare exact terms. Reciprocal-rank fusion avoids incompatible score scales:
The constant \(K\) controls how quickly rank contributions decay. RRF is robust but discards score margins; learned calibration may exploit margins at the cost of more labels and drift.
7. Context selection as constrained optimization
Given candidates \(D\), budget \(B\), length \(\ell(d)\), and utility \(u(d\mid q,S)\) conditional on already selected evidence \(S\), context construction resembles a knapsack/submodular problem:
Maximal marginal relevance is a simple greedy surrogate:
Real constraints add per-document limits, permissions, temporal validity, modality/token cost, source diversity, conflict groups, and minimum complete evidence chains. The optimal \(k\) depends on the reader: a stronger long-context model may tolerate more noise, while another loses relevant evidence in the middle. Report token budget and evidence order with every result.
8. Graph and hierarchical retrieval
Graph retrieval transforms chunks into nodes and relations. With adjacency matrix \(P\), personalization vector \(e_q\), and restart probability \(1-\alpha\), Personalized PageRank solves
Query-linked seed entities spread relevance over multi-hop neighbors. Graph quality depends on extraction, canonicalization, edge direction/type, and source/version lineage. A high PageRank score is associative relevance, not a proof that a path constitutes valid reasoning.
Hierarchical indexes cluster leaf chunks and create summaries or community reports. They reduce global-synthesis context but form a lossy derived corpus. Every summary needs lineage to children; inserts, corrections, and deletions must invalidate ancestors. Compare graph/hierarchy against a text-retrieval and map-reduce baseline under equal generation budget.
9. Retrieval policy and reinforcement learning
An agentic retriever treats search as sequential decision making. At state \(s_t=(q,h_t,o_{1:t})\), action \(a_t\) can reason, rewrite, choose a source, retrieve, answer, or stop. The trajectory objective is
Answer-only rewards are sparse and allow spurious evidence, fabricated tags, or excessive search. Process rewards for relevance, information gain, redundancy, citation, and correct stopping add supervision but can themselves be gamed. Retrieved observations should be masked from the policy loss so the model is not trained to reproduce environment text as its own action.
Evaluate action accuracy, over-search, under-search, mean/tail calls, final answer, evidence chain recall, citation entailment, and policy transfer after changing corpus, retriever, or generator. Enforce hard tool, token, time, cost, and source limits outside the learned policy.
10. Calibration, abstention, and risk
Suppose a system emits confidence \(c(x)\) and answers only if \(c(x)\ge\tau\). Coverage is the fraction answered; selective risk is error among answered examples. Plot risk against coverage rather than selecting one threshold on the test set.
RAG needs at least two confidences:
- answer confidence: is the candidate answer likely correct?
- context sufficiency: can the permitted current evidence support it?
A model can know a fact parametrically while evidence is insufficient for a required citation; or evidence can be sufficient while the model is uncertain. Conflicts add source authority and temporal validity. Calibrate by slice, especially unanswerable, long-tail, dynamic, and adversarial cases.
Conformal risk control can bound an aggregate bounded loss at a chosen confidence under exchangeability or explicitly modeled shift. Such a bound is only as broad as its loss and assumptions; it does not authenticate sources or prevent prompt injection.
11. Claim-level attribution
Let generated atomic claims be \(A\), reference claims \(Y\), and citations \(C(a)\) for claim \(a\). Distinct measurements are
Completeness can be high while entailment is low; entailment can be high while the source is malicious or outdated. Also record source authority, version, valid time, exact span/page/region, and viewer permission. Automatic NLI or LLM judges need calibration against human labels and adversarial citation cases.
12. End-to-end error accounting
A useful failure tree is
The terms are not independent, so this is diagnostic rather than a literal factorization. It prevents one answer metric from hiding the causal stage. Run the generator on oracle evidence, the reranker on oracle candidates, and the retriever with exact search. These counterfactuals estimate headroom and locate the next useful investment.
The final deployment choice is a Pareto frontier across claim quality, retrieval/citation coverage, abstention, source/permission/freshness validity, security, p95 latency, memory, update time, energy, and cost per correct supported answer.
13. Query likelihood and pseudo-relevance feedback
With Dirichlet smoothing, a document language model scores
Large \(\mu\) moves short-document estimates toward the collection language model. In relevance-model feedback, top documents \(F\) define an expansion distribution
which is truncated to useful terms and interpolated with the original query. Feedback estimates corpus vocabulary but can drift when initial top documents are wrong. The correct experiment retains an original-query run and measures per-query relevant evidence gained and lost.
14. Learned sparse representation and execution cost
SPLADE-style vocabulary weight for dimension \(j\) is often
Ranking is a sparse dot product. A batch FLOPS proxy is
This penalizes vocabulary terms activated for many examples and thereby long posting lists. Query and document regularization have different consequences: query nonzeros determine how many lists are opened; document nonzeros determine index postings/bytes. The execution objective is not fully captured by vector L0/L1 alone because posting distribution, WAND upper bounds, and term correlation also affect latency.
15. Ranking and distillation losses
For scores \(s^+,s^-\), pairwise logistic loss is
and margin hinge is \(\max(0,m-s^++s^-)\). A listwise softmax with graded target distribution \(p_T\) minimizes
Margin-MSE distillation matches differences rather than absolute scale:
LambdaRank-style methods weight pair gradients by the change in the target ranking metric such as \(|\Delta\mathrm{nDCG}|\). All depend on the candidate and judgment pool; incomplete qrels can make an unjudged relevant passage a strong negative.
16. Score fusion and calibration
After calibration or normalization, linear fusion is
CombSUM uses this with unit weights; CombMNZ multiplies the sum by the number of runs that retrieved \(d\). Per-run min-max normalization
is sensitive to outliers and candidate depth. Z-score assumes a stable score distribution. Logistic/isotonic calibration estimates relevance probability on labeled data but must be refreshed after corpus/retriever changes. RRF avoids score calibration but discards score margins.
17. Product quantization and IVF
Product quantization partitions \(x\in\mathbb R^d\) into \(M\) subvectors and stores a codebook index \(k_m(x)\) for each:
Asymmetric query distance uses lookup tables:
An IVF index first assigns vectors to coarse centroid \(a(x)\). Query time selects nprobe centroids and searches only their posting lists, often with PQ codes and exact rescoring of finalists. Recall/latency/memory are controlled by centroid count, assignment multiplicity, nprobe, code size, and rerank depth.
18. HNSW and graph ANN complexity
HNSW samples a maximum layer for each point from an exponential distribution, connects approximate neighbors per layer, greedily descends sparse upper layers, then runs a bounded best-first search at layer zero. Important controls are
- \(M\): maximum neighbor degree;
efConstruction: candidate width during insertion;efSearch: candidate width during query.
Larger values generally improve empirical recall while increasing memory, build, and query work. Complexity is distribution/implementation dependent; common graph ANN methods have constructed linear-time worst cases. Report exact neighbor recall and qrel/answer effect under deployed filters rather than claiming a fixed asymptotic latency.
19. Set coverage and knapsack selection
Let information needs be \(H\), item \(i\) cover need \(j\) by \(a_{ij}\in[0,1]\), and cost \(c_i\). A budgeted coverage objective is
This makes comparison/multi-hop retrieval explicit: several high-scoring items covering the same need are worse than a complementary set. Greedy marginal gain per cost is a practical approximation when the utility is monotone submodular; authority, conflict, and minimum-chain constraints can break those assumptions.
20. Temporal ranking and bitemporal validity
For query as-of time \(t_q\), document valid interval \([v_s,v_e)\), observed time \(o\), and publication time \(p\), temporal compatibility may be
A composite score is
The stale term must be query-specific: recency is useful for current prices but wrong for historical law or evergreen definitions. Bitemporal storage retains both valid time and system/observed time so replay can reconstruct what the system could have known.
21. Cost, latency, and reliability models
Per-request cost decomposes
End-to-end latency with concurrent retrieval branches is approximately
while iterative actions add sequential latencies. Queueing makes tail latency nonlinear near saturation, so component microbenchmarks do not sum to production p95/p99.
If stage conditional success probabilities are \(p_i\), a naive diagnostic upper bound is \(\prod_i p_i\), but errors are dependent. Use counterfactual oracles—gold corpus unit, exact search, oracle candidate ranking, gold context— to estimate each ceiling rather than assuming independence.