Text Classification: Approaches and Trade-offs
~45 min read
Zero-shot generative, embedding+classifier, and fine-tuned encoder approaches with decision criteria.
Text classification maps text → label. The right approach depends on how much labeled data you have and what inference cost/latency you can afford.
Approach 1: Zero-Shot Generative Prompt a capable LLM with the text and label options.
Classify this customer message into one of: [billing, shipping, returns, other]
Message: 'I never got my package and it's been 3 weeks'
Category:
- Data needed: 0 examples
- Latency: 1-5 seconds
- Cost: $0.001-0.05 per text
- Accuracy: excellent for ≤10 clear categories, degrades at 20+
- Best for: prototyping, rare labels, categories needing reasoning
Approach 2: Zero-Shot via Embedding Similarity Embed the text and each label string. Assign the closest label.
label_embeddings = {label: embed(label) for label in labels} text_emb = embed(text) predicted = max(labels, key=lambda l: cosine_sim(text_emb, label_embeddings[l]))
- Data needed: 0 examples
- Latency: < 100ms (no LLM generation)
- Accuracy: good for semantically clear labels, poor for domain-specific jargon
Approach 3: Embedding + Classifier Head Embed all labeled examples once, then train a classical ML classifier. Steps: embed corpus → split train/test → fit LogReg/SVM → evaluate → serve
- Data needed: 100+ examples (1K+ for reliable results)
- Latency: < 10ms at inference (no embedding API call if cached)
- Cost: negligible at inference (classifier only, no LLM)
- Accuracy: excellent for well-defined tasks with sufficient data
Approach 4: Fine-Tuned Encoder (BERT/RoBERTa) Update the encoder weights on your labeled data (full fine-tune or adapter).
- Data needed: 1K-100K examples
- Training cost: GPU hours
- Accuracy: highest ceiling, captures domain-specific patterns
- Use when: production-critical, domain is very specialized, have sufficient data
Few-Shot with In-Context Examples For generative approaches, include 3-5 (text, label) examples before the target. Improves accuracy significantly when labels are ambiguous or examples clarify edge cases.
💬 Deep Dive with AI
Key points
- •Embed+classifier is the pragmatic production workhorse: fast, cheap, and accurate once you have 500+ labeled examples
- •Zero-shot embedding similarity is a powerful 0-data baseline — embed text and labels, pick the closest label via cosine similarity
- •Fine-tuned encoders have the highest accuracy ceiling but need 1K+ examples and GPU training time to justify