KatInterview

Build a Binary Classifier from LLM Token Log-Probabilities

Machine LearningModel answer
MediumModel answerMachine Learningllmclassificationnumerical-stability

Last updated

Design a binary text classifier that uses only per-token log-probabilities from an LLM helper, covering scoring math, batching, calibration, and evaluation.

You have access to a helper function score_batch(prompt, inputs) that, given a system prompt and a list of input strings, returns the LLM's per-token probabilities (possibly as log-probabilities) for each input under that prompt. Your task is to design a complete binary text classifier using only this helper — no fine-tuning, no separate model training.

Specifically, cover the following:

  1. Prompt construction. Write two system prompts, one for each class, and explain what design principles make them effective and fair.

  2. Scoring. For each input, call the helper with both prompts and produce a continuous score s ∈ [0, 1] representing P(class A | x), not a hard label. If the helper returns log-probabilities, show how to compute this score in a numerically stable way (e.g., log-sum-exp, log-space normalization).

  3. Token aggregation and batching. Explain how per-token probabilities roll up into a single sequence-level score. Describe how to batch inputs efficiently and why batching decisions matter.

  4. Threshold selection and evaluation. Discuss which metrics to use (ROC-AUC, precision/recall, F1, calibration measures), how to pick an operating threshold, and how to calibrate the raw scores.

  5. Performance improvements. Propose concrete ways to improve accuracy or reduce cost: prompt engineering, prompt ensembling, repeated context sampling, temperature handling, class-imbalance mitigation.

  6. Failure modes and offline iteration. Identify key failure modes and describe a disciplined offline validation loop that lets you iterate without fine-tuning.

Provide pseudocode and state the inference-time complexity.

Hints

Hint 1

Treat the LLM as a class-conditional language model: compare how likely the input is under a class-A prompt versus a class-B prompt, then apply Bayes' rule.

Hint 2

Raw likelihoods underflow for long sequences; stay in log space and use log-sum-exp or a stable sigmoid of the log-prob margin.

Hint 3

Sum vs. mean of log-probs changes how length affects ranking; pick empirically on a dev set, or let a calibrator learn a length-dependent scale.

Hint 4

Prompt surface bias can be detected with content-free inputs and corrected by subtracting the bias offset from the log-prob margin.