I built a retrieval-augmented generation (RAG) Q&A bot over my own build notes, in plain Python with no framework, so I could see every step. The part that taught me the most was measuring retrieval on its own. The fix that took the hit rate from 87% to 100% wasn’t a better model. It was keeping more chunks.
Why measure retrieval separately
A RAG answer can only be as good as the chunks it’s given. When retrieval misses, the model says it doesn’t know, or worse, guesses. From the outside that looks exactly like a model problem. My first test showed it: asked for the root cause of a monitoring issue, the model correctly said the notes didn’t give one, because the chunk that did wasn’t among the ones it received. The model did the right thing. Retrieval failed.
There’s a security side too. Everything retrieved goes into the prompt, and everything in the prompt leaves the machine for the model API. So retrieval decides both answer quality and what data gets sent out.
The pipeline
It runs on a small Ubuntu VM with no cloud credentials on it, only an API key for the model.
| Step | What it does |
|---|---|
| Ingest | Split the notes into chunks by heading, label each with its section, and block any chunk that contains account IDs, GUIDs, keys or personal emails |
| Embed | Turn each chunk into a 384-number vector with all-MiniLM-L6-v2 and store it in a local Chroma database |
| Retrieve | Pull the 20 closest chunks to the question |
| Re-rank | A cross-encoder (ms-marco-MiniLM-L-6-v2) reads the question and each chunk together and keeps the best ones |
| Answer | Send those chunks and the question to Claude, which answers with citations |
| Evaluate | 15 questions with a known answer phrase each, scored as hit rate |
Retrieving 20 and then re-ranking is the standard retrieve-and-re-rank pattern [1]. Vector search is fast because each chunk’s vector is computed once and stored, but it only judges “same topic”. The cross-encoder is much better at “does this answer the question”, but it has to run once per chunk for every question, so it only sees the top 20. It’s the same shape as a SOC: detection rules narrow millions of events down to a few alerts, and an analyst reads only those.
Measure the chunk size instead of guessing
The embedding model truncates input longer than 256 word pieces by default [2], so anything past that limit is silently ignored. I’d set an 800-character chunk limit assuming about 4 characters per token. Counting real tokens with the model’s own tokenizer showed 7 chunks over the limit. My notes run closer to 2.8 characters per token because they’re full of commands, paths and IDs. Dropping the limit to 650 characters gave 164 chunks, the longest at 233 tokens, none over.
A crash that wasn’t a code bug
The embedding script worked once, then died with Illegal instruction (core dumped). That error means a program used a CPU instruction the processor says it doesn’t have. Proxmox’s default virtual CPU type hides newer instructions like AVX2, which PyTorch uses. This shows which flags the VM can see:
grep -o -w -E 'avx|avx2|fma' /proc/cpuinfo | sort -u
Setting the VM’s CPU type to host gives the guest exactly the host’s CPU flags [3]. Its downside is live migration to a host with a different CPU, which doesn’t apply on a single node.
The evaluation
Each question has a phrase that must appear in the right chunk. The script checks whether that chunk lands in the kept set (the hit rate) and in the top 20, and saves every run to a CSV. Comparing the two numbers tells you where a miss happened:
| In top 20? | In kept chunks? | Meaning | Where to fix it |
|---|---|---|---|
| Yes | Yes | Works | Nothing |
| Yes | No | Found, then cut by the re-ranker | Keep more chunks, or try another re-ranker |
| No | No | Never found | Chunking, headings or the embedding model |
My first run said 74%. Three of those misses were bugs in my test, not in the search: two expected phrases appeared nowhere in the notes, and one was too loose. An eval set can be wrong too, so check the test before blaming retrieval. After fixing the test, 15 questions:
| Run | Chunks kept | Hit rate with re-ranker | Without re-ranker | Found in top 20 |
|---|---|---|---|---|
| Baseline | 5 | 13/15 = 87% | 13/15 = 87% | 15/15 |
| Top 8 | 8 | 15/15 = 100% | 14/15 = 93% | 15/15 |
Vector search found every answer somewhere in its top 20, so chunking was fine. The problem was how many chunks I kept. At 5, the re-ranker added nothing: it fixed one question and broke another. At 8, it earns its place, pulling the monitoring root cause from 13th to 6th, which vector search alone can’t do.
Keeping 8 isn’t free. It’s about 60% more text in every prompt, which means more tokens and more room for a planted chunk. And 15 questions is a small set. Still, the lesson holds: measure where the miss happens before changing anything.
One limit RAG can’t fix by tuning
“How many VMs do I have?” can’t work as a retrieval question. The VMs are spread across six sections and the model only sees the kept chunks, so it can’t count what it never sees. RAG is good at “find the fact” and bad at “count” or “list all”. Those questions need a summary document or a tool that reads the whole source.
The security risks, phase by phase
- Sensitive data in the index. My first copy of the notes had real account IDs and personal emails in it. I caught that before indexing and swapped in a cleaned copy with placeholders. The ingest step also blocks chunks matching those patterns.
- The API key. It lives in a
.envfile only my user can read (chmod 600), written with an editor so it stays out of shell history, and ignored by git. The key is scoped to a single workspace, with a spend limit set before the first call. - Indirect prompt injection. My question is trusted. The chunks come from a file. A planted line like “ignore your instructions” would arrive inside the prompt. OWASP describes exactly this: injection through input from external sources such as websites or files [4]. Chunks are wrapped in
<document>tags, the system message says their contents are data, and the bot has no tools, so the worst case is a bad answer. Those are mitigations, not guarantees.
Comment section
Sorry babes, you have to sign into Github to comment. Feel free to let me know what's on your mind!