A local contract and invoice agent whose citations are checked by code
- Hardware
- A laptop, fully local, no API key
- Models & engines
- Models compared: Gemma 4 12B, Gemma 3 4B, Qwen3.5:9B
Ollama for embeddings - Key gotcha
- The first version called `ollama embed`, a CLI subcommand that doesn't exist; every call silently fell back to a three-dimension mock vector and the tests still passed. He removed the fallback so ingestion now fails loudly.
What they built
Gonzalo Blasco built a local-first agent that answers questions about contracts and invoices. It runs entirely on a laptop with no API key, no cloud and no data leaving the machine.
His point is that the hard part isn't retrieval but verification: a model can invent a clause or a page number that sounds right. So every answer comes back with structured citations, and the app checks them with code instead of trusting the model.
It started as a design exercise for a DEV.to challenge after the deadline had passed; the photographer with contracts in the case study is a persona, not a real user. The code is MIT-licensed at github.com/gonzoblasco/localdoc-agent.
Setup
- A laptop, fully local, no API key
- Models compared: Gemma 4 12B, Gemma 3 4B, Qwen3.5:9B
- Ollama for embeddings
- SQLite with sqlite-vec for vector search
- Python project run with uv and pytest
How it works
- Every answer includes citations made of a chunk_id and a literal quote.
- A citation is valid only if its quote is a substring of the chunk after whitespace normalization. If one fails, the model gets one more try; if it fails again, the answer becomes a refusal.
- Arithmetic, such as summing two contract totals, is done by a few lines of deterministic code: the agent retrieves and composes, and the calculator adds.
- A button deletes the index, embeddings and drafts, and he checked with lsof that no outbound connection is open during transcription or query.
Results & numbers
- On his 18 questions, Gemma 4 12B scored 18/18 with a P50 latency of 49.5 s; Gemma 3 4B scored 4/18 at 6.8 s.
- Qwen3.5:9B kept a 262k context resident and didn't finish a single answer in 90 seconds, while the 12B answered in 50.
- The 12B consistently failed to add two contract totals, which is why sums moved to code.
Gotchas
- The first version called `ollama embed`, a CLI subcommand that doesn't exist; every call silently fell back to a three-dimension mock vector and the tests still passed. He removed the fallback so ingestion now fails loudly.
- sqlite-vec was never loaded, so chunks_vec was a regular table and the "KNN" was brute force; it only worked by accident with 13 chunks. Check sqlite_master for CREATE VIRTUAL TABLE ... USING vec0.
- Three times a coding subagent reported files it never wrote; run git status and the tests before accepting any delivery.
- Limits he lists: no scanned PDFs (they're rejected), about 50 seconds per answer with the 12B, one person and one corpus, and the 18 test questions were written by the same person who built it.
“what can be verified by code, is verified by code.”— Gonzalo Blasco
Do it yourself
- Run a local model and embedding model (he used Gemma via Ollama) and store chunks in SQLite with sqlite-vec loaded as a vec0 virtual table.
- Ask the model to return its answer with citations as chunk_id plus a literal quote.
- Verify each quote is a substring of the cited chunk after whitespace normalization; retry once on failure, then refuse.
- Do any arithmetic in code, not in the model.
- Measure accuracy and latency on your own machine, corpus and questions before picking a model.
def verify_citations(citations, chunks_by_id):
valid, invalid = [], []
for cit in citations:
chunk = chunks_by_id.get(cit["chunk_id"], "")
if _normalize(cit["quote"]) in _normalize(chunk):
valid.append(cit)
else:
invalid.append(cit)
return valid, invalidSimilar setups
Receipt Box: Gemma 3 4B in Ollama sorts a freelancer's receipts and VAT on the laptop
E. Smith built Receipt Box, a receipt and invoice sorter for freelancers where Gemma 3 4B with vision, running in Ollama on the user's own machine, reads vendor, date, total, VAT, currency and category from photos and PDFs.
Gist: an offline exam-prep partner that mines past papers with Ollama
A student built Gist so a friend could study from lecture PDFs and three-plus years of past exam papers without uploading copyrighted course material anywhere; parsing, embeddings, search and the LLM all run on the laptop with Wi-Fi off.
Breaking re-ranker ties with Gemma's own token probabilities on a Mac mini
A developer testing a local retrieval setup on an M4 Mac mini fixed tied re-ranker scores from gemma3:12b by reading the model's digit probabilities from Ollama, getting the right article first on 11 of 12 questions.
Company Brain: a private consultancy RAG on a 16 GB MacBook Air M4 with Ollama and Open WebUI
Ángeles Hernández Acosta’s KeepCoding final project runs Qwen3.5-4B via Ollama on a 16 GB MacBook Air M4, answers only from local documents with citations, and scored 16/22 on a locked final exam run on 6 November 2026.