# How do I chat with my documents?

> The app finds the passages that match your question and pastes them into the prompt. You need a chat model, an embedding model and an app that does the plumbing.

The model never reads your whole folder. The app searches your files for the few passages that look relevant and pastes them into the prompt next to your question. This is called retrieval-augmented generation, or RAG.

## How RAG works in five steps

The first two steps happen once, when you add files. The last three happen on every question.

1. Split files into chunks
2. Embed each chunk
3. Embed the question
4. Find the closest chunks
5. Answer from those chunks

1. The app cuts each document into chunks of a few hundred words.
2. An embedding model turns each chunk into a list of numbers that captures its meaning. The app stores these in a small database.
3. When you ask something, the same embedding model turns your question into numbers.
4. The app finds the chunks whose numbers sit closest to the question's. Usually it keeps the top few.
5. Those chunks go into the prompt with an instruction to answer from them. The chat model writes the answer and, in good apps, cites the chunks.

Step 4 is where most answers are won or lost. The chat model can only use what the search hands it.

## What you need

Three parts, all of which run locally.

- A chat model. Any model that follows instructions well. A [mid-class](https://localagi.co/model-classes) model is noticeably better at sticking to the sources than a small one.
- An embedding model. These are tiny, usually well under 1 GB, and run on any machine. A common local choice is nomic-embed-text.
- An app that does the chunking, storage and search. AnythingLLM, Open WebUI, LM Studio and Jan all do some version of this.

The embedding model must stay the same once your files are indexed. Switch it and the app has to re-embed everything, because numbers from two different embedding models do not compare.

## Setup in one app

AnythingLLM is built for this, so it is the shortest path. It bundles a runtime and a default embedding model, and keeps documents in separate workspaces.

## Step 1: Install and pick a chat model

Download AnythingLLM from [anythingllm.com](https://anythingllm.com) and open it. During setup, choose its built-in provider and a model that fits your memory, or point it at Ollama or LM Studio if you already run one. [Which model should I use for my task?](https://localagi.co/choosing-a-model) helps you pick.

## Step 2: Make a workspace

Create a workspace for one topic, such as "Tax 2026" or "Product docs". Keep unrelated files in separate workspaces. Search works better over a focused pile.

## Step 3: Add documents

Open the workspace's upload panel, drop in your PDFs, Word files or text, move them into the workspace and save. This is when the embedding runs. A few hundred pages takes a minute or two on a laptop.

## Step 4: Ask, then check the citations

Ask a question that a specific passage answers. Expand the citations under the reply and read the chunks it used. If the right chunk is missing, the problem is retrieval, not the model.

**The catch.** 
Scanned PDFs are images, not text. If a file was scanned without text recognition, the app embeds nothing and the model answers from thin air. Open the PDF and try to select a sentence, and if you cannot, run it through OCR first.

## Why answers go wrong

### Chunking

Chunks that are too small lose context. A chunk that says "the limit is 40 days" is useless if the heading saying which limit was in the chunk before. Chunks that are too large dilute the match and eat the context window.

Tables and slide decks chunk badly in most apps. If those matter, export them to plain text with clear headings before you upload.

### Retrieval misses

Embedding search matches meaning, not exact words. It is good at "how do I get a refund" finding a passage about returns. It is bad at part numbers, names and codes, where the exact string is what matters.

Some apps add keyword search or a [reranker](https://localagi.co/glossary) to fix this. If yours offers either, turn it on. Otherwise, rephrase the question using the words the document uses.

### Context too small

The retrieved chunks, your question and the chat history all share one [How much can the model keep in mind?](https://localagi.co/context-window). If the runtime is set to a small context, the app silently drops chunks or older messages to make room.

**Rule of thumb.** 
If answers ignore documents you know are there, raise the context to at least 16K before changing anything else.

## When to skip RAG

If the document fits in the context window, paste the whole thing. The model then sees every word, including the parts a search would have missed.

At 4-bit, a mid-class model on 24 GB can usually hold about 32K tokens of context, which is roughly 50 pages of plain text. A 128 GB machine can hold far more. Most apps let you attach a file directly to one chat, and that is usually better than a workspace for a single contract or paper.

Use RAG when the material is bigger than your context, or when you ask many questions over the same large pile.

**Try it.** 
Attach one long document to a plain chat and ask this. Then ask the same question in a RAG workspace and compare.

```text
Quote the exact sentence that answers this, then explain it in one line: what is the notice period for cancelling?
```

## Go deeper

### What an embedding is

An embedding model reads a chunk and outputs a fixed-length list of numbers, often 768 or 1024 of them. Texts with similar meaning end up with similar lists. The search measures the angle between the question's list and each chunk's list, and the smallest angles win.

That is why the embedding model matters more than the chat model for retrieval quality. A better chat model cannot fix a search that returned the wrong passages.

### Rerankers

A reranker is a second, slower model that reads the question and each candidate chunk together and scores how well they match. Apps fetch twenty or so chunks with embeddings, then rerank them and keep the best few. It costs a little time per question and fixes many near misses.

### Long context is not free

Pasting a 100-page document works, but every token has to be processed before the first word of the answer. On a laptop that [prefill](https://localagi.co/life-of-a-prompt) can take a minute or more. RAG keeps prompts short, which keeps answers fast. [What makes a model fast?](https://localagi.co/what-makes-it-fast) explains why prefill and writing speed differ.
