Skip to content

    AI Architect Case Files

    Case: Legal Clause Search with Hybrid Retrieval

    A law firm's clause search missed exact clause numbers and defined terms with embeddings alone. Hybrid search, a reranker and abstention fixed it.

    By Published 7 min read

    • ai-architect
    • case-file
    • industry-legal
    • pattern-hybrid-search
    • failure-hallucination
    • kind-composite
    Share

    Composite scenario, not a real company. Assembled from common industry patterns; every number is illustrative and its assumptions are listed at the end.

    This case belongs to Data and Retrieval Architecture for AI Systems.

    Context

    • The knowledge team of a commercial law firm maintains 6,500 precedent agreements and negotiated contracts, split into clauses (about 80 per agreement): 6,500 x 80 = 520,000 clause-level passages.
    • Some questions are paraphrases: "which of our supply agreements let the buyer walk away on short notice?"
    • Others name something exact: a clause number ("clause 14.3(b) of the 2019 supply agreement") or a defined term ("Permitted Disclosure", "Change of Control").
    • A first assistant used embeddings only: retrieve the five nearest clauses, then ask a model to answer from them.

    Problem statement

    An evaluation set of 200 answerable questions (60 exact, 140 paraphrase) showed the split. A further 30 questions had no answer in the library.

    Measure Result now
    Right clause in top five, 60 exact questions 21 (35%)
    Right clause in top five, 140 paraphrases 119 (85%)
    Right clause in top five, all 200 140 (70%)
    Unanswerable questions answered anyway 22 of 30 (73.3%)

    The worst failure is the confident wrong one. Asked whether clause 14.3(b) of the 2019 supply agreement allows assignment without consent, the retriever returned an assignment clause from a different agreement, and the model answered from it.

    Goal: the right clause in the top five for at least 90% of both groups, and at most 3 of the 30 unanswerable questions answered.

    Constraints

    • Fixed: every answer quotes the clause and names agreement, clause number and version.
    • Fixed: a lawyer is accountable for the conclusion; the tool finds and reads clauses.
    • Fixed: confidential agreements stay inside the firm's approved environment.
    • Negotiable: a few seconds of latency, and the number of candidates reranked.
    • Limit: the team has no search specialist; the lexical component must come from an existing engine.

    Why the traditional approach fell short

    Approach Strength Why it fell short
    Boolean keyword search Finds "14.3(b)" and "Change of Control" exactly Misses "terminate for convenience" when the text says "exit on notice"
    Embedding-only retrieval Matches meaning Treats a clause number as noise: "14.3(b)" is near "14.3(c)" and near "12.1" of another agreement
    Hand-tagged clause libraries Covers anticipated types Nothing outside those types

    Options considered

    Option Why accepted or rejected
    A. Keyword and boolean only, no AI: add clause-number and defined-term fields to the existing search Kept as one half of the final design and as the fallback; exact, but blind to paraphrase
    B. Embeddings only (the first assistant) Rejected: misses exact terms and does not know when it is wrong
    C. Hybrid lexical plus vector, rank fusion, reranker of the top 20, abstention Accepted. Keep the reranker only if the evaluation shows it lifts the 90% target: it adds a model pass per candidate

    Solution architecture

    Figure 1. Hybrid retrieval components

    Hybrid retrieval for clause searchOrchestrator to Retriever; Retriever to Document store; Retriever to Vector index; Document store to Rank fusion; Vector index to Rank fusion; Rank fusion to Reranker; Reranker to Orchestrator; Orchestrator to Model gateway (strong evidence); Orchestrator to No answer found (weak evidence)OrchestratorOrchestratorRetrieverRetrieverDocument store: lexical indexDocument storelexical indexVector indexVector indexRank fusionRank fusionReranker: top 20Rerankertop 20Model gatewayModel gatewayNo answer foundNo answerfoundstrong evidenceweak evidence

    How to read this: two searches feed one merged list, a reranker orders it, and the orchestrator decides between answering and abstaining.

    Figure 2. One question with the abstain path

    sequenceDiagram accTitle: Runtime path for a clause question accDescr: The orchestrator asks the retriever, which runs lexical and vector searches in parallel and fuses the results. The reranker scores the top candidates. If the best score is below the threshold or the named clause is absent, the answer is no match found. Otherwise the model gateway drafts an answer and a check verifies each quote against the passage. autonumber participant O as Orchestrator participant R as Retriever participant L as Document store participant V as Vector index participant X as Reranker participant M as Model gateway O->>R: Question par Lexical search R->>L: Terms and clause number L-->>R: Ranked list 1 and Vector search R->>V: Question embedding V-->>R: Ranked list 2 end Note over R: Fuse the two lists by rank R->>X: Top 20 fused passages X-->>O: Passages with scores alt Best score below threshold or named clause absent O-->>O: Abstain Note over O: Reply no matching clause found, offer keyword search else Strong evidence O->>M: Draft answer quoting the passages M-->>O: Answer with quotes Note over O: Every quote must appear verbatim in its passage end

    How to read this: the two searches run side by side, and two checks, the score threshold and the quote check, stand between the retriever and the lawyer.

    How AI solved it, step by step

    Step through the clause 14.3(b) question, then switch to "Without the fix".

    One clause question (interactive when JavaScript is on)

    As designed

    1. Ask. An associate asks whether clause 14.3(b) of the 2019 supply agreement allows assignment without consent.
    2. Extract exact items. The Orchestrator pulls out the clause number "14.3(b)" and the agreement, and passes them with the question to the Retriever.
    3. Search both ways. Lexical search looks for the clause number and terms; vector search looks for the meaning. Both always run, because neither covers both question types.
    4. Fuse by rank. The two ranked lists merge by rank, which avoids comparing a lexical score with a similarity score.
    5. Rerank. The Reranker reads the question and each of the top 20 passages together and reorders them.
    6. Check the evidence. If the best score is under the threshold, or the named clause is missing, the reply is "no matching clause found" plus an offer of keyword search.
    7. Draft. Otherwise the Model gateway drafts a short answer that quotes the clause.
    8. Verify the quotes. Every quoted span must appear verbatim in a retrieved passage, or the answer is withheld. The associate sees agreement, clause number, version and the quote.

    Without the fix

    1. Embed and retrieve. The first assistant embeds the question and takes the five nearest clauses.
    2. Wrong agreement. The nearest assignment clause is from a different agreement: similar in meaning, not the one asked about.
    3. The model answers anyway. Nothing checks the evidence, so the model answers from it. 22 of 30 unanswerable questions also got an answer.

    Under the hood

    BM25 scores a passage by how often query terms appear in it, how rare each term is across the library, and the passage's length against the average. Elasticsearch's similarity documentation gives the defaults:

    Parameter Default Controls
    k1 1.2 How quickly repeated occurrences stop adding score
    b 0.75 How strongly length matters

    Anthropic's contextual retrieval post says BM25 is "particularly effective for queries that include unique identifiers or technical terms", with the example of the error code "TS-999". A clause number and a defined term behave like those identifiers. Test whether "14.3(b)" survives your analyser as one token, or index clause numbers in a separate exact-match field.

    Fusion uses reciprocal rank fusion. Cormack, Clarke and Büttcher define RRFscore(d) as the sum over rankings of 1 / (k + r(d)), "where k = 60 was fixed during a pilot investigation". Worked example: clause C (the one asked about) is 7th in the vector list and 1st in the lexical list; A is 1st and 2nd; B is 2nd in the vector list only. D to G are invented placeholders filling the vector list.

    Fusing the vector and lexical lists (k = 60) (interactive when JavaScript is on)
    1. Ascore 0.03252 (ranks 1, 2)
    2. Cscore 0.03132 (ranks 7, 1)
    3. Bscore 0.01613 (ranks 2, -)
    4. Dscore 0.01587 (ranks 3, -)
    5. Escore 0.01563 (ranks 4, -)
    6. Fscore 0.01538 (ranks 5, -)

    Score(A) = 1/61 + 1/62 = 0.0325; score(C) = 1/67 + 1/61 = 0.0313; score(B) = 1/62 = 0.0161. Fused order: A, C, B, then D to G. C moved from 7th to 2nd, and the reranker can promote it to 1st. Anthropic publishes that on its own datasets a reranked, contextual-embedding-plus-BM25 pipeline cut the top-20 retrieval failure rate by 67% (5.7% to 1.9%); treat that as a pointer to test. Details: Under the hood: BM25 and hybrid search.

    What went wrong / failure modes

    Failure Mitigation
    Tokenisation splits the clause number: "14.3(b)" becomes fragments that match many clauses Test it on the evaluation set; index clause numbers as exact fields
    Defined terms in lowercase: the same words in their ordinary sense match too Boost capitalised defined terms and check them against the agreement's definitions clause
    Right clause, wrong version: a superseded amendment ranks first Index the operative version and show its date
    Threshold set too high: the system abstains on answerable questions Set it on the evaluation set, track the abstain rate, and always offer keyword search

    Results

    These are illustrative targets, not measurements.

    Measure Now Target
    Exact questions, right clause in top five 21 of 60 (35%) 54 of 60 (90%)
    Paraphrases 119 of 140 (85%) 126 of 140 (90%)
    All 200 answerable 140 (70%) 180 (90%)
    Unanswerable questions answered 22 of 30 (73.3%) at most 3 (10%)
    Cost none A second search per query, plus 20 reranker passes per question

    Tradeoffs and when NOT to use this approach

    • Two indexes must be kept in sync. If questions are almost all paraphrases, vectors alone may be enough.
    • The reranker adds latency and cost. Skip it if the fused list already meets the target.

    Design rules extracted

    1. Exact identifiers need lexical search; paraphrase needs vectors; legal text has both, so run both.
    2. Merge by rank, not by raw score, because the two scores are not comparable.
    3. Define when the system must say "no matching clause" and test it with questions that have no answer.
    4. Verify that every quote appears verbatim in the passage it cites.
    5. Keep the evaluation set split by question type so one average does not hide a weak group.

    Assumptions

    All numbers are illustrative.

    Last verified: 2026-10-22

    Ask about this article

    Get answers grounded in this post. AI-generated — based on this article, and may be imperfect.

    Scaled AI Weekly

    Enjoyed this? Get more like it every Monday.

    Real architecture decisions, LLMOps patterns that survive production, and engineering leadership advice — from 12+ years of building at enterprise scale. Free. No spam. Unsubscribe anytime.

    Get new articles by email

    Free. We email a confirmation link first, and you can unsubscribe from any email.

    Join engineers building production AI systems

    Comments