AI Auto-Labeling Implementation
Auto-labeling is using existing models to automatically label new data. Goal: reduce manual work by 60-80% while keeping dataset quality above the threshold needed for training. Key question—which examples to accept automatically and which to send for manual review.
Auto-Labeling Strategies by Task Type
from anthropic import Anthropic
import numpy as np
import pandas as pd
from dataclasses import dataclass
from typing import Optional
@dataclass
class AutoLabelResult:
text: str
predicted_label: str
confidence: float
auto_accepted: bool
method: str # 'weak_model', 'llm', 'rules', 'ensemble'
class AutoLabelingPipeline:
def __init__(self, task_type: str, confidence_threshold: float = 0.85):
self.task_type = task_type
self.threshold = confidence_threshold
self.llm = Anthropic()
self.stats = {'auto_accepted': 0, 'sent_to_review': 0}
def label_batch(self, texts: list[str],
label_schema: list[str],
method: str = 'ensemble') -> list[AutoLabelResult]:
"""Auto-label batch of texts"""
if method == 'llm':
return self._llm_labeling(texts, label_schema)
elif method == 'weak_model':
return self._weak_model_labeling(texts, label_schema)
elif method == 'ensemble':
return self._ensemble_labeling(texts, label_schema)
else:
raise ValueError(f"Unknown method: {method}")
def _llm_labeling(self, texts: list[str],
label_schema: list[str]) -> list[AutoLabelResult]:
"""LLM-labeling with confidence estimation"""
results = []
batch_size = 10
for i in range(0, len(texts), batch_size):
batch = texts[i:i + batch_size]
texts_formatted = "\n".join([f"{j+1}. {t[:300]}" for j, t in enumerate(batch)])
labels_str = ", ".join(label_schema)
response = self.llm.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=400,
messages=[{
"role": "user",
"content": f"""Classify each text. Labels: {labels_str}
Texts:
{texts_formatted}
Return JSON array with label and confidence (0.0-1.0)."""
}]
)
try:
import json
preds = json.loads(response.content[0].text)
for text, pred in zip(batch, preds):
confidence = pred.get('confidence', 0.5)
results.append(AutoLabelResult(
text=text,
predicted_label=pred['label'],
confidence=confidence,
auto_accepted=confidence >= self.threshold,
method='llm'
))
except Exception:
for text in batch:
results.append(AutoLabelResult(
text=text,
predicted_label='unknown',
confidence=0.0,
auto_accepted=False,
method='llm_failed'
))
return results
Resource Savings at Different Thresholds
| Confidence Threshold | Auto-Accept Rate | Auto-Accepted Accuracy | Manual Work |
|---|---|---|---|
| 0.95 | 35% | 98.5% | 65% of tasks |
| 0.90 | 52% | 97.2% | 48% of tasks |
| 0.85 | 68% | 95.8% | 32% of tasks |
| 0.80 | 78% | 93.1% | 22% of tasks |
| 0.70 | 89% | 88.4% | 11% of tasks |
Optimal threshold for most classification tasks—0.85-0.90: reduce manual work by 65-70% with auto-accepted accuracy 95-97%. Final dataset requires random spot-check of 3-5% of auto-labeled examples for quality validation.







