logit bias
**Logit Bias** is a **mechanism for directly manipulating the probability of specific tokens in LLM output by adding a bias value to their logits before the softmax step** — enabling precise, deterministic control over generation by forcing specific tokens to appear (large positive bias) or preventing them from appearing (large negative bias), used for enforcing output formats, banning unwanted words, and steering classification outputs in production LLM applications.
**What Is Logit Bias?**
- **Definition**: A parameter available in LLM APIs (OpenAI, Anthropic) that adds a numerical value to the logit (pre-softmax score) of specified tokens — a positive bias increases the token's probability, a negative bias decreases it, and extreme values (+100 or -100) effectively force or ban the token.
- **Token-Level Control**: Logit bias operates on individual tokens (as defined by the model's tokenizer), not words — a word like "unfortunately" might be split into multiple tokens, requiring bias on each token ID. This requires knowledge of the tokenizer's vocabulary.
- **Pre-Softmax Modification**: The bias is added before softmax normalization — a bias of +5 on a token with logit 2.0 changes it to 7.0, dramatically increasing its probability relative to other tokens. A bias of -100 effectively sets the probability to zero.
- **API Parameter**: In OpenAI's API: `logit_bias: {"token_id": bias_value}` — accepts a dictionary mapping token IDs (integers) to bias values (floats from -100 to +100).
**Why Logit Bias Matters**
- **Format Enforcement**: Bias toward opening brackets `{` or `[` to ensure JSON output — more reliable than prompt instructions alone for structured output.
- **Word Banning**: Negative bias on competitor names, profanity, or sensitive terms — deterministically prevents these tokens from appearing regardless of prompt.
- **Classification Steering**: For yes/no or true/false classification, bias toward the answer tokens — ensuring the model responds with the expected format rather than verbose explanations.
- **Deterministic Control**: Unlike prompt engineering (which is probabilistic), logit bias provides deterministic token-level control — a token with -100 bias will never appear, period.
**Logit Bias Applications**
| Use Case | Bias Direction | Example |
|----------|---------------|---------|
| Force JSON output | +5 to +20 on `{`, `[` | Structured API responses |
| Ban specific words | -100 on unwanted tokens | Content filtering |
| Steer classification | +10 on "True"/"False" tokens | Binary classification |
| Reduce repetition | -2 to -5 on recently used tokens | Diverse generation |
| Language control | -100 on non-target language tokens | Monolingual output |
| Brand safety | -100 on competitor name tokens | Marketing content |
**Logit bias is the precision tool for deterministic control over LLM token generation** — directly modifying pre-softmax scores to force, ban, or adjust the probability of specific tokens, providing the reliable, programmatic output control that prompt engineering alone cannot guarantee for production applications requiring strict format compliance or content restrictions.