DavidGadyan

Data Science & ML · Cloud, DevOps & LLMOps

LexDistill: Claude Distilled into Small Local Models for Law and Tax

By David Gadyan · Published

LexDistill score vs latency chart: held-out test score against median latency per answer for every run, one line per model from its untuned base (small marker) to its Claude-distilled versions (large marker): Qwen3-14B 71.9% and Mistral-Nemo 12B 70.9% on top at about 27 s, then Qwen3-4B KD 60.1%, Qwen3-8B 57.9%, Phi-4 14B 56.2%, Granite-3.3 8B 55.4%, Qwen3-4B 55.1% and Qwen3-1.7B 53.4%, with a dashed Pareto frontier of the best score at each latency

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

The results dashboard on the real runs: base vs distilled scores, the leaderboard by training data, GPU memory and latency, the full results table, what distillation changed per task, reliability, latency, training curves, the cost calculator with the measured Sonnet token counts, real example answers with the law text highlighted, scaling and the per-task heatmap.

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 typeWhat the model must do
Statute Q&AAnswer from the law text, quoting it
EligibilityMark each requirement met, unmet or unknown
Document checklistList the documents for a procedure
Status transitionExplain how to move from one residence status to another
Deadline calendarGive the filing date, moved past weekends and holidays
Tax computationCompute Modelo 303 VAT, Modelo 130 instalments, invoice withholding
Issue spottingSpot 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.

Leaderboard: ES-ImmTax score against training examples, one line per model from its untuned base at 0 examples to each retrain, Qwen3-14B and Mistral-Nemo 12B on top at about 71%, Qwen3-1.7B and Granite-3.3 8B rising from about 10% to over 50%

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.

Training section: train and eval loss per run, gradient norm, learning rate and peak reserved GPU memory for every QLoRA run, all under the 16 GB line

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.

Example answers: a Qwen3-14B answer for the last day to file Modelo 303 and 130 for Q1 2025, the reference answer, and the BOE text on quarterly deadlines with the model's quoted evidence highlighted

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):

StudentTest (51)Gold (23)Unseen law (42)Mean gain
Qwen3-14B59.2 → 71.945.2 → 56.354.3 → 66.8+12.1 pp
Mistral-Nemo 12B27.1 → 70.916.5 → 70.924.1 → 67.6+47.2 pp
Granite-3.3 8B12.9 → 55.48.5 → 56.411.9 → 68.4+49.0 pp
Qwen3-8B37.9 → 57.942.6 → 49.454.7 → 69.3+13.8 pp
Phi-4 14B49.8 → 56.256.8 → 49.556.9 → 65.5+2.6 pp
Qwen3-4B47.5 → 55.132.4 → 30.654.9 → 68.2+6.3 pp
Qwen3-1.7B9.2 → 53.43.8 → 31.27.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.

What distillation changed: per-task score bars for each base model and its distilled version, with gains such as +57 pp on eligibility and +45 pp on statute Q&A

Per-task heatmap on the test set: statute Q&A, eligibility, document checklist, status change, deadlines, tax maths and issue spotting for every run, darker cells for higher scores

Cost and running on edge hardware

Leaderboard with GPU memory on the x-axis: each model's untuned base and distilled version, Qwen3-1.7B near 2.2 GB, Qwen3-4B near 3.6 GB, Granite-3.3 8B 5.5 GB, Qwen3-8B 6.9 GB, Mistral-Nemo 12B 8.9 GB and Qwen3-14B 10.6 GB, with the Pareto frontier of the best score for each memory budget

Claude Sonnet as the answer engineDistilled local model
Cost per 1,000 answers≈ $12 at list price (estimate)$0 API charge (electricity and hardware only)
Where it runsOutside API, needs internetOne laptop or small edge GPU, offline
Where the data goesTo the API providerStays on the machine
GPU memoryn/a2.2 GB (1.7B) to 10.6 GB (14B), 4-bit
Speedn/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.

Economics: the cost calculator with Claude Sonnet and the measured 2,494 prompt and 731 completion tokens, $12.30 per 1,000 requests teacher-only, the monthly cost curve and the API usage ledger by stage and model

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

← All projects