Yash Gupta

A contract reviewer that cannot quote what is not there

Upload a contract and get it checked against eight things that matter, each finding quoting the clause and citing its page. The hard part is not the checking. It is making a language model incapable of showing you a sentence the contract does not contain.

Role
Designed and built it, solo
Stack
Next.js 15, TypeScript, Postgres with pgvector, Bull, Clerk, Docker
Scored
16/16 items, 13/13 with a verified quote
Tests
52, including 8 against a real Postgres
Code
github.com/ygupta9132/Clause-Contract-Review

The problem with asking a model about a contract

Ask a model whether a contract auto-renews and it will usually tell you something plausible. Plausible is the failure mode. A reviewer who invents a termination clause is worse than no reviewer, because the invented one reads exactly like the real ones and nobody goes back to check.

So the design rule is that the model is never the last word. It proposes an answer and a quote; the server then decides whether that quote is real.

The guard

Every finding carries a quote, and before anything reaches the screen the server searches for that quote in the extracted contract text. If it is not there, the finding is downgraded to unclear and the quote field is emptied, so invented wording is never rendered as a quotation.

The comparison cannot be byte-exact, because an honest quote still differs from its source: PDF extraction flattens line breaks, models swap curly quotes for straight ones and single for double, and long quotes come back with an ellipsis in the middle. So the rule is narrow and stated in one line: normalisation touches whitespace, quotation marks, dashes and case, and nothing else. Different words always fail.

Proposed quoteVerdict
thirty (30) days' prior written noticeaccepted, apostrophe variant
1.1 'Services' means the data processingaccepted, single rather than double quotes
Either party can end this agreement with 30 days noticerejected, a paraphrase
Either party must terminate this Agreementrejected, one word changed

The detail that makes it airtight

Finding the quote is only half of it. What gets stored and displayed is not the model's string at all: the matcher returns character offsets into the original document, and the application slices the quote out of the contract itself. The page number comes from those same offsets rather than from anything the model said.

That turns a check into a guarantee. It is not that invented wording is detected and rejected; it is that invented wording has no path to the screen, because the model's text is never what gets rendered.

Why the chunking is not a sliding window

Every tutorial splits documents every thousand characters with some overlap. On contracts that fails in a way nothing downstream can repair: it cuts "either party may terminate" away from "on thirty days written notice", and once those sit in separate chunks, no retrieval strategy puts the clause back together.

So the splits come from the document's own structure, with windowing only as a fallback inside an over-long section. That has a second payoff: the section label falls out for free, and the brief required every finding to cite a section or a page.

Two things only real documents teach you. Contracts number clauses two different ways, 7. TERMINATION on its own line versus 7.1 Either party may... running straight into prose, so trailing text is kept only when it reads like a title. And a bare integer is only a clause number when a separator follows it, otherwise "22 Finsbury Square, London" becomes Section 22. Which it did.

A search query per checklist item

This is the single change that moved accuracy most. Embedding the phrase "limitation of liability" does not find a contract that says "neither party shall be liable for indirect damages". So each of the eight items carries its own query, written to read like the clause rather than like its label, plus a keyword list. Two searches run per item, semantic through pgvector and lexical through the database, merged in favour of meaning.

The chat borrows the same vocabulary. Before it did, "who are the parties to this agreement?" answered "the contract does not cover this" on a contract whose preamble lists them by name but never uses the word "parties". It says "by and between".

What verification does not prove

Worth being precise, because it is the question I would ask. Verification proves the quote exists. It does not prove the quote is relevant to the item, that the status is right, or that the summary describes it accurately. A model could quote a real sentence from the payment clause and label it as termination, and it would pass.

It is also completely blind to a false "not found", because there is no quote to check. Those are caught somewhere else: a scoring harness that runs the real pipeline against known-correct results, currently 16 of 16 items with 13 of 13 found items carrying a verified quote. It started at 13 of 16, and every point after came from reading an actual failure rather than guessing.

The rest of the shape

Uploads return immediately and the work happens on a Bull worker, because a 20 MB PDF is tens of seconds of parsing and minutes of embedding against a rate-limited endpoint, and a restart should not lose it. Every table carries the owner's id, including the chunks, so the vector search holds its own tenant check rather than inheriting one through a join. Asking for someone else's contract returns 404 rather than 403, because a 403 confirms the id exists.

What the tests cover

52 tests across six files. Most are pure: the quote matcher gets the largest share, because it is the component the whole guarantee rests on, and its cases are the awkward ones rather than the easy ones, including the apostrophe and quote-mark variants above, the ellipsis in a long quote, and the paraphrase that must fail. The rest cover clause splitting, citation handling and summarisation.

Eight run against a real Postgres rather than a mock, and those are the tenant-isolation ones. A mocked database would have tested the mock: the thing worth proving is that a query written against the real schema cannot return another user's rows, which only the real schema can answer. The whole suite runs in about seven seconds, which is what keeps it worth running.

What I took from it

  • Grounding is an architecture decision, not a prompt. Asking a model not to invent things helps; making the invented version structurally unable to reach the user is what you can actually promise.
  • Build the scoring harness early. Without it, every change to a retrieval pipeline is guesswork delivered confidently.
  • Test against the specification rather than the happy path. Doing that found a body-size limit quietly breaking the 20 MB requirement and a secrets file being baked into the Docker image, neither of which was visible from reading the code.