The question
The usual retrieval-augmented generation stack is a cloud stack: a hosted vector database, an embedding API, a large model. We needed the opposite. Teachmint’s classroom panels had to answer questions from NCERT textbooks with no network, on a Rockchip RK3576 board, inside an Android app.
So the question was practical:
What breaks first when the whole retrieval pipeline has to run on the device?
My initial mental model
I assumed the language model would be the hard part. Retrieval looked like the easy half: embed some text, store vectors, take the nearest few.
That was backwards. Running a small model on the panel was the predictable part. Retrieval failed in several places, most of them before any vector was computed.
What actually happens
1. The corpus is the first bug
The pipeline starts from the textbook PDFs, and that’s where the first failures were:
- Text layers that lie. Many NCERT Hindi books use legacy glyph-coded fonts. Their text extracts as Latin mojibake that looks like text and embeds like noise. Extraction now has three quarantine gates: font name, empty text, and the ratio of non-Latin script. A Hindi book with a genuine Unicode layer slipped past the font gate, which is why the third exists.
- A draft textbook. A set of Social Science chapter PDFs in our storage turned out to be pre-publication drafts. One said “appointed by the Crown of the UK” where the official book says “by the Monarch (Head of State) on the advice of the Prime Minister”. A retrieval system answers confidently from whatever it was given. We replaced the files and now track source hashes.
- Books that aren’t textbooks. Question banks and exemplar books are mostly multiple-choice items with their own chapter numbering. Excluding them lifted on-target retrieval for real past exam questions from 44 to 58.
None of these show up in a retrieval metric until you look at what’s being retrieved.
2. Structure is information
Splitting text every N tokens destroys what makes a textbook searchable: sections, tables and equations. The extractor now uses a layout model, and the chunker is section-aware:
- chunks split within sections, never across them
- each carries a breadcrumb, “Book — Chapter — Section”, in the text that gets embedded
- tables stay whole where they fit
- running headers and page numbers are dropped
That removed 268 stray page numbers and almost all of 907 repeated reprint footers, and made 186 table chunks retrievable.
Mathematics is still the weak spot. The layout model turned sin²A into “sin 2 A”. In a separate test,
sending the raw chapter PDF to a large multimodal model produced far cleaner notation, with 5 equation
fragments orphaned from their context against 21, at about $0.12 per chapter as a one-time offline cost.
3. The embedder has to be identical on both sides
The pipeline embeds the textbooks on a laptop, and the panel embeds the question. If those two disagree, even slightly, retrieval quietly degrades. So the pipeline embeds with the exact int8 ONNX bytes pulled from the panel, a tokenizer configured to match the Kotlin one, and the same pooling and normalisation. Each database stores a fingerprint of a fixed sentence’s embedding, so drift is detectable.
The model was also chosen on the panel, not from a leaderboard. EmbeddingGemma matched the small models on a 20-question gold set, but took about 9.4 seconds per chunk on the panel’s CPU, against about 71 ms for MiniLM. bge-small shipped.
Let’s look at the implementation
The vector store was the one piece of real engineering on the device. The databases use the standard
sqlite-vec vec0 schema. The vendor SDK’s own vector store couldn’t read them, and Android’s SQLite
bindings don’t expose loading extensions at runtime.
The fix was to compile SQLite and sqlite-vec into a single JNI library and register the extension before any database opens:
// Simplified. Built with SQLITE_CORE=1 and SQLITE_VEC_STATIC=1:
// no runtime extension loading.
#include "sqlite3.h"
#include "sqlite-vec.h"
JNIEXPORT jlong JNICALL
Java_..._RagVecNative_open(JNIEnv *env, jclass c, jstring path) {
// Register once: every connection opened afterwards gets vec0.
sqlite3_auto_extension((void (*)(void)) sqlite3_vec_init);
sqlite3 *db;
sqlite3_open_v2(utf8(env, path), &db, SQLITE_OPEN_READONLY, NULL);
return (jlong) db;
}
A nearest-neighbour query is then plain SQL, with the query vector bound as a float32 blob:
SELECT c.id, c.text, v.distance
FROM vec_chunks v JOIN chunks c ON c.id = v.rowid
WHERE v.embedding MATCH ?1 AND k = 5
ORDER BY v.distance;
On the panel this reproduced the desktop pipeline’s results exactly, with a query p50 of about 7–8 ms on the pilot corpus and almost no JVM heap, because the vectors stay on disk. sqlite-vec is exact search, not an approximate index. Its advantage here is flat memory, not sublinear time.
The surprising part: the index was too big, not too small
With the full corpus of Classes I–XII in one combined index, Class 10 questions retrieved worse: hit@1 0.73, hit@3 0.93. Similar topics from other grades crowded out the right chapter. Splitting the index into one database per class and subject brought the same questions to hit@1 0.87, hit@3 1.00 and MRR 0.93.
More data made retrieval worse until it was scoped to the user. A teacher in a Class 10 science lesson doesn’t need Class 7’s chapter on the same topic, however semantically close it is.
Practical consequences for Android developers
- Audit the corpus before tuning anything. Fonts, drafts and question banks cost more accuracy than any model choice.
- Keep structure in the chunk. Section breadcrumbs are cheap and give the model context a raw window can’t.
- Make desktop and device embeddings provably identical. Same bytes, same tokenizer, a stored fingerprint.
- Measure the embedder on the device. “Same quality” models differed by more than 100× in cost.
- Static-link your SQLite extension if you need sqlite-vec on Android.
- Scope retrieval to the user’s context. One big index is not the neutral choice.
Keyword search is still missing. Exact identifiers like “Exercise 12.2” are where dense vectors are weakest. The obvious engine for that, SQLite’s FTS5, turned out not to exist in the panel ROM’s SQLite, which is its own story.
What I would remember
- Retrieval breaks before the model does, and mostly in the corpus.
- Measure what’s retrieved, not just how often the right chapter comes back.
- An embedding pipeline is two pipelines, desktop and device, that must agree byte for byte.
- A bigger index can be a worse index.
Further reading
- sqlite-vec and SQLite’s loadable extensions.
- ONNX Runtime for Android.
- Docling, the layout-aware document extractor.