{
    "slug": "fine_tuning",
    "term": "Fine-Tuning LLMs",
    "category": "ai_ml",
    "difficulty": "advanced",
    "short": "Training a pre-trained LLM on domain-specific data to improve performance on a specific task — more expensive and complex than prompt engineering but produces more consistent results.",
    "long": "Fine-tuning updates model weights on a curated dataset, specialising the model for a domain or task. Full fine-tuning updates all weights (expensive, requires GPUs). PEFT (Parameter-Efficient Fine-Tuning) methods like LoRA update a small fraction of weights — tractable on consumer hardware. When to fine-tune: consistent tone/style (not achievable with prompting), domain terminology, or format adherence. When NOT to fine-tune: adding new knowledge (use RAG), one-off tasks (use prompt engineering), or small datasets (risk overfitting).",
    "aliases": [
        "fine-tuning",
        "LoRA",
        "PEFT",
        "model fine-tuning"
    ],
    "tags": [
        "ai",
        "llm",
        "machine-learning"
    ],
    "misconception": "Fine-tuning adds new knowledge to a model — fine-tuning improves task performance and style; for adding specific facts or current data, RAG is more appropriate.",
    "why_it_matters": "Fine-tuning vs RAG vs prompt engineering is a fundamental architectural decision — choosing fine-tuning when RAG is appropriate wastes significant compute budget without better results.",
    "common_mistakes": [
        "Fine-tuning on small datasets — under ~1000 examples risks overfitting; the model loses generality.",
        "Using fine-tuning to add current knowledge — training data has a cutoff; use RAG for dynamic knowledge.",
        "Not evaluating the fine-tuned model against a held-out test set — training accuracy does not predict real performance.",
        "Fine-tuning before trying prompt engineering — prompt engineering is cheaper and often sufficient."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "large_language_models",
        "retrieval_augmented_generation",
        "prompt_engineering"
    ],
    "prerequisites": [
        "large_language_models",
        "embeddings",
        "ai_evaluation_metrics"
    ],
    "refs": [
        "https://huggingface.co/docs/peft/index"
    ],
    "bad_code": "// Fine-tuning for a task that prompt engineering handles:\n// Expensive approach: collect 5000 examples, train for 3 hours, $200 GPU cost\n// Task: 'Summarise PHP documentation in 2 sentences'\n\n// This is trivially solved with a system prompt:\nconst systemPrompt = 'You are a PHP documentation summariser.\nRespond with exactly 2 concise sentences.';",
    "good_code": "// Fine-tuning appropriate use case:\n// Task: generate code comments in our internal style guide\n// 1000+ examples of before/after pairs\n// Consistent style not achievable with prompting\n\n// LoRA fine-tune (efficient):\n// Training data format:\n// {\"prompt\": \"Add PHPDoc for: public function process(Order $order): bool\",\n//  \"completion\": \"/**\\n * Processes order payment and updates inventory.\\n * @param Order $order The order to process\\n * @return bool True if successful\\n */\"}\n\n// Cheaper alternative first: few-shot prompting with 10 examples in context",
    "quick_fix": "Try prompt engineering and RAG before fine-tuning — fine-tuning requires 100s of high-quality examples, costs money, and produces a model you must re-fine-tune as the base model improves",
    "severity": "info",
    "effort": "high",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/fine_tuning",
        "html_url": "https://codeclaritylab.com/glossary/fine_tuning",
        "json_url": "https://codeclaritylab.com/glossary/fine_tuning.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Fine-Tuning LLMs](https://codeclaritylab.com/glossary/fine_tuning) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/fine_tuning"
            }
        }
    }
}