Sub topic 12
Combining Prompting and Fine-Tuned Models
This chapter assumes that you know JSON, basic Python, and introductory precision, recall, and F1. It also assumes that you understand the difference between a general-purpose language model and a model trained for one task.
Prompting and fine-tuning are not mutually exclusive. A production information-extraction system can use both. A large language model (LLM) can handle unusual cases and create labels. A smaller fine-tuned model can process common cases quickly and cheaply.
The hybrid pipeline
A common design has three stages:
- A fine-tuned extractor handles normal inputs.
- Validation checks the prediction against business rules.
- An LLM handles low-confidence or invalid results.
The LLM can also label new examples. Those labels are reviewed, corrected, and added to the training set. This process is called distillation when the smaller model learns from predictions produced by a larger model.
The fast path works well when documents follow familiar patterns. The fallback covers new layouts, rare wording, and fields that the fine-tuned model has not seen.
LLM labeling and distillation
Suppose you have 50,000 unlabeled invoices but only 500 manually labeled invoices. You can ask an LLM to label additional invoices using a strict schema. Human reviewers then inspect a sample or all high-risk records. The reviewed results become training data for a token-classification model.
This approach can reduce annotation time, but LLM output is not automatically ground truth. The LLM can miss entities, invent values, or apply a rule inconsistently. Track the source of every label:
- human-verified label
- LLM-generated label
- model-generated label
- unresolved label
Use human labels as the main evaluation set. Do not evaluate the student model only against labels produced by its teacher.
Fallback rules
A fallback should have explicit triggers. Useful signals include:
- the model's confidence is below a threshold
- a required field is missing
- a currency does not match the document
- the total cannot be parsed as a number
- the document type is unknown
- the extracted span violates a business rule
The threshold should be chosen on a validation set. A low threshold sends fewer documents to the LLM but risks more errors. A high threshold improves coverage at greater cost and latency.
from dataclasses import dataclass
from typing import Any
@dataclass
class Result:
data: dict[str, Any]
confidence: float
valid: bool
def needs_fallback(result: Result, required_fields: set[str]) -> bool:
missing = required_fields - result.data.keys()
return (
result.confidence < 0.85
or bool(missing)
or not result.valid
)
def route(result: Result, required_fields: set[str]) -> str:
if needs_fallback(result, required_fields):
return "llm"
return "fine_tuned_model"
sample = Result(
data={"vendor": "Acme", "total": 120.5},
confidence=0.91,
valid=False,
)
print(route(sample, {"vendor", "total"}))The example routes the result to the LLM because the extraction is invalid, even though its confidence exceeds the threshold.
Operational trade-offs
| Design | Strength | Main risk |
|---|---|---|
| Fine-tuned model only | Low latency and predictable cost | Weak on new formats |
| LLM only | Flexible and quick to adapt | Higher cost and variable output |
| Fine-tuned model with LLM fallback | Efficient common-case processing with broader coverage | More routing and monitoring complexity |
Monitor each route separately. Record latency, cost, validation failures, fallback rate, and span-level F1. A rising fallback rate may indicate document drift, a broken parser, or a new document source.
Choosing a hybrid design
Use a hybrid pipeline when the normal workload is large and repetitive, but unusual cases are important. Keep the fine-tuned model when its quality is stable and its cost advantage is meaningful. Use the LLM fallback for ambiguity rather than for every input.
Review fallback examples regularly. If the same failure appears often, add corrected examples and retrain the fine-tuned model. If failures remain rare and varied, keeping them on the LLM path may be simpler.
Exercise
An extractor handles 98% of invoices correctly, but fails on new vendor layouts. What hybrid strategy should you try first?
Show answer
Keep the fine-tuned extractor as the fast path, route low-confidence or validation-failing results to an LLM, and review fallback outputs for future training data.