Work · Case file 05 · Local-first prototype · 2026

AgenticPDF

4 min readJump to proofJump to limits

At a glance

Result
1,007 SuperStore invoices and 3 reference papers indexed into a fully local two-stage retrieval workbench, with on-device embeddings and reranking and no API key needed in offline mode.
Problem
Chat-with-a-PDF tools hide how they reached an answer, and I wanted all of it to run privately on one machine.
My role
Solo build: the ingestion pipeline, the retrieval stack, the agent layer and the Next.js workbench.No team is listed in the README and the MIT license is in my name.
Stack
Next.js 15 · transformers.js (ONNX) · better-sqlite3 · BM25 with reciprocal-rank fusion

Proof

What try, see and check mean

On this pageThe problem

The problem

Chat-with-a-PDF tools hide how they reached an answer. For a corpus of invoices and papers I wanted to see how the agent routes, searches, reranks, aggregates and cites its work, and I wanted all of it to run privately on one machine with no API key.

The idea. Do the expensive work once at write time (extract, derive Markdown, chunk, summarise, hash, embed) so every read is a cheap, inspectable two-stage retrieval over local artifacts.

Fig. 01 · Two-stage retrieval, all on one machine

Ingest, once

  1. 1,007 invoices + 3 papers
  2. Parse to Markdownpdfjs, SHA-256 content addressing
  3. Chunk and embedbge-small-en-v1.5 on ONNX, local Float32 vectors
  4. SQLite + vector store

Answer, per question

  1. Question
  2. Summary routingwhich documents could answer it
  3. Hybrid searchdense + BM25, reciprocal-rank fusion
  4. Within-document chunksper-document candidate coverage
  5. Cross-encoder rerankms-marco MiniLM on ONNX
  6. Answer with visible traceoptional Claude tool use; offline needs no key
A query enters at the left. Stage one routes by document summaries. Stage two fuses dense (bge-small-en-v1.5) and BM25 scores with reciprocal-rank fusion, searches chunks within the routed documents, and reranks with the ms-marco-MiniLM cross-encoder. The scores from each step are what the Retrieve view shows.

How retrieval scores on a labelled set

AgenticPDF's own retrieve(), unchanged, run on CPU with no API key over two public-domain NIST PDFs (538 chunks). 16 questions, each labelled with one answer phrase before the run; a returned chunk is relevant when it contains that phrase. Recall at k is the share of questions with a relevant chunk in the first k after reranking.

Recall at k and mean reciprocal rank of AgenticPDF retrieval on 16 labelled questions
MeasureQuestionsShare
Recall at 19 of 1656.25%
Recall at 310 of 1662.5%
Recall at 510 of 1662.5%
Recall at 8 (all returned)11 of 1668.75%
Mean reciprocal rank160.603

Five questions miss. For three (the carbon estimate, indirect prompt injection and automation bias, all in AI 600-1) the answer sentence reached no chunk intact: the PDF text extraction dropped parts of its lines. The chunks holding the surviving fragments still come back at rank 1 or 2, so these are parsing failures more than ranking failures, but the strict label counts them as misses. For one, the chunk with the answer is not among the 8 returned, though the chunk after it, also on risk tolerance, is at rank 2. The last answer sits in a chunk garbled the same way.

16 questions is a small set: each question is 1 / 16 = 0.0625 of it. Treat it as a smoke test of the pipeline, not a benchmark.

Source: public/demos/agenticpdf/retrieval-traces.json and questions.json, recorded 11 Nov 2026 by scripts/agenticpdf-retrieval/run.ts at AgenticPDF commit 7e07ba4

Replay the retrieval traces

Each of the 16 questions was run through the repository's own retrieve() on CPU, with no API key. Pick a question to see the eight chunks it returned, every score from each stage, and which chunk holds the answer. These are recordings, not a live search.

Or read the raw file: retrieval-traces.json.

Corpus: NIST AI 100-1 (AI RMF 1.0) and NIST AI 600-1 (the generative AI profile), U.S. government works in the public domain. Dense is cosine similarity from bge-small-en-v1.5; n/a means the chunk came from BM25 only. Rerank is the ms-marco-MiniLM cross-encoder's sigmoid score and sets the order. The questions and their labelled answers are in questions.json; the traces were recorded at AgenticPDF commit 7e07ba4 by scripts/agenticpdf-retrieval/run.ts in this site's source.

