-
Notifications
You must be signed in to change notification settings - Fork 132
document classification with a local llm
To classify documents with a small local LLM, ask one yes/no question per document type ("Is this document an invoice?", "Is this document a contract between two parties?") about the first page or the first few hundred words, and pick the type from the probabilities, with an "other" bin when none is high. No training data is needed, a new type is a new question, and the text stays on your machine. What you have to handle yourself is getting text out of the file and deciding how much of it to send, because the text plus each question holds at most 8,192 tokens and every token costs time.
The non-obvious part is that more text rarely helps. A document announces its type early: the header, the title, the first paragraph. Sending the whole file makes each request slower and buries the signal among clauses and tables.
This page is the questions, how much of the document to send, how to turn probabilities into a label, documents that are two things at once, the step before the model, and how to check the result on your own archive.
Write each type as a question about what the document is, with the distinguishing feature in the question:
{
"model": "jev-latest",
"state": {
"file_name": "scan_0412.pdf",
"first_page": "INVOICE No. 2026-118. Bill to: ... Item, quantity, unit price ... Total due within 30 days."
},
"questions": {
"invoice": {"type": "noul", "instructions": "Is this document an invoice asking for payment?"},
"contract": {"type": "noul", "instructions": "Is this document a contract or agreement between parties?"},
"report": {"type": "noul", "instructions": "Is this document a report that presents findings or results?"},
"receipt": {"type": "noul", "instructions": "Is this document a receipt confirming a payment already made?"},
"letter": {"type": "noul", "instructions": "Is this document a letter addressed to a person or organisation?"}
}
}"Invoice asking for payment" and "receipt confirming a payment already made" differ in one
feature, and naming it is what separates them. Each question comes back as its own noul, and
questions in the same request share the text, which is read once. The general method, with the
pitfalls of a plain argmax, is on
zero-shot text classification with yes/no questions.
The first page, almost always. Three reasons.
- Speed. On our reference laptop the cost grows with the text: a request of about 190 tokens read from scratch took 112 ms. A first page of 400 to 600 tokens takes longer, and a whole report longer still; the numbers behind that are on why LLM latency grows with the length of the text.
- Limit. jevos reads at most 8,192 tokens per question, the text plus that question, by default. Many documents are longer.
- Signal. Type is a property of the whole document that is visible at the start. A contract says "Agreement" in its title and names the parties in its first lines.
Two useful additions: the file name, which often carries a hint, and a short sample from the last page, where signatures, totals and "Yours sincerely" live. If a type is only visible deep inside (an appendix that turns a letter into a claim form), classify by passage and combine, as on yes/no questions about long documents.
A workable rule in code:
- Take the type with the highest probability.
- If it is below a threshold you chose on labelled documents, the label is "other" and the file goes to a person or a default folder.
- If two types are both high, record both and let the workflow decide (see the next section).
The threshold does most of the work. A low one fills folders with wrong files; a high one sends too many to "other". Choose it by looking at a few dozen labelled documents near the boundary, not by default at 0.5.
Real archives are messy: an email with an invoice pasted in, a report with a contract attached, a letter that is also a complaint. Forcing one label hides the second. Two approaches:
- Allow several labels. If both "invoice" and "letter" are above the threshold, file it under both, or route it by the one your process cares about more.
- Ask what it mainly is. "Is this document mainly an invoice?" pushes the model toward the primary purpose. The wording pattern is on mainly about: questions for messages with several topics.
jevos reads text. It does not open PDFs, run OCR on scans or read tables as images. Before the request you need a text extraction step: the PDF's own text layer when it has one, OCR when it does not. Extraction quality sets an upper bound on classification quality, and a scanned page with bad OCR is where most errors in a pipeline like this come from. Check a sample of extracted text by eye before you tune any threshold.
It also reads English only. A mixed-language archive needs a translation step or a multilingual model, as discussed on using an English-only LLM with other languages.
Take a hundred or two documents you have already filed, with their correct types. Run the questions, and look at three things: the confusion between similar pairs (invoice and receipt, report and letter), the share that lands in "other", and the documents where two types were high. Then change the wording of the confused pair, not the threshold, first. On our 999-question test set written after training, stated facts were answered right 0.954 of the time, and "is this an invoice" is usually a question of that kind when the header says so. We have not measured document classification as such, so your archive is the only accuracy figure that counts. A method for the test set is on building a yes/no test set for your own data.
Once a document has a type, the next questions are often about its content. For contracts, that is contract clause detection with a local LLM.
Can a local LLM classify documents without training? Yes, if each type is a yes/no question. A new type is a new question, not a new model.
How much of the document should I send? Usually the first page, plus the file name and perhaps a sample of the last page. The limit is 8,192 tokens for the text plus each question.
Can it read PDFs or scans? Not directly. Extract the text first, with OCR for scans.
What about documents that match no type? Use a threshold: when no type is high enough, the label is "other".
Is it fast enough for a backlog? A request of about 190 tokens took 112 ms on a laptop CPU. Time a few of your own first pages; a backlog is a batch job.
See also: product categorization with yes/no questions, jevos vs bart-large-mnli for zero-shot classification and batch decisions from files with jev decide.
- Our measurements: context, latency and the reference laptop from the
jev README; accuracy on stated facts from our 999-question
test set on
jevos-q4_k_m. No document classification measurement exists; none is claimed. - No external facts are stated on this page.
From the notes of jev, a yes/no decision model that runs on a laptop CPU. The invoice and receipt pair is in the example on purpose: they differ by one feature, and the question has to name it.
- Ask a local LLM a yes/no question and get P(yes)
- Zero-shot text classification with yes/no questions
- LLM policy decisions: put the rule in the question
- LLM as a judge on a CPU
- Why a small LLM says yes when the answer is no
- Small LLMs and arithmetic in yes/no questions
- Our held-out benchmark said 0.855, new questions said 0.757
- jevos vs Jev vs Laya for yes/no decisions
- An open-source alternative to Jev for yes/no decisions
- jevos vs the OpenAI API for yes/no classification
- jevos vs Ollama for yes/no decisions
- jevos vs bart-large-mnli for zero-shot classification
- A yes/no LLM vs a fine-tuned BERT classifier
- jevos vs SetFit: zero-shot vs few-shot classification
- jevos vs Llama Guard for content safety checks
- jev serve vs llama.cpp server for classification
- jevos vs LM Studio: a decision server, not a chat app
- Local vs hosted LLM decisions: latency, cost, privacy
- A yes/no LLM vs a business rules engine
- LLM decisions vs keyword rules and regex
- The fastest AI model for yes/no decisions
- What makes a local LLM fast on a CPU
- Why one forward pass beats generating an answer
- Prefill vs decode: where LLM latency comes from
- Why LLM latency grows with the length of the text
- Why a hosted LLM API cannot answer in 50 ms
- Many questions about one text: why the extra ones are cheap
- CPU or GPU for a small LLM
- Latency budgets: where a 200 ms model fits
- Measuring LLM latency: median, p90 and warm-up
- Q4_K_M vs Q8_0: speed and size for a small model
- Throughput vs latency for a decision server
- What P(yes) means, and what it does not
- LLM calibration explained with yes/no answers
- Expected calibration error (ECE), explained
- Temperature scaling for LLM probabilities
- Platt scaling for a yes/no model
- Reading a reliability diagram
- How to choose a threshold for P(yes)
- Thresholds when a wrong yes costs more than a wrong no
- Human in the loop AI with a review band
- Precision and recall at a P(yes) threshold
- Base rates: why a 0.9 yes can still be wrong often
- Combining yes/no answers with AND, OR and NOT
- Logits, log-odds and P(yes)
- LLM confidence scores: probabilities vs self-reports
- How to write yes/no questions an LLM answers well
- Negation in yes/no questions for an LLM
- One condition per question: splitting compound questions
- Ask whether the text says it at all
- Scores as yes/no thresholds: is it at least high?
- Sending JSON as the text: designing the state
- Why wording changes an LLM's answer, and how to test it
- Mainly about: questions for messages with several topics
- Yes/no questions about tone and emotion
- Asking about intent: what does the writer want?
- Yes/no questions about long documents
- Using an English-only LLM with other languages
- Content moderation with a local LLM
- A Discord moderation bot with a local LLM
- Spam detection with yes/no questions
- Review moderation with a local LLM
- Email triage with a local LLM
- Support ticket routing with yes/no questions
- Urgency detection in customer messages
- Sentiment analysis with yes/no questions
- Intent detection with a local LLM
- Lead qualification with yes/no questions
- Fraud case triage with a local LLM
- Phishing email screening with a local LLM
- Log and alert triage with a local LLM
- Checking text for personal data with yes/no questions
- Prompt injection screening with a small model
- Document classification with a local LLM
- Product categorization with yes/no questions
- Contract clause detection with a local LLM
- Refund request triage with a local LLM
- Detecting cancellation intent in customer messages
- RAG evaluation with yes/no questions
- RAG faithfulness check with a local LLM
- Hallucination detection with a local LLM
- LLM regression tests in CI with yes/no checks
- Rubric design for an LLM judge
- Pairwise comparison with a yes/no judge
- LLM judge bias and how to control it
- Evaluation metrics for yes/no classifiers
- Building a yes/no test set for your own data
- Accuracy by kind of question: why one number hides failures
- Generating test questions with answers computed by code
- Benchmark contamination and truly held-out tests
- An LLM router with yes/no questions
- A model cascade: small model first, large model on doubt
- Semantic routing vs yes/no questions
- Gating AI agent tool calls with yes/no checks
- AI agent guardrails with yes/no questions
- Stop conditions for AI agents
- Logging LLM decisions for audit
- Reducing LLM cost with local yes/no decisions
- Replacing chat LLM calls with yes/no questions
- Structured output vs a probability
- A Python client for local LLM decisions
- Calling a local LLM decision server from JavaScript
- Local LLM yes/no decisions in n8n
- A Slack bot that uses local LLM decisions
- Home Assistant automations with local LLM decisions
- A LangChain tool for local yes/no decisions
- Batch decisions from files with jev decide
- Running LLM yes/no checks in GitHub Actions
- Securing a local LLM server with an API key
- curl examples for a local LLM decision API
- Self-hosted AI for decisions
- A private LLM for text classification
- On-premise LLM for business decisions
- GDPR and automated decision-making with an LLM
- Offline AI for decisions: no network needed
- Edge AI decisions on a CPU
- Run an LLM locally without a GPU
- Small language models explained
- When a small model is enough, and when it is not
- An LLM on a laptop: what it can do in real time
- What is GGUF, for someone deploying a classifier
- GGUF quantization types explained: Q4_K_M, Q8_0 and others
- GGUF vs safetensors
- llama.cpp vs Ollama for a classification service
- llama-cpp-python vs calling llama.cpp through ctypes
- llama.cpp on Windows without compiling
- Running llama.cpp CPU only
- Using llama.cpp prebuilt binaries instead of building