Skip to content
Ali Hassan, home

Blog6 min read

How I built a chat-with-PDF app that cites its pages

A chat with PDF app that cites the PDF and page for every answer: ingestion, chunking, per-user vectors and streaming. Read how each part was built.

By Ali Hassan, Senior engineer for SaaS and AI products

A chat with PDF app only earns trust if you can check its answers. The one I built, AI PDF Chat, cites the PDF and page behind every answer, and the citation opens the document with the passage highlighted. Here is how each part works: the ingestion queue, chunking and embedding with Gemini, per-user vectors in Qdrant, and answers streamed over SSE.

What is a chat-with-PDF app, really?

A chat with PDF app is retrieval over your own documents: you upload PDFs, the text is split and embedded, and a question is answered from the chunks that match it. The useful version cites its sources. Every answer points to the PDF and page it came from, so a reader can open the passage and confirm it, instead of trusting the model's word. That makes it a PDF RAG system with receipts.

The pattern is retrieval-augmented generation, named in a 2020 paper that fed matching text to the model. I built this one on LangChain in TypeScript, with an Express worker behind a Next.js app. LangChain gives one interface for chat models and embeddings across providers, so the vendor can change without touching the pipeline. The finished app is the AI PDF Chat case study.

How does the ingestion queue handle big PDFs and rate limits?

The upload responds at once, and the real work happens in the background. A worker takes one file at a time, extracts its text, splits it into chunks and embeds them, moving the file through four states you can watch in the app: queued, processing, ready, failed. One file failing never holds up the rest. If the worker restarts mid-job, files that were still in flight are put back on the queue, so nothing gets stuck.

Embedding inside the upload request would time out on a large PDF, so it cannot live there. The free tier of the Gemini embeddings API also rate-limits hard, and through the library returns empty vectors instead of an error when it does. I treat an empty or short vector as a hidden rate-limit response, retry it with exponential backoff, and pace the batches to stay under the limit in the first place; Google publishes the per-model caps on its Gemini rate limits page. If it never recovers, the user sees a plain message about the limit, not a half-filled index. Pacing the batches up front means most uploads never hit the limit at all.

How are the PDFs chunked and embedded?

Chunking splits each PDF into passages small enough to retrieve precisely but large enough to carry meaning, and each chunk is turned into a vector by an embedding model. Gemini embeddings are the default here. The model accepts up to 8,192 tokens of text per request and lets you pick the output size, from 128 up to 3,072 dimensions, so you trade index size against recall.

Those limits come straight from the Gemini embeddings docs, which use Matryoshka representation learning, a 2022 technique, so a truncated vector still works. Each re-ingest clears the file's old vectors first, so a retry never leaves duplicates behind, and a scanned PDF with no extractable text is marked failed with a useful message rather than shown as ready. The retrieval shape underneath is the one I lay out in RAG architecture.

How does Qdrant keep each user's PDFs separate?

Many users share one Qdrant collection, so isolation has to hold on every query. Each vector carries the user it belongs to, and every search filters by the signed-in user on an indexed field, with a relevance threshold so weak matches never reach the model. In single-document mode it also filters by the one file in scope. One person's PDFs can never surface in another person's results.

Qdrant's own multitenancy guidance is to keep every tenant in one collection and filter by a payload field that names them, backed by a keyword payload index, a feature it dates to version 1.11.0. The filter itself is a standard Qdrant payload filter. Deleting a PDF removes the file, its record and its vectors together, so deleted content stops surfacing, and viewing, deleting and retrying a file are all scoped to its owner. I compare Qdrant-style stores with Postgres in pgvector vs Pinecone.

How does every answer cite its PDF and page?

Every answer is restricted to the retrieved chunks, and each citation is grouped per file and page and carries the best-matching passage. The viewer highlights that passage when you open the citation, so you land on the exact lines, not just the right document. Recent turns are kept too, so a follow-up question still makes sense. In a chat with PDF app, the citation is the product.

This is grounding in the sense the 2020 retrieval-augmented generation paper meant: the model answers from fetched passages, not from memory, which is what lets each claim carry a page number. If the chunks hold no good answer, the honest move is to say so rather than guess at one. How well that grounding holds is exactly what RAG evaluation measures: whether the cited pages really support the answer. A citation you can open and read is the difference between a demo people distrust and a tool they actually use.

How are answers streamed to the browser?

Answers stream in as they are written, over server-sent events. The browser opens one EventSource connection and the server pushes text as a run of small events, so the first words appear fast and the reader is not left staring at a spinner. Because the stream is one-way, server to client, it fits a chat answer better than a two-way socket.

MDN's page on server-sent events sets out the format: the server replies with the text/event-stream MIME type and sends each notification as a block of text ended by a blank line. The format itself is defined in the HTML standard's server-sent events section. SSE is lighter to run than a WebSocket for traffic that only flows one way, which is the usual shape of a chat reply.

What does it cost, and when should you get help?

Most of the spend in a chat with PDF app is the generation step, not retrieval. Embedding a page is cheap: as of 30 November 2026, Gemini embeddings accept up to 8,192 tokens per request, and a page of text sits well under that, so ingestion costs a fraction of answering. The generation model, a hosted Llama through Groq here, is swappable, so you can shop that cost without rebuilding the pipeline.

You can put your own token counts into the LLM cost calculator to see where the money goes before you build anything. If you want to build a chat with PDF app that cites its pages, or you have a RAG feature that answers confidently and wrongly, that is the work I take on. I build retrieval over your own documents and keep every answer checkable against its source. You can see the shape of it on RAG development.

From the work

  • PDF chat with document scope selector, landing page and upload queue of a RAG assistant that cites its pages.

    AI PDF Chat

    Upload several PDFs and ask questions across all of them or one; every answer cites the PDF and page, and the citation opens the passage highlighted.

    • Next.js
    • Express
    • LangChain
  • AI reply grounded in cited help-centre content, shadow-mode draft metrics per channel, and metered AI credits in billing.

    Evoriqa

    My own SaaS: a multi-tenant AI customer-support platform, live in production, built by the person who also pays its inference bill.

    Live in production

    • Next.js
    • TypeScript
    • PostgreSQL

The offer this article leads to: RAG development, typically $4k-$10k.

Tell me what you’re building and where it’s stuck.

I’ll tell you the cleanest path forward, including if it’s “don’t build that.”

Email only, on purpose: it reaches me, not a sales team.

Or write tocontact@alihassan.dev

Ali Hassan in a dark winter jacket, looking off to one side, standing in a stone courtyard with a minaret and cloudy sky behind him.