How it works

  1. Read

    pdfjs extracts text from born-digital PDFs with line reconstruction, column detection and text-layer checks. Scanned PDFs are marked ocr-pending rather than guessed at.

  2. Derive

    Each document becomes a clean Markdown working copy with structure-aware chunks, contextual chunk prefixes, a document summary and, for invoices, extracted metadata.

  3. Store

    Raw PDFs and Markdown are content-addressed by SHA-256, so duplicate uploads are caught by hash. SQLite (corpus.db) holds metadata, chunks, jobs, runs and feedback, and vectors are stored locally as Float32.

  4. Retrieve

    Stage one routes the query to candidate documents by their summaries. Stage two fuses dense (bge-small-en-v1.5) and BM25 scores with reciprocal-rank fusion, searches chunks within the routed documents with per-document candidate coverage, and reranks with the ms-marco-MiniLM cross-encoder.

  5. Answer

    Offline mode gives extractive Q&A with exact invoice aggregates. Optionally, Claude tool use runs with two tools, search_corpus and aggregate_invoices.

  6. Trust

    Every run is saved with a structured trace, evidence snippets, warnings, feedback, source and job health, and the dense, sparse, fused and rerank scores are visible in the Retrieve view.

What was hard

Keeping retrieval inspectable without making it slow. At roughly 1k documents and 4k vectors I chose a dependency-free SQLite vector store with brute-force cosine search instead of a vector database, because at this scale it is fast enough and every score can be shown in the UI. The cost is that the vector cache must be invalidated on every write or delete so reindex and delete never serve stale results, and the choice is only justified at this scale.

Limits. OCR and VLM parsing are not implemented, so scanned PDFs sit as ocr-pending. Page-level bounding boxes, table-cell provenance, authentication, tenants and billing are deferred, and Matrix mode is a local demo over existing metadata rather than a workflow engine. The retrieval measurement is small: 16 questions I wrote and labelled myself, one gold phrase each, over two documents, so stage-one routing always keeps both and only the chunk search and reranker are tested. It is not run on the invoice corpus. On the layout of NIST AI 600-1 the text extraction drops parts of lines, which is where 3 of the misses come from.

Evidence

Corpus
1,007 SuperStore invoices plus 3 reference papersAgenticPDF README
Index scale
Roughly 1k documents and 4k vectorsAgenticPDF README, Architecture
Embedding model
bge-small-en-v1.5, on device via transformers.js / ONNXAgenticPDF README
Reranker
ms-marco-MiniLM cross-encoder, on deviceAgenticPDF README
Vector store
Dependency-free SQLite with in-memory brute-force cosine search; cache invalidates on vector writes and deletesAgenticPDF README, Architecture
Tests
Node tests cover metadata filter extraction, analytical query detection, chunking and context prefixes, reciprocal-rank fusion, and vector cache invalidationAgenticPDF README, Verification
Workbench
Seven views: Overview, Ask, Retrieve, Corpus, Document, Workbench, MatrixAgenticPDF README, Interface
Retrieval on a labelled set
16 questions over NIST AI 100-1 and AI 600-1 (538 chunks): answer passage at rank 1 for 9, in the top 3 for 10, top 5 for 10, top 8 for 11; MRR 0.60. In 3 of the 5 misses the answer sentence is in no chunk at all.public/demos/agenticpdf/retrieval-traces.json, recorded at AgenticPDF commit 7e07ba4 by scripts/agenticpdf-retrieval/run.ts
Repository
4 commits, MIT license, no releases; created June 2026github.com/ZulfaqarHafez/AgenticPDF (metadata and LICENSE)

Stack

  • Next.js 15
  • React 19
  • TypeScript
  • pdfjs-dist
  • @huggingface/transformers (ONNX)
  • bge-small-en-v1.5
  • ms-marco-MiniLM
  • better-sqlite3
  • BM25 with reciprocal-rank fusion
  • Anthropic SDK (optional Claude tool use)