Domain-Specific Small Language Models
What this skill does
This skill is a practitioner's decision-and-technique reference for building small language
models (SLMs) that are adapted to one domain, optimized to run cheaply, and
deployed close to the data — including on-prem clusters, laptops, CPUs, and mobile/edge
devices. It distills Guglielmo Iozzia's Domain-Specific Small Language Models.
Use it to answer questions like: Do I fine-tune or use RAG? Which quantization? How do I get
this onto a CPU or a phone? How do I serve it? How do I turn a small model into an accurate,
reasoning, RAG-grounded agent for my domain?
The book's thesis: you rarely need a frontier model. A carefully chosen, well-tuned model in
the ~100M–10B parameter range, pushed as close as possible to where data is generated and
decisions are made, delivers outsized business value — lower latency, lower cost, data privacy,
regulatory fit, and no vendor lock-in.
The mental model: the SLM lifecycle
Every SLM project moves through four stages. Most questions map to one of them.
1. CHOOSE & ADAPT Pick a base model in your domain → adapt it
(prompting → RAG → PEFT/LoRA → full fine-tune)
│
2. OPTIMIZE Make it small and fast enough for your target
(precision/quantization → ONNX → profiling → advanced quant)
│
3. DEPLOY & SERVE Put it where the data is
(vLLM / FastAPI server → laptop runner → Android/edge)
│
4. COMPOSE Make it capable and grounded
(RAG → vector DB → agents → GraphRAG → memory → test-time compute)
The stages are a pipeline, not a waterfall — optimization choices (e.g. INT4 for a phone)
constrain adaptation choices, and composition (RAG) can substitute for adaptation (fine-tuning).
Reference routing
Load the reference file that matches the task. Read only what you need.
| Topic |
Reference file |
Read when |
| What SLMs are, generalist-vs-domain decision, open-source ecosystem, data prep, fine-tuning, LoRA/PEFT, RAG-vs-fine-tune, end-to-end tuning + evaluation |
references/01-fundamentals-and-adaptation.md |
Choosing a base model; deciding how to adapt; preparing data; running a fine-tune; evaluating a tuned model |
| Generation/decoding params, inference cost math, GPU utilization, batching, DeepSpeed, ONNX format + Runtime + providers, exporting to ONNX, precision formats, INT8/INT4, LLM.int8(), GPTQ, ggml/gguf |
references/02-inference-onnx-quantization.md |
Tuning generation quality; cutting inference cost; converting to ONNX; picking a quantization method |
| Domain playbook: code generation, chemistry/proteins/materials; advanced quantization (FlexGen, SmoothQuant, BitNet 1.58-bit) |
references/03-domain-usecases-advanced-quant.md |
Adapting an SLM to a specialized/scientific domain; squeezing a model past INT4 |
| Profiling ONNX graphs, deployment & serving (vLLM, FastAPI, MLC LLM, Android), local runners (Ollama, LM Studio, Jan, Cortex) |
references/04-profiling-deployment-serving.md |
Finding bottlenecks; choosing how/where to serve; running locally or on-device |
| RAG pipelines, vector DBs, agents, GraphRAG, agentic RAG, memory, test-time compute, reasoning SLMs, OptiLLM |
references/05-llm-applications-and-test-time-compute.md |
Building a real application around the model; grounding, agents, or reasoning |
Master decision frameworks
These are the high-frequency calls. Deeper criteria live in the reference files.
1. Domain-specific SLM vs. generalist LLM
Prefer a domain-specific SLM when any of these hold:
- Data is sensitive / regulated (health, finance, legal, defense) — you need on-prem or
air-gapped inference, no data leaving your boundary.
- Cost at scale matters — per-token API costs of a frontier model dwarf a self-hosted SLM.
- Latency / offline — edge, mobile, or real-time needs rule out a round-trip to a hosted API.
- The task is narrow and repeatable — a tuned small model matches or beats a generalist on
your task while being 10–100× cheaper to run.
- You need control — reproducibility, versioning, no silent model swaps, no vendor lock-in.
Prefer a generalist (often hosted) LLM when: the task is broad/open-ended, volume is low,
you have no training data, or time-to-first-prototype dominates cost. Common pattern: prototype
on a generalist, then distill/fine-tune a small model for production.
2. RAG vs. fine-tuning (vs. both)
| Use RAG when… |
Use fine-tuning / PEFT when… |
| Knowledge changes often (docs, tickets, policies) |
The behavior/format/style must change (tone, schema, code dialect) |
| You need source citations / auditability |
You need a new skill or domain language, not just facts |
| Facts are large and enumerable |
Latency/prompt-length budget is tight (no room for retrieved context) |
| You must add/remove knowledge without retraining |
The domain vocabulary/tokens are alien to the base model |
| Hallucination on facts is the main risk |
You need the capability offline with no retrieval store |
They compose. The book's headline application pattern is a fine-tuned (or PEFT-adapted)
domain SLM + RAG: fine-tune for the domain's language and behavior, use RAG for current,
citable facts. Default to RAG first (cheaper, faster to iterate); add PEFT/LoRA when RAG can't
fix behavior; reserve full fine-tuning for when PEFT plateaus.
3. Quantization / precision picker
Order of preference — stop at the first that meets your accuracy + hardware budget:
- FP16 / BF16 (2 bytes) — near-lossless, ~2× smaller than FP32. First step for any GPU.
- INT8 (LLM.int8() / bitsandbytes, or ONNX dynamic/static) — ~4× smaller than FP32, small
accuracy loss; great default for GPU serving and many CPUs.
- INT4 — GPTQ (GPU, calibration-based) or ggml/gguf (llama.cpp, CPU/laptop-first). ~8×
smaller; the workhorse for laptops and commodity hardware. Expect some accuracy cost.
- Advanced — SmoothQuant (better INT8 by moving outliers weight-ward), AWQ, FlexGen
(offloading for throughput on one GPU), BitNet (1.58-bit ternary, needs a BitNet-native model).
Rule of thumb: quantize as little as you can get away with. More bits = more accuracy. Pick
the coarsest precision that still passes your domain evaluation, not the coarsest that runs.
4. Deployment / runner picker
| Target |
Reach for |
Why |
| High-throughput GPU serving |
vLLM (offline batch or OpenAI-compatible online server) |
PagedAttention + continuous batching → best tokens/sec |
| Simple custom API / microservice |
FastAPI wrapping the model |
Full control, easy to embed in an app |
| Developer laptop, quick local use |
Ollama (Modelfile), LM Studio (GUI + Python SDK), Jan, Cortex |
Zero-infra, private, gguf-based |
| CPU-only / pure C++ |
llama.cpp / ggml/gguf |
No GPU, portable |
| Phone / Android / cross-platform edge |
MLC LLM (compiles models per-target) |
On-device, offline, private |
| CPU/GPU-portable optimized runtime |
ONNX Runtime |
One graph, many execution providers |
Quick workflow for a new SLM project
- Frame the decision — domain SLM vs generalist (framework 1). If SLM, pick a base model
from a family already strong in your domain (see fundamentals reference).
- Adapt cheaply first — prompting → RAG → LoRA/PEFT → full fine-tune, in that order. Stop
when domain evaluation is good enough. (fundamentals + applications references)
- Define domain evaluation early — generic benchmarks lie; build task-specific metrics and a
validation set before you optimize. (fundamentals reference)
- Optimize to the target — precision/quantization + ONNX; profile to find the real
bottleneck before optimizing blindly. (inference-onnx-quantization + profiling references)
- Deploy where the data is — pick a runner (framework 4). (deployment reference)
- Compose — add RAG for facts, a vector DB, agents/tools, and (if you need reasoning)
test-time compute. (applications reference)
Guardrails the book insists on
- Measure, don't guess. Profile before optimizing; benchmark models against your task, not
a leaderboard. Accuracy claims from generic benchmarks rarely survive contact with a domain.
- Every optimization is a tradeoff. Quantization and offloading trade accuracy/latency for
size/throughput. Re-run domain evaluation after every optimization step.
- Privacy is a feature, not an afterthought. For regulated data, on-prem/local/on-device
inference (Ollama, MLC LLM, air-gapped ONNX) is often the reason to use an SLM at all.
- Leave the calibration knob. Hardware and quantization behave differently than the spec
sheet — keep tunable params (batch size, precision, decoding params) exposed and re-tune per
deployment target.
1---2name: domain-specific-slms3description: Design, adapt, optimize, and ship small, domain-specific language models (SLMs) — the practitioner's playbook from Guglielmo Iozzia's *Domain-Specific Small Language Models* (Manning). Use whenever someone is working with SLMs or trying to make an LLM run cheaply, privately, or on constrained hardware. Triggers: "should I fine-tune or use RAG?", "which quantization should I use?", "run an LLM on a laptop / CPU / phone / edge device", "shrink / speed up / reduce the cost of a model", LoRA / PEFT, ONNX / ONNX Runtime, INT8 / INT4 / GPTQ / ggml / gguf / AWQ / SmoothQuant / BitNet / FlexGen, vLLM, Ollama / LM Studio / Jan / Cortex / llama.cpp, MLC LLM, DeepSpeed, model profiling, "deploy an LLM with FastAPI / vLLM", domain-specific fine-tuning (code, chemistry, proteins, legal, medical, finance), RAG pipelines, vector databases, GraphRAG, agentic RAG, LLM agents, long/short-term memory, test-time compute, reasoning models, OptiLLM, or building a private/on-prem generative AI system in a regulated industry. Also t4---56# Domain-Specific Small Language Models78## What this skill does910This skill is a practitioner's decision-and-technique reference for building **small language11models (SLMs)** that are **adapted to one domain**, **optimized to run cheaply**, and12**deployed close to the data** — including on-prem clusters, laptops, CPUs, and mobile/edge13devices. It distills Guglielmo Iozzia's *Domain-Specific Small Language Models*.1415Use it to answer questions like: *Do I fine-tune or use RAG? Which quantization? How do I get16this onto a CPU or a phone? How do I serve it? How do I turn a small model into an accurate,17reasoning, RAG-grounded agent for my domain?*1819The book's thesis: **you rarely need a frontier model.** A carefully chosen, well-tuned model in20the **~100M–10B parameter** range, pushed as close as possible to where data is generated and21decisions are made, delivers outsized business value — lower latency, lower cost, data privacy,22regulatory fit, and no vendor lock-in.2324## The mental model: the SLM lifecycle2526Every SLM project moves through four stages. Most questions map to one of them.2728```291. CHOOSE & ADAPT Pick a base model in your domain → adapt it30 (prompting → RAG → PEFT/LoRA → full fine-tune)31 │322. OPTIMIZE Make it small and fast enough for your target33 (precision/quantization → ONNX → profiling → advanced quant)34 │353. DEPLOY & SERVE Put it where the data is36 (vLLM / FastAPI server → laptop runner → Android/edge)37 │384. COMPOSE Make it capable and grounded39 (RAG → vector DB → agents → GraphRAG → memory → test-time compute)40```4142The stages are a pipeline, not a waterfall — optimization choices (e.g. INT4 for a phone)43constrain adaptation choices, and composition (RAG) can substitute for adaptation (fine-tuning).4445## Reference routing4647Load the reference file that matches the task. Read only what you need.4849| Topic | Reference file | Read when |50|-------|----------------|-----------|51| What SLMs are, generalist-vs-domain decision, open-source ecosystem, data prep, fine-tuning, LoRA/PEFT, RAG-vs-fine-tune, end-to-end tuning + evaluation | `references/01-fundamentals-and-adaptation.md` | Choosing a base model; deciding how to adapt; preparing data; running a fine-tune; evaluating a tuned model |52| Generation/decoding params, inference cost math, GPU utilization, batching, DeepSpeed, ONNX format + Runtime + providers, exporting to ONNX, precision formats, INT8/INT4, LLM.int8(), GPTQ, ggml/gguf | `references/02-inference-onnx-quantization.md` | Tuning generation quality; cutting inference cost; converting to ONNX; picking a quantization method |53| Domain playbook: code generation, chemistry/proteins/materials; advanced quantization (FlexGen, SmoothQuant, BitNet 1.58-bit) | `references/03-domain-usecases-advanced-quant.md` | Adapting an SLM to a specialized/scientific domain; squeezing a model past INT4 |54| Profiling ONNX graphs, deployment & serving (vLLM, FastAPI, MLC LLM, Android), local runners (Ollama, LM Studio, Jan, Cortex) | `references/04-profiling-deployment-serving.md` | Finding bottlenecks; choosing how/where to serve; running locally or on-device |55| RAG pipelines, vector DBs, agents, GraphRAG, agentic RAG, memory, test-time compute, reasoning SLMs, OptiLLM | `references/05-llm-applications-and-test-time-compute.md` | Building a real application around the model; grounding, agents, or reasoning |5657## Master decision frameworks5859These are the high-frequency calls. Deeper criteria live in the reference files.6061### 1. Domain-specific SLM vs. generalist LLM6263Prefer a **domain-specific SLM** when any of these hold:6465- **Data is sensitive / regulated** (health, finance, legal, defense) — you need on-prem or66 air-gapped inference, no data leaving your boundary.67- **Cost at scale matters** — per-token API costs of a frontier model dwarf a self-hosted SLM.68- **Latency / offline** — edge, mobile, or real-time needs rule out a round-trip to a hosted API.69- **The task is narrow and repeatable** — a tuned small model matches or beats a generalist on70 *your* task while being 10–100× cheaper to run.71- **You need control** — reproducibility, versioning, no silent model swaps, no vendor lock-in.7273Prefer a **generalist (often hosted) LLM** when: the task is broad/open-ended, volume is low,74you have no training data, or time-to-first-prototype dominates cost. Common pattern: prototype75on a generalist, then distill/fine-tune a small model for production.7677### 2. RAG vs. fine-tuning (vs. both)7879| Use **RAG** when… | Use **fine-tuning / PEFT** when… |80|---|---|81| Knowledge changes often (docs, tickets, policies) | The *behavior/format/style* must change (tone, schema, code dialect) |82| You need source citations / auditability | You need a new *skill* or domain *language*, not just facts |83| Facts are large and enumerable | Latency/prompt-length budget is tight (no room for retrieved context) |84| You must add/remove knowledge without retraining | The domain vocabulary/tokens are alien to the base model |85| Hallucination on facts is the main risk | You need the capability offline with no retrieval store |8687**They compose.** The book's headline application pattern is a **fine-tuned (or PEFT-adapted)88domain SLM + RAG**: fine-tune for the domain's *language and behavior*, use RAG for *current,89citable facts*. Default to RAG first (cheaper, faster to iterate); add PEFT/LoRA when RAG can't90fix behavior; reserve full fine-tuning for when PEFT plateaus.9192### 3. Quantization / precision picker9394Order of preference — stop at the first that meets your accuracy + hardware budget:95961. **FP16 / BF16** (2 bytes) — near-lossless, ~2× smaller than FP32. First step for any GPU.972. **INT8** (LLM.int8() / bitsandbytes, or ONNX dynamic/static) — ~4× smaller than FP32, small98 accuracy loss; great default for GPU serving and many CPUs.993. **INT4** — GPTQ (GPU, calibration-based) or ggml/gguf (llama.cpp, CPU/laptop-first). ~8×100 smaller; the workhorse for laptops and commodity hardware. Expect some accuracy cost.1014. **Advanced** — SmoothQuant (better INT8 by moving outliers weight-ward), AWQ, FlexGen102 (offloading for throughput on one GPU), BitNet (1.58-bit ternary, needs a BitNet-native model).103104Rule of thumb: **quantize as little as you can get away with.** More bits = more accuracy. Pick105the coarsest precision that still passes your domain evaluation, not the coarsest that runs.106107### 4. Deployment / runner picker108109| Target | Reach for | Why |110|---|---|---|111| High-throughput GPU serving | **vLLM** (offline batch or OpenAI-compatible online server) | PagedAttention + continuous batching → best tokens/sec |112| Simple custom API / microservice | **FastAPI** wrapping the model | Full control, easy to embed in an app |113| Developer laptop, quick local use | **Ollama** (Modelfile), **LM Studio** (GUI + Python SDK), **Jan**, **Cortex** | Zero-infra, private, gguf-based |114| CPU-only / pure C++ | **llama.cpp** / **ggml/gguf** | No GPU, portable |115| Phone / Android / cross-platform edge | **MLC LLM** (compiles models per-target) | On-device, offline, private |116| CPU/GPU-portable optimized runtime | **ONNX Runtime** | One graph, many execution providers |117118## Quick workflow for a new SLM project1191201. **Frame the decision** — domain SLM vs generalist (framework 1). If SLM, pick a base model121 from a family already strong in your domain (see fundamentals reference).1222. **Adapt cheaply first** — prompting → RAG → LoRA/PEFT → full fine-tune, in that order. Stop123 when domain evaluation is good enough. (fundamentals + applications references)1243. **Define domain evaluation early** — generic benchmarks lie; build task-specific metrics and a125 validation set before you optimize. (fundamentals reference)1264. **Optimize to the target** — precision/quantization + ONNX; profile to find the real127 bottleneck before optimizing blindly. (inference-onnx-quantization + profiling references)1285. **Deploy where the data is** — pick a runner (framework 4). (deployment reference)1296. **Compose** — add RAG for facts, a vector DB, agents/tools, and (if you need reasoning)130 test-time compute. (applications reference)131132## Guardrails the book insists on133134- **Measure, don't guess.** Profile before optimizing; benchmark models against *your* task, not135 a leaderboard. Accuracy claims from generic benchmarks rarely survive contact with a domain.136- **Every optimization is a tradeoff.** Quantization and offloading trade accuracy/latency for137 size/throughput. Re-run domain evaluation after every optimization step.138- **Privacy is a feature, not an afterthought.** For regulated data, on-prem/local/on-device139 inference (Ollama, MLC LLM, air-gapped ONNX) is often the *reason* to use an SLM at all.140- **Leave the calibration knob.** Hardware and quantization behave differently than the spec141 sheet — keep tunable params (batch size, precision, decoding params) exposed and re-tune per142 deployment target.