I built StudyWithFriend, a local AI study assistant that lets you drop PDFs into a folder, ask questions, and get answers based on your own notes using RAG (Retrieval-Augmented Generation).

Sounds simple, right? Well, it wasn't.

The problem

In my first version, embedding a PDF would freeze the entire chat. My first solution was to move the embedding process into a separate terminal window.

The chat stopped freezing, but there was a new problem: the AI wasn't answering from my PDFs anymore.

After investigating the pipeline, I discovered that the database had exactly 0 chunks.

Five bugs were hiding behind that single symptom:

the 5 fixes

The debugging journey

1. The LLM was controlling the ingestion process

I was letting the model decide whether to start the embedding job. That's not something an LLM should be responsible for. The application should manage its own background tasks.

2. A breaking library change

The embedding job crashed at its final step because it was calling persist(), a method that was no longer available in the newer library version.

3. Silent failures

The separate terminal window closed as soon as the process finished. When something went wrong, I had no visible error and no useful feedback.

4. Chunk ID collisions

Every PDF started generating chunk IDs from 0, 1, 2....

So when I added another PDF, its chunks overwrote the previous ones. I wasn't building a knowledge base; I was replacing it.

5. Incorrect similarity scores

The search results were returning negative scores, but my filtering logic was dropping every match. The retrieval stage was effectively returning nothing.

What I changed

Instead of patching each symptom individually, I fixed the ingestion workflow:

  • Application-controlled execution: The app now triggers embedding jobs instead of relying on the LLM to decide.
  • Background processing: A background thread handles PDF ingestion without blocking the chat.
  • Proper feedback: Once the job finishes, the chat receives a status message, such as Ready: 30 chunks, or the actual error if something fails.
  • Unique chunk IDs: Each document now gets unique chunk identifiers, preventing collisions.
  • Correct similarity calculation: Fixed the scoring and filtering logic using cosine similarity.

The new architecture

An unexpected performance improvement

While working on this, I also discovered something interesting about Ollama.

I had num_gpu=1 configured, assuming it meant using one GPU.

It actually controls how many model layers are offloaded to the GPU, not the number of GPUs.

After removing that setting, my model's generation speed went from 16 tokens/s to 81 tokens/s.

That's roughly a 5x improvement from one configuration change.

The result

An 8-page PDF is now fully processed and searchable within approximately 3 seconds after dropping it into the folder.

More importantly, the chat stays responsive, ingestion errors are visible, and retrieval actually works.

What I learned

The biggest lesson wasn't about embeddings, vector databases, or similarity search.

It's about system design:

A background job that can't report back is a job you can't trust.

When building AI applications, it's easy to focus on the model and forget the engineering around it. But sometimes the problem isn't the AI at all. It's the infrastructure connecting everything together.

I'm continuing to improve StudyWithFriend, and this debugging session was a good reminder that reliable AI systems start with reliable software engineering.