Data Science & ML · Cloud, DevOps & LLMOps
LexDistill: Claude Distilled into Small Local Models for Law and Tax
By David Gadyan · Published
Large models such as Claude answer specialist questions well, but every answer costs money, needs the internet and sends the data to an outside API. LexDistill teaches seven small open models (1.7B to 14B) to answer like Claude in Spanish immigration law and autónomo tax, using 2,494 Claude-written answers that code checks against the official BOE law texts. QLoRA fine-tuning on one 16 GB laptop GPU lifts the best model from 59.2% to 71.9% on held-out questions, cuts made-up legal citations from 16% to 0%, and the distilled models run locally in 2.2 to 10.6 GB of GPU memory with no per-request API bill. PyTorch, Transformers, PEFT/QLoRA, TRL, Claude, an offline results dashboard.
The problem
Large models such as Claude answer specialist questions well, for example "which documents do I need to change from a student visa to a work permit?" or "what is the last day to file my quarterly VAT return?". But each answer is billed per token, needs an internet connection, and sends the question, often with personal details, to an outside API. A small open model can run on a laptop or a small server for free, but out of the box it gets these questions wrong, invents legal citations and often breaks the required output format. LexDistill asks: can a small model on one laptop GPU learn to answer like Claude in one narrow field? The field is Spanish immigration law (visas, residence statuses, changes of status) and tax for the self-employed, autónomos (VAT, income tax instalments, filing deadlines).
The dashboard in use
What I built
Teacher answers from Claude, checked by code before use. Questions are built from the official consolidated texts of ten Spanish laws in the BOE, using the version in force on the project's as-of date. Claude Sonnet writes most answers, Opus the hardest ones, and Haiku writes questions and critiques. Every answer follows a strict JSON schema with the jurisdiction, the as-of date, evidence quotes, citations, assumptions and a confidence. Before an answer can become training data, code checks that every quote really appears in the source text, that every citation points to that source, that tax arithmetic and filing deadlines match a calculator and calendar written in code, that regional regimes (País Vasco, Navarra, Canarias) are flagged, and that no personal data is present. 1,220 answers for numeric and date tasks are computed by code alone, with no model call. The result is 2,494 validated training examples across seven task types.
| Task type | What the model must do |
|---|---|
| Statute Q&A | Answer from the law text, quoting it |
| Eligibility | Mark each requirement met, unmet or unknown |
| Document checklist | List the documents for a procedure |
| Status transition | Explain how to move from one residence status to another |
| Deadline calendar | Give the filing date, moved past weekends and holidays |
| Tax computation | Compute Modelo 303 VAT, Modelo 130 instalments, invoice withholding |
| Issue spotting | Spot foral regimes, Canarias IGIC or missing facts, and decline to answer |
Fine-tuning that fits one laptop GPU. Seven open models are trained with QLoRA: the base model is loaded in 4-bit and only small LoRA adapters are trained, on one RTX 4090 Laptop GPU with 16 GB. The loss is computed on the answer only, in chunks, so even the 14B models fit. The final runs took 31 to 151 minutes and 0.04 to 0.20 kWh each. The 4B student was also trained a second way, with teacher-logit knowledge distillation from Qwen3-8B, to compare both recipes.
A fair, leakage-safe evaluation. Each distilled model is compared with its own untuned base on the same questions, which never appear in training. The split is made by law-text chunk so no passage leaks, and the evaluation sets were frozen before the last training rounds. There are three sets: test (51 questions), gold (23 questions whose answers are computed by code) and out-of-distribution (42 questions from a law held out of training). Metrics: a task-weighted score, a strict success rate, schema validity, made-up citations, critical errors (a fabricated quote or citation, wrong arithmetic, a wrong date), correct refusals and calibration.
An offline results dashboard. One static page that reads the evaluation report: a leaderboard with a Pareto frontier (score against training data, GPU memory, latency or tokens), what changed per task, reliability diagrams, latency, training curves, a cost calculator and a browser of real answers with the quoted law highlighted in the BOE text.
Architecture design
Hover or tap a component to light its path and see what it does. Built from this project's stack.
Results
Score on each set, untuned base model → distilled model (QLoRA SFT on about 730 to 830 examples):
| Student | Test (51) | Gold (23) | Unseen law (42) | Mean gain |
|---|---|---|---|---|
| Qwen3-14B | 59.2 → 71.9 | 45.2 → 56.3 | 54.3 → 66.8 | +12.1 pp |
| Mistral-Nemo 12B | 27.1 → 70.9 | 16.5 → 70.9 | 24.1 → 67.6 | +47.2 pp |
| Granite-3.3 8B | 12.9 → 55.4 | 8.5 → 56.4 | 11.9 → 68.4 | +49.0 pp |
| Qwen3-8B | 37.9 → 57.9 | 42.6 → 49.4 | 54.7 → 69.3 | +13.8 pp |
| Phi-4 14B | 49.8 → 56.2 | 56.8 → 49.5 | 56.9 → 65.5 | +2.6 pp |
| Qwen3-4B | 47.5 → 55.1 | 32.4 → 30.6 | 54.9 → 68.2 | +6.3 pp |
| Qwen3-1.7B | 9.2 → 53.4 | 3.8 → 31.2 | 7.6 → 48.4 | +37.5 pp |
- The score rose in 19 of 21 model and set combinations. The two drops are on the 23-question gold set, where one question is worth about 4 points.
- Made-up legal citations fell from 16.0% of answers on average to 0.0% for every distilled model.
- Output format: valid in 95 to 100% of answers for every distilled model; the weakest base models were at 26 to 80%.
- Calibration (ECE) improved in 21 of 21 cases: the models' confidence matches their accuracy better.
- The largest gains come from models that mostly failed the output format before training; the strongest base models gain less but still improve on every set.
Cost and running on edge hardware
| Claude Sonnet as the answer engine | Distilled local model | |
|---|---|---|
| Cost per 1,000 answers | ≈ $12 at list price (estimate) | $0 API charge (electricity and hardware only) |
| Where it runs | Outside API, needs internet | One laptop or small edge GPU, offline |
| Where the data goes | To the API provider | Stays on the machine |
| GPU memory | n/a | 2.2 GB (1.7B) to 10.6 GB (14B), 4-bit |
| Speed | n/a (not measured on these sets) | 22 to 32 tokens/s, median 18 to 27 s per structured answer |
The Sonnet estimate uses the project's own measured average of 2,494 input and 731 output tokens per teacher answer and the list price at the time (Opus works out at about $27 per 1,000). All Claude calls for the training data come to about $19 at list price. The distilled models also cost electricity and hardware, which depend on the owner's setup; the dashboard's calculator takes them as inputs instead of assuming them.
Limits
- A research prototype, not legal or tax advice. Answers can be wrong or out of date.
- The evaluation sets are small (23 to 51 questions), so differences of a few points between the best distilled models are within noise.
- Claude itself was not scored on these sets, so the result is "far better than the untuned small model, at no per-request API cost", not "as good as Claude".
- Arithmetic and dates are still the weak spot (tax computation and deadlines in the heatmap); giving the student the code calculator as a tool is the next step.
Related projects
Computer Vision with YOLO26/27: Three Edge-Ready Demos
Computer Vision & Generative Media · Data Science & ML · Cloud, DevOps & LLMOps
Hybrid Recommender API on GCP (Chilldays)
Data Science & ML · Cloud, DevOps & LLMOps · Databases & Data Engineering
AI-Powered SME Synergy Matching Platform
Full-Stack & UI/UX · Data Science & ML · Cloud, DevOps & LLMOps