Skip to content
mentoor

Topic 1 of 4 · 5 min read

Embeddings as model features

Goal: Train a classifier on sentence embeddings and compare its accuracy and cost per prediction with zero-shot LLM prompting.

An embedding model turns text into a fixed-length vector, and a plain logistic regression or gradient-boosted model trained on those vectors is often a fast, cheap and accurate classifier. You pay for the embedding once per text and the classifier costs almost nothing to run.

Check yourself · 1 of 3

You have 500 labelled tickets and need a classifier that answers in milliseconds on a CPU. What is the most direct approach?

Choose one answer

Why it matters

Prompting an LLM to classify every incoming ticket costs tokens and about a second of latency per call, and the answer can change between runs. A classifier on embeddings runs in milliseconds on a CPU, gives the same output for the same input, and exposes a probability you can threshold. With a few hundred labelled examples it often matches a zero-shot LLM on a narrow task, and you can measure that instead of guessing.

How to do it

  1. Pick an embedding model. Sentence Transformers gives open-weight models you run yourself. Hosted embedding APIs from OpenAI, Google or Cohere work too. Fix the model name and version, because vectors from different models cannot be mixed.
  2. Embed once, store the vectors. Encode your texts in batches and save them next to the labels.
  3. Split before fitting. Hold out a test set first. Split by group (customer, thread) if near-duplicates exist.
  4. Train a simple model. Start with LogisticRegression. Try gradient boosting only if the simple model underperforms.
  5. Compare against the LLM baseline. Run the same test set through a zero-shot prompt and compare accuracy, latency and cost per 1,000 predictions.
from sentence_transformers import SentenceTransformer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

model = SentenceTransformer("all-MiniLM-L6-v2")
X = model.encode(texts, batch_size=64)
X_tr, X_te, y_tr, y_te = train_test_split(X, labels, test_size=0.2, stratify=labels, random_state=0)
clf = LogisticRegression(max_iter=1000).fit(X_tr, y_tr)
print(clf.score(X_te, y_te))

Before and after

Before: a support team sends each of 50,000 monthly emails to an LLM with the prompt "Is this billing, bug or feature request?" and gets inconsistent labels for borderline emails. After: they label 600 emails once, embed them, fit a logistic regression and keep the LLM only for a weekly spot check. The same email always gets the same label.

What goes wrong

  • Mixing embedding models. Vectors from the model used at training time must be the ones used in production. Changing the model means retraining.
  • Leaky splits. Near-duplicate texts on both sides of the split inflate accuracy.
  • Skipping the baseline. Without a zero-shot comparison you cannot say whether the extra pipeline earned its place.
  • Wrong tool for the task. Embeddings capture topic and meaning well. Tasks that hinge on exact wording, numbers or negation may need fine-tuning or an LLM.

Examples

Few labels, one afternoon

A team has 400 labelled app-store reviews in four classes. They embed them with a Sentence Transformers model, fit a logistic regression and score the held-out 80 reviews. The same 80 go through a zero-shot LLM prompt. They put both in one table (accuracy, seconds per 1,000 texts, cost per 1,000 texts) and decide with numbers. SetFit is the next step if the score is short, since it fine-tunes the embedding model with few examples.

The model swap

Production switches to a newer embedding model without retraining. Accuracy drops with no error message, because the classifier's weights were fit to a different vector space. The fix is to store the model name with the classifier and fail loudly when they disagree.

Key points

  • An embedding plus a simple classifier gives deterministic, millisecond predictions at a tiny fraction of the per-call cost of prompting an LLM.
  • Fix and record the embedding model: training and serving vectors must come from the same model and version.
  • Start with logistic regression on the vectors and only add complexity if the held-out score demands it.
  • Always run a zero-shot LLM baseline on the same test set so the comparison covers accuracy, latency and cost.
  • Split before fitting, and group near-duplicates so the test score is honest.

Common mistakes

  • Embedding with one model at training time and another at serving time.
  • Reporting accuracy from a random split when the data holds near-duplicates or repeat customers.
  • Reaching for gradient boosting or a neural head before trying logistic regression.
  • Comparing against an LLM without measuring latency and cost, only accuracy.

Check yourself · 2 of 3

A classifier was trained on vectors from one embedding model. Production now embeds with a different model. What happens?

Choose one answer

Check yourself · 3 of 3

Which result tells you the embedding classifier earns its place against a zero-shot LLM?

Choose one answer

Practice items, not counted towards your mastery.

Start here